@Test public void add() throws InstantiationException, IllegalAccessException, CannotCompileException, NotFoundException { ByteCodeProxy byteCodeProxy = new ByteCodeProxy(); UserService2 userService = new UserServiceImpl(); String methodStr = "public void add() {System.out.println(\"--------------------adddd1---------------\");}"; String methodStr2 = "public void add2() {System.out.println(\"--------------------adddd2---------------\");}"; String methodArray[] = new String[]{methodStr,methodStr2}; userService = (UserService2) byteCodeProxy.createMethod(userService, methodArray); userService.add2(); }
public Object createMethod(Object delegate,String[] methods) throws CannotCompileException, InstantiationException, IllegalAccessException, NotFoundException { ClassPool mPool = new ClassPool(true); CtClass mCtc = mPool.makeClass(delegate.getClass().getName() + "JavaassistProxy"); Class [] interfaceArray= delegate.getClass().getInterfaces(); for(int i = 0;i<interfaceArray.length;i++) { mCtc.addInterface(mPool.get(interfaceArray[i].getName())); } for(int i = 0 ;i<methods.length;i++) { mCtc.addMethod(CtNewMethod.make(methods[i], mCtc)); } Class pc = mCtc.toClass(); Object bytecodeProxy = pc.newInstance(); return bytecodeProxy; }
ByteCodeProxy { public Object createMethod(Object delegate,String[] methods) throws CannotCompileException, InstantiationException, IllegalAccessException, NotFoundException { ClassPool mPool = new ClassPool(true); CtClass mCtc = mPool.makeClass(delegate.getClass().getName() + "JavaassistProxy"); Class [] interfaceArray= delegate.getClass().getInterfaces(); for(int i = 0;i<interfaceArray.length;i++) { mCtc.addInterface(mPool.get(interfaceArray[i].getName())); } for(int i = 0 ;i<methods.length;i++) { mCtc.addMethod(CtNewMethod.make(methods[i], mCtc)); } Class pc = mCtc.toClass(); Object bytecodeProxy = pc.newInstance(); return bytecodeProxy; } }
ByteCodeProxy { public Object createMethod(Object delegate,String[] methods) throws CannotCompileException, InstantiationException, IllegalAccessException, NotFoundException { ClassPool mPool = new ClassPool(true); CtClass mCtc = mPool.makeClass(delegate.getClass().getName() + "JavaassistProxy"); Class [] interfaceArray= delegate.getClass().getInterfaces(); for(int i = 0;i<interfaceArray.length;i++) { mCtc.addInterface(mPool.get(interfaceArray[i].getName())); } for(int i = 0 ;i<methods.length;i++) { mCtc.addMethod(CtNewMethod.make(methods[i], mCtc)); } Class pc = mCtc.toClass(); Object bytecodeProxy = pc.newInstance(); return bytecodeProxy; }  }
ByteCodeProxy { public Object createMethod(Object delegate,String[] methods) throws CannotCompileException, InstantiationException, IllegalAccessException, NotFoundException { ClassPool mPool = new ClassPool(true); CtClass mCtc = mPool.makeClass(delegate.getClass().getName() + "JavaassistProxy"); Class [] interfaceArray= delegate.getClass().getInterfaces(); for(int i = 0;i<interfaceArray.length;i++) { mCtc.addInterface(mPool.get(interfaceArray[i].getName())); } for(int i = 0 ;i<methods.length;i++) { mCtc.addMethod(CtNewMethod.make(methods[i], mCtc)); } Class pc = mCtc.toClass(); Object bytecodeProxy = pc.newInstance(); return bytecodeProxy; }  Object createMethod(Object delegate,String[] methods); }
ByteCodeProxy { public Object createMethod(Object delegate,String[] methods) throws CannotCompileException, InstantiationException, IllegalAccessException, NotFoundException { ClassPool mPool = new ClassPool(true); CtClass mCtc = mPool.makeClass(delegate.getClass().getName() + "JavaassistProxy"); Class [] interfaceArray= delegate.getClass().getInterfaces(); for(int i = 0;i<interfaceArray.length;i++) { mCtc.addInterface(mPool.get(interfaceArray[i].getName())); } for(int i = 0 ;i<methods.length;i++) { mCtc.addMethod(CtNewMethod.make(methods[i], mCtc)); } Class pc = mCtc.toClass(); Object bytecodeProxy = pc.newInstance(); return bytecodeProxy; }  Object createMethod(Object delegate,String[] methods);  }