1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
import java.lang.reflect.Method;
public class Test {
public Test() {
}
public int allo(int x) {
System.out.println(x);
return x;
}
public Object lancerMethode(Object obj, Object[] args, String nomMethode) throws Exception {
Class[] paramTypes = null;
if (args != null) {
paramTypes = new Class[args.length];
for (int i = 0; i < args.length; ++i) {
paramTypes[i] = args[i].getClass();
}
}
Method m = obj.getClass().getMethod(nomMethode, paramTypes);
return m.invoke(obj, args);
}
public static void main(String[] args) throws Exception {
Test t = new Test();
Object parametres[] = { new Integer(-1) };
t.lancerMethode(new Test(), parametres , "allo");
}
} |
Partager