1 2 3 4 5 6 7 8 9 10 11 12 13
|
public class JavassistTest { public static void main(String[] args) throws NotFoundException, CannotCompileException, IllegalAccessException, InstantiationException, IOException { ClassPool cp = ClassPool.getDefault(); CtClass aa = cp.get("cn.vgbhfive.bytecodedemo.A"); CtMethod m = aa.getDeclaredMethod("operation"); m.insertBefore("{ System.out.println(\"start\"); }"); m.insertAfter("{ System.out.println(\"end\"); }"); Class c = aa.toClass(); aa.writeFile("F://workSpace/projects"); A a = (A) c.newInstance(); a.operation(); } }
|