I'm stuck with javassist. I've added a new method to my object class on runtime.
My object class:
package tmp3;
public class Car {
public Car(){}
}
My test class:
package tmp3;
import java.lang.reflect.Method;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtMethod;
import javassist.CtNewMethod;
public class TestMain {
public static void main(String[] args) {
try {
CtClass ctclass = ClassPool.getDefault().get("tmp3.Car");
CtMethod newmethod = CtNewMethod.make("public void testPrint() { System.out.println(\"test ok\"); }",ctclass);
ctclass.addMethod(newmethod);
ctclass.writeFile();
for(Method me: ctclass.toClass().getDeclaredMethods()){ //test print, ok
System.out.println(me.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
But after that point, I don't know how to call(invoke) it. I've read that javassist has not the capability to invoke methods. Then how can I invoke the method which I've just added with javassist?
I've tried lots of things in two days but had no success. Could you please help me with this?