1 package sample; 2 3 import javassist.*; 4 5 20 public class Test { 21 public int f(int i) { 22 return i + 1; 23 } 24 25 public static void main(String [] args) throws Exception { 26 ClassPool pool = ClassPool.getDefault(); 27 28 CtClass cc = pool.get("sample.Test"); 29 try { 30 cc.getDeclaredMethod("g"); 31 System.out.println("g() is already defined in sample.Test."); 32 } 33 catch (NotFoundException e) { 34 37 CtMethod fMethod = cc.getDeclaredMethod("f"); 38 CtMethod gMethod = CtNewMethod.copy(fMethod, "g", cc, null); 39 cc.addMethod(gMethod); 40 cc.writeFile(); System.out.println("g() was added."); 42 } 43 } 44 } 45 | Popular Tags |