Javassist之常用API的应用 02

 

测试模型代码:

 1 package org.study2.JavaSenior.annotation.javassistDemo;
 2 
 3 /**
 4  * @Auther:GongXingRui
 5  * @Date:2018/10/30
 6  * @Description:
 7  **/
 8 
 9 @Author(name = "gxr", year = 2018)
10 public class Emp {
11 
12     private int empno;
13     private String ename;
14 
15     public void sayHello(int a) {
16         System.out.println("sayHello," + a);
17     }
18 
19     public int getEmpno() {
20         return empno;
21     }
22 
23     public void setEmpno(int empno) {
24         this.empno = empno;
25     }
26 
27     public String getEname() {
28         return ename;
29     }
30 
31     public void setEname(String ename) {
32         this.ename = ename;
33     }
34 
35     public Emp(int empno, String ename) {
36         super();
37         this.empno = empno;
38         this.ename = ename;
39     }
40 
41     public Emp() {
42     }
43 }

 

API应用代码:

  1 package org.study2.JavaSenior.annotation.javassistDemo;
  2 
  3 import javassist.*;
  4 
  5 import java.lang.reflect.Method;
  6 import java.util.Arrays;
  7 
  8 /**
  9  * @Auther:GongXingRui
 10  * @Date:2018/10/30
 11  * @Description: Javassist 常用API的应用
 12  **/
 13 public class JavassistDemo2 {
 14     /**
 15      * 处理类的基本用法
 16      *
 17      * @throws Exception
 18      */
 19     public static void test01() throws Exception {
 20         ClassPool pool = ClassPool.getDefault();
 21         CtClass cc = pool.get("org.study2.JavaSenior.annotation.javassistDemo.Emp");
 22 
 23         byte[] bytes = cc.toBytecode();
 24         System.out.println(Arrays.toString(bytes));
 25 
 26         System.out.println(cc.getName()); //获取类名
 27         System.out.println(cc.getSimpleName()); //获取简要类名
 28         System.out.println(cc.getSuperclass()); //获得父类
 29         System.out.println(cc.getInterfaces()); //获得接口
 30 
 31     }
 32 
 33     /**
 34      * 测试产生新的方法
 35      *
 36      * @throws Exception
 37      */
 38     public static void test02() throws Exception {
 39         ClassPool pool = ClassPool.getDefault();
 40         CtClass cc = pool.get("org.study2.JavaSenior.annotation.javassistDemo.Emp");
 41 
 42         // CtMethod m = CtNewMethod.make("public int add(int a,int b){return a+b;}", cc);
 43 
 44         CtMethod m = new CtMethod(CtClass.intType, "add",
 45                 new CtClass[]{CtClass.intType, CtClass.intType}, cc);
 46         m.setModifiers(Modifier.PUBLIC);
 47         m.setBody("{System.out.println(\"www.sxt.cn\");return $1+$2;}");
 48 
 49         cc.addMethod(m);
 50 
 51         //通过反射调用新生成的方法
 52         Class clazz = cc.toClass();
 53         Object obj = clazz.newInstance();  //通过调用Emp无参构造器,创建新的Emp对象
 54         Method method = clazz.getDeclaredMethod("add", int.class, int.class);
 55         Object result = method.invoke(obj, 200, 300);
 56         System.out.println(result);
 57     }
 58 
 59     /**
 60      * 修改已有的方法的信息,修改方法体的内容
 61      *
 62      * @throws Exception
 63      */
 64     public static void test03() throws Exception {
 65         ClassPool pool = ClassPool.getDefault();
 66         CtClass cc = pool.get("org.study2.JavaSenior.annotation.javassistDemo.Emp");
 67 
 68         CtMethod cm = cc.getDeclaredMethod("sayHello", new CtClass[]{CtClass.intType});
 69         cm.insertBefore("System.out.println($1);System.out.println(\"start!!!\");");
 70         cm.insertAt(9, "int b=3;System.out.println(\"b=\"+b);");
 71         cm.insertAfter("System.out.println(\"end!!!\");");
 72 
 73         //通过反射调用新生成的方法
 74         Class clazz = cc.toClass();
 75         Object obj = clazz.newInstance();  //通过调用Emp无参构造器,创建新的Emp对象
 76         Method method = clazz.getDeclaredMethod("sayHello", int.class);
 77         method.invoke(obj, 300);
 78     }
 79 
 80     /**
 81      * 属性的操作
 82      *
 83      * @throws Exception
 84      */
 85     public static void test04() throws Exception {
 86         ClassPool pool = ClassPool.getDefault();
 87         CtClass cc = pool.get("org.study2.JavaSenior.annotation.javassistDemo.Emp");
 88 
 89         //  CtField f1 = CtField.make("private int empno;", cc);
 90         CtField f1 = new CtField(CtClass.intType, "salary", cc);
 91         f1.setModifiers(Modifier.PRIVATE);
 92         cc.addField(f1);
 93 
 94         //  cc.getDeclaredField("ename");   //获取指定的属性
 95 
 96         //增加相应的set和get方法
 97         cc.addMethod(CtNewMethod.getter("getSalary", f1));
 98         cc.addMethod(CtNewMethod.setter("setSalary", f1));
 99 
100         Class clazz = cc.toClass();
101         Object obj = clazz.newInstance();
102         Method method = clazz.getDeclaredMethod("setSalary", int.class);
103         method.invoke(obj, 2000);
104         Method method2 = clazz.getDeclaredMethod("getSalary", null);
105         int n = (int) method2.invoke(obj, null);
106         System.out.println("Salary=" + n);
107 
108     }
109 
110     /**
111      * 构造方法的操作
112      *
113      * @throws Exception
114      */
115     public static void test05() throws Exception {
116         ClassPool pool = ClassPool.getDefault();
117         CtClass cc = pool.get("org.study2.JavaSenior.annotation.javassistDemo.Emp");
118 
119         CtConstructor[] cs = cc.getConstructors();
120         for (CtConstructor c : cs) {
121             System.out.println(c.getLongName());
122         }
123     }
124 
125 
126     public static void test06() throws Exception {
127         CtClass cc = ClassPool.getDefault().get("org.study2.JavaSenior.annotation.javassistDemo.Emp");
128         Object[] all = cc.getAnnotations();
129         Author a = (Author) all[0];
130         String name = a.name();
131         int year = a.year();
132         System.out.println("name: " + name + ", year: " + year);
133 
134     }
135 
136 
137     public static void main(String[] args) throws Exception {
138         test06();
139     }
140 }

 

posted @ 2018-10-31 11:27  星瑞  阅读(254)  评论(0编辑  收藏  举报