一、前言
关于cglib源码包(相关jar下载)分别基于net.sf.cglib.core.KeyFactory、net.sf.cglib.proxy.MethodInterceptor实现动态接口实现类动态实例化加载、方法拦截,详情参见示例说明。
二、示例代码
1.MyFactorySample示例定义定义MyFactory接口,并通过cglib的KeyFactory.create对接口动态实现并实例化对象
import net.sf.cglib.core.KeyFactory;@b@@b@public class MyFactorySample {@b@ @b@ private interface MyFactory {@b@ public Object newInstance(String aa,String bb);@b@ }@b@ @b@ public static void main(String[] args) {@b@ MyFactory f = (MyFactory)KeyFactory.create(MyFactory.class);@b@ @b@ Object skey1 = f.newInstance("aa", "hello");@b@ Object skey2 = f.newInstance("aa", "hello");@b@ Object skey3 = f.newInstance("bb", "hello");@b@ System.out.println(skey1.equals(skey2));@b@ System.out.println(skey1.equals(skey3));@b@ }@b@}
控制台
true@b@false
2.定义轨迹模拟Trace类,通过代理拦截打印内部变化的详细轨迹日志
import net.sf.cglib.proxy.*;@b@import java.util.*;@b@@b@public class Trace implements MethodInterceptor {@b@ @b@ int ident = 1;@b@ static Trace callback = new Trace();@b@ @b@ private Trace() {@b@ }@b@ @b@ public static Object newInstance( Class clazz ){@b@ try{@b@ Enhancer e = new Enhancer();@b@ e.setSuperclass(clazz);@b@ e.setCallback(callback);@b@ return e.create();@b@ }catch( Throwable e ){@b@ e.printStackTrace(); @b@ throw new Error(e.getMessage());@b@ } @b@ @b@ }@b@ @b@ public static void main(String[] args) {@b@ List list = (List)newInstance(Vector.class);@b@ Object value = "TEST";@b@ list.add(value);@b@ list.contains(value);@b@ try{@b@ list.set(2, "ArrayIndexOutOfBounds" );@b@ }catch( ArrayIndexOutOfBoundsException ignore ){@b@ }@b@ list.add(value + "1");@b@ list.add(value + "2");@b@ list.toString();@b@ list.equals(list); @b@ list.set( 0, null ); @b@ list.toString();@b@ list.add(list);@b@ list.get(1);@b@ list.toArray();@b@ list.remove(list);@b@ list.remove("");@b@ list.containsAll(list);@b@ list.lastIndexOf(value);@b@ }@b@@b@@b@ public Object intercept(Object obj, java.lang.reflect.Method method, Object[] args,@b@ MethodProxy proxy) throws Throwable {@b@ printIdent(ident);@b@ System.out.println( method );@b@ for( int i = 0; i < args.length; i++ ){@b@ printIdent(ident); @b@ System.out.print( "arg" + (i + 1) + ": ");@b@ if( obj == args[i])@b@ System.out.println("this");@b@ else@b@ System.out.println(args[i]);@b@ }@b@ ident++;@b@@b@ Object retValFromSuper = null;@b@ @b@ try {@b@ retValFromSuper = proxy.invokeSuper(obj, args);@b@ ident--;@b@ } catch (Throwable t) {@b@ ident--;@b@ printIdent(ident); @b@ System.out.println("throw " + t ); @b@ System.out.println();@b@ throw t.fillInStackTrace();@b@ }@b@ @b@ printIdent(ident); @b@ System.out.print("return " );@b@ if( obj == retValFromSuper)@b@ System.out.println("this");@b@ else System.out.println(retValFromSuper);@b@ @b@ if(ident == 1)@b@ System.out.println();@b@ @b@ return retValFromSuper;@b@ }@b@ @b@ void printIdent( int ident ){@b@ @b@ while( --ident > 0 ){@b@ System.out.print(".......");@b@ }@b@ System.out.print(" ");@b@ }@b@ @b@}
控制台
public synchronized boolean java.util.Vector.add(java.lang.Object)@b@ arg1: TEST@b@ return true@b@@b@ public boolean java.util.Vector.contains(java.lang.Object)@b@ arg1: TEST@b@....... public synchronized int java.util.Vector.indexOf(java.lang.Object,int)@b@....... arg1: TEST@b@....... arg2: 0@b@....... return 0@b@ return true@b@@b@ public synchronized java.lang.Object java.util.Vector.set(int,java.lang.Object)@b@ arg1: 2@b@ arg2: ArrayIndexOutOfBounds@b@ throw java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 2@b@@b@ public synchronized boolean java.util.Vector.add(java.lang.Object)@b@ arg1: TEST1@b@ return true@b@@b@ public synchronized boolean java.util.Vector.add(java.lang.Object)@b@ arg1: TEST2@b@ return true@b@@b@ public synchronized java.lang.String java.util.Vector.toString()@b@....... public java.util.Iterator java.util.AbstractList.iterator()@b@....... return java.util.AbstractList$Itr@635b9e68@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 0@b@....... return TEST@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 1@b@....... return TEST1@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 2@b@....... return TEST2@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@ return [TEST, TEST1, TEST2]@b@@b@ public synchronized boolean java.util.Vector.equals(java.lang.Object)@b@ arg1: this@b@ return true@b@@b@ public synchronized java.lang.Object java.util.Vector.set(int,java.lang.Object)@b@ arg1: 0@b@ arg2: null@b@ return TEST@b@@b@ public synchronized java.lang.String java.util.Vector.toString()@b@....... public java.util.Iterator java.util.AbstractList.iterator()@b@....... return java.util.AbstractList$Itr@13fcf0ce@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 0@b@....... return null@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 1@b@....... return TEST1@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 2@b@....... return TEST2@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@ return [null, TEST1, TEST2]@b@@b@ public synchronized boolean java.util.Vector.add(java.lang.Object)@b@ arg1: this@b@ return true@b@@b@ public synchronized java.lang.Object java.util.Vector.get(int)@b@ arg1: 1@b@ return TEST1@b@@b@ public synchronized java.lang.Object[] java.util.Vector.toArray()@b@ return [Ljava.lang.Object;@43256ea2@b@@b@ public boolean java.util.Vector.remove(java.lang.Object)@b@ arg1: this@b@....... public synchronized boolean java.util.Vector.removeElement(java.lang.Object)@b@....... arg1: this@b@.............. public int java.util.Vector.indexOf(java.lang.Object)@b@.............. arg1: this@b@..................... public synchronized int java.util.Vector.indexOf(java.lang.Object,int)@b@..................... arg1: this@b@..................... arg2: 0@b@............................ public synchronized boolean java.util.Vector.equals(java.lang.Object)@b@............................ arg1: null@b@............................ return false@b@............................ public synchronized boolean java.util.Vector.equals(java.lang.Object)@b@............................ arg1: TEST1@b@............................ return false@b@............................ public synchronized boolean java.util.Vector.equals(java.lang.Object)@b@............................ arg1: TEST2@b@............................ return false@b@............................ public synchronized boolean java.util.Vector.equals(java.lang.Object)@b@............................ arg1: this@b@............................ return true@b@..................... return 3@b@.............. return 3@b@.............. public synchronized void java.util.Vector.removeElementAt(int)@b@.............. arg1: 3@b@.............. return null@b@....... return true@b@ return true@b@@b@ public boolean java.util.Vector.remove(java.lang.Object)@b@ arg1: @b@....... public synchronized boolean java.util.Vector.removeElement(java.lang.Object)@b@....... arg1: @b@.............. public int java.util.Vector.indexOf(java.lang.Object)@b@.............. arg1: @b@..................... public synchronized int java.util.Vector.indexOf(java.lang.Object,int)@b@..................... arg1: @b@..................... arg2: 0@b@..................... return -1@b@.............. return -1@b@....... return false@b@ return false@b@@b@ public synchronized boolean java.util.Vector.containsAll(java.util.Collection)@b@ arg1: this@b@....... public java.util.Iterator java.util.AbstractList.iterator()@b@....... return java.util.AbstractList$Itr@4e82701e@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 0@b@....... return null@b@....... public boolean java.util.Vector.contains(java.lang.Object)@b@....... arg1: null@b@.............. public synchronized int java.util.Vector.indexOf(java.lang.Object,int)@b@.............. arg1: null@b@.............. arg2: 0@b@.............. return 0@b@....... return true@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 1@b@....... return TEST1@b@....... public boolean java.util.Vector.contains(java.lang.Object)@b@....... arg1: TEST1@b@.............. public synchronized int java.util.Vector.indexOf(java.lang.Object,int)@b@.............. arg1: TEST1@b@.............. arg2: 0@b@.............. return 1@b@....... return true@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@....... public synchronized java.lang.Object java.util.Vector.get(int)@b@....... arg1: 2@b@....... return TEST2@b@....... public boolean java.util.Vector.contains(java.lang.Object)@b@....... arg1: TEST2@b@.............. public synchronized int java.util.Vector.indexOf(java.lang.Object,int)@b@.............. arg1: TEST2@b@.............. arg2: 0@b@.............. return 2@b@....... return true@b@....... public synchronized int java.util.Vector.size()@b@....... return 3@b@ return true@b@@b@ public synchronized int java.util.Vector.lastIndexOf(java.lang.Object)@b@ arg1: TEST@b@....... public synchronized int java.util.Vector.lastIndexOf(java.lang.Object,int)@b@....... arg1: TEST@b@....... arg2: 2@b@....... return -1@b@ return -1