I'm trying to figure out how to invoke a method. I get it all but it's not working. Here's my code:
public class WrapperGenerator {
public static void generateWrapper(Class wrappedClass)
throws /*...*/ {
String className = wrappedClass.getSimpleName()+"Wrapper";
Object wrappedInstance = wrappedClass.newInstance();
JCodeModel jcm = new JCodeModel();
JDefinedClass jdc = jcm._class(className);
Method[] methods = wrappedClass.getDeclaredMethods();
for(Method method : methods) {
JMethod jm = jdc.method(0, method.getReturnType(), method.getName());
Object[] params = new Object[method.getParameters().length];
for(int i=0,size=method.getParameters().length ; i<size ; ++i) {
Parameter param = method.getParameters()[i];
params[i] = jm.param(0, param.getType(), "arg" + (i++));
}
method.invoke(wrappedInstance//exception thrown here
, params);
jm.body()._return();
}
File file = new File("./");
file.mkdirs();
jcm.build(file);
}
}
and here is the stack trace:
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at types.WrapperGenerator.generateWrapper(WrapperGenerator.java:54)
at types.Main.main(Main.java:15)
I have no idea what is wrong, as only the first argument(so far) is wrong. But I found out on the Internet that it should be a newInstance
and it makes sens. Any ideas?
EDIT:
I made a short version to avoid confusion, heres, the code(stack trace's pretty much the same):
public class Wrapper {
public static void generateWrapper(Class wrappedClass)
throws InstantiationException, IllegalAccessException,
IllegalArgumentException, InvocationTargetException {
Object wrappedInstance = wrappedClass.newInstance();
Method[] methods = wrappedClass.getDeclaredMethods();
for(Method method : methods) {
Object[] params = new Object[method.getParameters().length];
for(int i=0,size=method.getParameters().length ; i<size ; ++i) {
params[i] = method.getParameters()[i];
}
method.invoke(wrappedInstance//exception here
, params);
}
File file = new File("./src/types/");
file.mkdirs();
}
}
Aucun commentaire:
Enregistrer un commentaire