|
3 | 3 | import javassist.*;
|
4 | 4 |
|
5 | 5 | import java.io.IOException;
|
| 6 | +import java.lang.reflect.Field; |
6 | 7 | import java.lang.reflect.Method;
|
7 | 8 | import java.text.MessageFormat;
|
8 | 9 | import java.util.Arrays;
|
@@ -54,23 +55,68 @@ public static void test02() throws Exception {
|
54 | 55 |
|
55 | 56 | }
|
56 | 57 |
|
57 |
| - |
| 58 | + /** |
| 59 | + * 修改已有方法的信息 |
| 60 | + * @throws Exception |
| 61 | + */ |
58 | 62 | public static void test03()throws Exception{
|
59 | 63 | ClassPool pool = ClassPool.getDefault();
|
60 | 64 | CtClass cc = pool.get("com.zyj.javassist.Emp");
|
61 | 65 |
|
62 | 66 | CtMethod cm = cc.getDeclaredMethod("sayHello",new CtClass[]{CtClass.intType});
|
63 | 67 | cm.insertBefore("System.out.println($1);System.out.println(\"start!!!\");");
|
64 |
| - |
| 68 | + cm.insertAfter("System.out.println(\"end!!!!!!\");"); |
| 69 | + //在某一行前面加上代码 |
| 70 | + cm.insertAt(27,"empno = \"19191\";"); |
65 | 71 | //通过反射调用新生成的方法
|
66 | 72 | Class clazz = cc.toClass();
|
67 | 73 | Object obj = clazz.newInstance();
|
68 | 74 | Method m = clazz.getDeclaredMethod("sayHello",int.class);
|
69 |
| - Object result = m.invoke(obj, 1111); |
70 |
| - System.out.println(result); |
| 75 | + m.invoke(obj, 1111); |
| 76 | + Method m2 = clazz.getDeclaredMethod("getEmpno",null); |
| 77 | + Object o = m2.invoke(obj); |
| 78 | + System.out.println(o); |
| 79 | + } |
| 80 | + |
| 81 | + public static void test04()throws Exception{ |
| 82 | + ClassPool pool = ClassPool.getDefault(); |
| 83 | + CtClass cc = pool.get("com.zyj.javassist.Emp"); |
| 84 | + |
| 85 | + //CtField f1 = CtField.make("private int no;",cc); |
| 86 | + |
| 87 | + CtField f1 = new CtField(CtClass.intType,"salary",cc); |
| 88 | + f1.setModifiers(Modifier.PRIVATE); |
| 89 | + cc.addField(f1); |
| 90 | + //获取指定的属性 |
| 91 | + cc.getDeclaredField("salary"); |
| 92 | + //增加相应的get和set方法 |
| 93 | + /*cc.addMethod(CtNewMethod.getter("getSalary",f1)); //不可行,原因未知 |
| 94 | + cc.addMethod(CtNewMethod.getter("setSalary",f1));*/ |
| 95 | + //第一张方法::这样可行 |
| 96 | + /*cc.addMethod(CtNewMethod.make("public void setSalary(int salary){this.salary = salary;}",cc)); |
| 97 | + cc.addMethod(CtNewMethod.make("public int getSalary(){return this.salary;}",cc));*/ |
71 | 98 |
|
| 99 | + //第二种方法:亲测可行 |
| 100 | + CtMethod setSalary = new CtMethod(CtClass.voidType, "setSalary", new CtClass[]{CtClass.intType}, cc); |
| 101 | + setSalary.setBody("this.salary = salary;"); |
| 102 | + setSalary.setModifiers(Modifier.PUBLIC); |
| 103 | + CtMethod getSalary = new CtMethod(CtClass.intType, "getSalary", null, cc); |
| 104 | + getSalary.setBody("return salary;"); |
| 105 | + getSalary.setModifiers(Modifier.PRIVATE); |
| 106 | + cc.addMethod(setSalary); |
| 107 | + cc.addMethod(getSalary); |
| 108 | + Class clazz = cc.toClass(); |
| 109 | + Object obj = clazz.newInstance(); |
| 110 | + Field field = clazz.getDeclaredField("salary"); |
| 111 | + System.out.println(field.getName()); |
| 112 | + Method setter = clazz.getDeclaredMethod("setSalary",int.class); |
| 113 | + setter.invoke(obj,3000); |
| 114 | + Method getter = clazz.getDeclaredMethod("getSalary"); |
| 115 | + getter.setAccessible(true); |
| 116 | + Object re = getter.invoke(obj); |
| 117 | + System.out.println("salary:::"+re); |
72 | 118 | }
|
73 | 119 | public static void main(String[] args) throws Exception {
|
74 |
| - test03(); |
| 120 | + test04(); |
75 | 121 | }
|
76 | 122 | }
|
0 commit comments