169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpackage sample; 269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalimport javassist.*; 469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal/* 669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal A very simple sample program 769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal This program overwrites sample/Test.class (the class file of this 969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal class itself) for adding a method g(). If the method g() is not 1069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal defined in class Test, then this program adds a copy of 1169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal f() to the class Test with name g(). Otherwise, this program does 1269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal not modify sample/Test.class at all. 1369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal To see the modified class definition, execute: 1569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal % javap sample.Test 1769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 1869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal after running this program. 1969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal*/ 2069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigalpublic class Test { 2169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public int f(int i) { 2269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal return i + 1; 2369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 2469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal public static void main(String[] args) throws Exception { 2669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal ClassPool pool = ClassPool.getDefault(); 2769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal 2869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtClass cc = pool.get("sample.Test"); 2969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal try { 3069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal cc.getDeclaredMethod("g"); 3169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal System.out.println("g() is already defined in sample.Test."); 3269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 3369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal catch (NotFoundException e) { 3469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal /* getDeclaredMethod() throws an exception if g() 3569e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal * is not defined in sample.Test. 3669e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal */ 3769e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtMethod fMethod = cc.getDeclaredMethod("f"); 3869e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal CtMethod gMethod = CtNewMethod.copy(fMethod, "g", cc, null); 3969e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal cc.addMethod(gMethod); 4069e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal cc.writeFile(); // update the class file 4169e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal System.out.println("g() was added."); 4269e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 4369e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal } 4469e17611504376e4d4603925f8528dfc890fd2c6Luis Sigal} 45