1. 程式人生 > >Java之Javassistの常用API-yellowcong

Java之Javassistの常用API-yellowcong

  1 package com.swust.javassist;
  2 
  3 import java.lang.reflect.Field;
  4 import java.lang.reflect.Method;
  5 import java.util.Arrays;
  6 
  7 import javassist.ClassPool;
  8 import javassist.CtClass;
  9 import javassist.CtConstructor;
 10 import javassist.CtField;
 11 import javassist.CtMethod;
12 import javassist.CtNewMethod; 13 import javassist.Modifier; 14 15 public class Example2 { 16 //獲取類的簡單資訊 17 public static void test01() throws Exception{ 18 ClassPool pool = ClassPool.getDefault(); 19 CtClass cc = pool.get("com.swust.beans.Person"
); 20 //得到位元組碼 21 byte[] bytes = cc.toBytecode(); 22 System.out.println(Arrays.toString(bytes)); 23 System.out.println(cc.getName());//獲取類名 24 System.out.println(cc.getSimpleName());//獲取簡要類名 25 System.out.println(cc.getSuperclass());//
獲取父類 26 System.out.println(cc.getInterfaces());//獲取介面 27 System.out.println(cc.getMethods());//獲取 28 } 29 //新生成一個方法 30 public static void test02() throws Exception{ 31 ClassPool pool = ClassPool.getDefault(); 32 CtClass cc = pool.get("com.swust.beans.Person"); 33 //第一種 34 //CtMethod cm = CtMethod.make("public String getName(){return name;}", cc); 35 //第二種 36 //引數:返回值型別,方法名,引數,物件 37 CtMethod cm = new CtMethod(CtClass.intType,"add",new CtClass[]{CtClass.intType,CtClass.intType},cc); 38 cm.setModifiers(Modifier.PUBLIC);//訪問範圍 39 cm.setBody("{return $1+$2;}"); 40 //cc.removeMethod(m) 刪除一個方法 41 cc.addMethod(cm); 42 //通過反射呼叫方法 43 Class clazz = cc.toClass(); 44 Object obj = clazz.newInstance();//通過呼叫無參構造器,生成新的物件 45 Method m = clazz.getDeclaredMethod("add", int.class,int.class); 46 Object result = m.invoke(obj, 2,3); 47 System.out.println(result); 48 } 49 50 //修改已有的方法 51 public static void test03() throws Exception{ 52 ClassPool pool = ClassPool.getDefault(); 53 CtClass cc = pool.get("bean.User"); 54 55 CtMethod cm = cc.getDeclaredMethod("hello",new CtClass[]{pool.get("java.lang.String")}); 56 cm.insertBefore("System.out.println(\"呼叫前\");");//呼叫前 57 cm.insertAt(29, "System.out.println(\"29\");");//行號 58 cm.insertAfter("System.out.println(\"呼叫後\");");//呼叫後 59 60 //通過反射呼叫方法 61 Class clazz = cc.toClass(); 62 Object obj = clazz.newInstance(); 63 Method m = clazz.getDeclaredMethod("hello", String.class); 64 Object result = m.invoke(obj, "張三"); 65 System.out.println(result); 66 } 67 68 //修改已有屬性 69 public static void test04() throws Exception{ 70 ClassPool pool = ClassPool.getDefault(); 71 CtClass cc = pool.get("bean.User"); 72 73 //屬性 74 CtField cf = new CtField(CtClass.intType,"age",cc); 75 cf.setModifiers(Modifier.PRIVATE); 76 cc.addField(cf); 77 //增加響應的get set方法 78 cc.addMethod(CtNewMethod.getter("getAge",cf)); 79 cc.addMethod(CtNewMethod.setter("setAge",cf)); 80 81 //訪問屬性 82 Class clazz = cc.toClass(); 83 Object obj = clazz.newInstance(); 84 Field field = clazz.getDeclaredField("age"); 85 System.out.println(field); 86 Method m = clazz.getDeclaredMethod("setAge", int.class); 87 m.invoke(obj, 16); 88 Method m2 = clazz.getDeclaredMethod("getAge", null); 89 Object resutl = m2.invoke(obj,null); 90 System.out.println(resutl); 91 } 92 93 //操作構造方法 94 public static void test05() throws Exception{ 95 ClassPool pool = ClassPool.getDefault(); 96 CtClass cc = pool.get("com.swust.beans.Person"); 97 98 CtConstructor[] cons = cc.getConstructors(); 99 for(CtConstructor con:cons){ 100 System.out.println(con); 101 } 102 } 103 public static void main(String[] args) throws Exception { 104 test01(); 105 //test02(); 106 //test03(); 107 //test04(); 108 test05(); 109 } 110 }