下面就是一個具體的demo來介紹利用Javaassist庫來建立類,不過要先在工程裏面導入Javaassist的架包,java
package JavaAasist; import java.lang.reflect.Method; import java.util.Arrays; import javassist.ClassPool; import javassist.CtClass; import javassist.CtConstructor; import javassist.CtField; import javassist.CtMethod; import javassist.CtNewMethod; import javassist.Modifier; /** * 測試javassist的API * @author 尚學堂高淇 www.sxt.cn * */ public class DEmo02 { /** * 處理類的基本用法 * @throws Exception */ public static void test01() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("com.bjsxt.test.Emp"); byte[] bytes = cc.toBytecode(); System.out.println(Arrays.toString(bytes)); System.out.println(cc.getName()); //獲取類名 System.out.println(cc.getSimpleName()); //獲取簡要類名 System.out.println(cc.getSuperclass()); //得到父類 System.out.println(cc.getInterfaces()); //得到接口 } /** * 測試產生新的方法 * @throws Exception */ public static void test02() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("com.bjsxt.test.Emp"); // CtMethod m = CtNewMethod.make("public int add(int a,int b){return a+b;}", cc); CtMethod m = new CtMethod(CtClass.intType,"add", new CtClass[]{CtClass.intType,CtClass.intType},cc); m.setModifiers(Modifier.PUBLIC); m.setBody("{System.out.println(\"www.sxt.cn\");return $1+$2;}"); cc.addMethod(m); //經過反射調用新生成的方法 Class clazz = cc.toClass(); Object obj = clazz.newInstance(); //經過調用Emp無參構造器,建立新的Emp對象 Method method = clazz.getDeclaredMethod("add", int.class,int.class); Object result = method.invoke(obj, 200,300); System.out.println(result); } /** * 修改已有的方法的信息,修改方法體的內容 * @throws Exception */ public static void test03() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("com.bjsxt.test.Emp"); CtMethod cm = cc.getDeclaredMethod("sayHello",new CtClass[]{CtClass.intType}); cm.insertBefore("System.out.println($1);System.out.println(\"start!!!\");"); cm.insertAt(9, "int b=3;System.out.println(\"b=\"+b);"); cm.insertAfter("System.out.println(\"end!!!\");"); //經過反射調用新生成的方法 Class clazz = cc.toClass(); Object obj = clazz.newInstance(); //經過調用Emp無參構造器,建立新的Emp對象 Method method = clazz.getDeclaredMethod("sayHello", int.class); method.invoke(obj, 300); } /** * 屬性的操做 * @throws Exception */ public static void test04() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("com.bjsxt.test.Emp"); // CtField f1 = CtField.make("private int empno;", cc); CtField f1 = new CtField(CtClass.intType,"salary",cc); f1.setModifiers(Modifier.PRIVATE); cc.addField(f1); // cc.getDeclaredField("ename"); //獲取指定的屬性 //增長相應的set和get方法 cc.addMethod(CtNewMethod.getter("getSalary", f1));; cc.addMethod(CtNewMethod.getter("setSalary", f1));; } /** * 構造方法的操做 * @throws Exception */ public static void test05() throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("com.bjsxt.test.Emp"); CtConstructor[] cs = cc.getConstructors(); for (CtConstructor c : cs) { System.out.println(c.getLongName()); } } public static void test06() throws Exception{ CtClass cc = ClassPool.getDefault().get("com.bjsxt.test.Emp"); Object[] all = cc.getAnnotations(); Author a = (Author)all[0]; String name = a.name(); int year = a.year(); System.out.println("name: " + name + ", year: " + year); } public static void main(String[] args) throws Exception { test06(); } }
運行改代碼則在對應路徑生成一個java文件,不過該文件須要反編譯才能夠查看,具體的工具是xjad工具
下面是JavaAssist處理類的demo測試
package JavaAssist; import java.lang.reflect.Method; import java.util.Arrays; import javassist.ClassPool; import javassist.CtClass; import javassist.CtConstructor; import javassist.CtField; import javassist.CtMethod; import javassist.CtNewMethod; import javassist.Modifier; /** * 測試javassist的API * @author 19579 * */ public class DEmo02 { /** * 處理類的基本用法 * @throws Exception */ public static void test01() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("JavaAssist.Emp"); byte[] bytes = cc.toBytecode(); System.out.println(Arrays.toString(bytes)); System.out.println(cc.getName()); //獲取類名 System.out.println(cc.getSimpleName()); //獲取簡要類名 System.out.println(cc.getSuperclass()); //得到父類 System.out.println(cc.getInterfaces()); //得到接口 } /** * 測試產生新的方法 * @throws Exception */ public static void test02() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("JavaAssist.Emp"); // CtMethod m = CtNewMethod.make("public int add(int a,int b){return a+b;}", cc); CtMethod m = new CtMethod(CtClass.intType,"add", new CtClass[]{CtClass.intType,CtClass.intType},cc); m.setModifiers(Modifier.PUBLIC); m.setBody("{System.out.println(\"www.sxt.cn\");return $1+$2;}");//$1和$2分別指兩個形參 cc.addMethod(m); //經過反射調用新生成的方法 Class clazz = cc.toClass(); Object obj = clazz.newInstance(); //經過調用Emp無參構造器,建立新的Emp對象 Method method = clazz.getDeclaredMethod("add", int.class,int.class); Object result = method.invoke(obj, 200,300); System.out.println(result); } /** * 修改已有的方法的信息,修改方法體的內容 * @throws Exception */ public static void test03() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("JavaAssist.Emp"); CtMethod cm = cc.getDeclaredMethod("sayHello",new CtClass[]{CtClass.intType}); cm.insertBefore("System.out.println($1);System.out.println(\"start!!!\");");//在方法體前加入 cm.insertAt(9, "int b=3;System.out.println(\"b=\"+b);");//具體的第9行加入 cm.insertAfter("System.out.println(\"end!!!\");");//在方法後面加入 //經過反射調用新生成的方法 Class clazz = cc.toClass(); Object obj = clazz.newInstance(); //經過調用Emp無參構造器,建立新的Emp對象 Method method = clazz.getDeclaredMethod("sayHello", int.class); method.invoke(obj, 300); } /** * 屬性的操做 * @throws Exception */ public static void test04() throws Exception{ ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("JavaAssist.Emp"); // CtField f1 = CtField.make("private int empno;", cc); CtField f1 = new CtField(CtClass.intType,"salary",cc); f1.setModifiers(Modifier.PRIVATE); cc.addField(f1); // cc.getDeclaredField("ename"); //獲取指定的屬性 //增長相應的set和get方法 cc.addMethod(CtNewMethod.getter("getSalary", f1));; cc.addMethod(CtNewMethod.getter("setSalary", f1));; } /** * 構造方法的操做 * @throws Exception */ public static void test05() throws Exception { ClassPool pool = ClassPool.getDefault(); CtClass cc = pool.get("JavaAssist.Emp"); CtConstructor[] cs = cc.getConstructors(); for (CtConstructor c : cs) { System.out.println(c.getLongName()); } } public static void test06() throws Exception{ CtClass cc = ClassPool.getDefault().get("JavaAssist.Emp"); Object[] all = cc.getAnnotations(); Author a = (Author)all[0]; String name = a.name(); int year = a.year(); System.out.println("name: " + name + ", year: " + year); } public static void main(String[] args) throws Exception { test06(); } }
對應的emp類以下this
package com.bjsxt.test; @Author(name="gaoqi", year=2014) public class Emp { private int empno; private String ename; public void sayHello(int a){ System.out.println("sayHello,"+a); } public int getEmpno() { return empno; } public void setEmpno(int empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public Emp(int empno, String ename) { super(); this.empno = empno; this.ename = ename; } public Emp() { } } Author類以下 package com.bjsxt.test; public @interface Author { String name(); int year(); }
固然javaAssist也有本身的侷限性spa