public class TargetClass {
public void doSomething() {
}
}
ClassPool pool = ClassPool.getDefault();
CtClass targetClass = pool.get("TargetClass");
CtMethod doSomethingMethod = targetClass.getDeclaredMethod("doSomething");
Class modifiedClass = targetClass.toClass();
Object modifiedObject = modifiedClass.newInstance();
Method modifiedMethod = modifiedObject.getClass().getMethod("doSomething");
modifiedMethod.invoke(modifiedObject);