被代理类
public class Human {
void doThings() {
System.out.println("Human");
}
}
代理类
public class CglibProxyHuman implements MethodInterceptor {
public Object getProxyInstance(Object target) {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setCallback(this); //将自己传给增强类,调用this.intercept
return enhancer.create();
}
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before target method...");
Object result = methodProxy.invokeSuper(o, objects);
System.out.println("after target method...");
return result;
}
}
测试代码
public static void main(String[] args) {
//调试查看动态代理生成文件
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "f:\\cglib");
Human human= new Human();
Human humanProxy=(Human)new CglibProxyHuman().getProxyInstance(human);
humanProxy.doThings();
}
查看动态代理生成文件,存储在 f:\\cglib
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "f:\\cglib");
增强类
humanProxy对象对应的动态生成的增强类 Human$ $EnhancerByCGLIB$$b7eedfef
import java.lang.reflect.Method;
import net.sf.cglib.core.ReflectUtils;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Factory;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
public class Human$$EnhancerByCGLIB$$b7eedfef extends Human implements Factory {
private boolean CGLIB$BOUND;
private static final ThreadLocal CGLIB$THREAD_CALLBACKS;
private static final Callback[] CGLIB$STATIC_CALLBACKS;
private MethodInterceptor CGLIB$CALLBACK_0;
private static final Method CGLIB$doThings$0$Method;
private static final MethodProxy CGLIB$doThings$0$Proxy;
private static final Object[] CGLIB$emptyArgs;
private static final Method CGLIB$finalize$1$Method;
private static final MethodProxy CGLIB$finalize$1$Proxy;
private static final Method CGLIB$equals$2$Method;
private static final MethodProxy CGLIB$equals$2$Proxy;
private static final Method CGLIB$toString$3$Method;
private static final MethodProxy CGLIB$toString$3$Proxy;
private static final Method CGLIB$hashCode$4$Method;
private static final MethodProxy CGLIB$hashCode$4$Proxy;
private static final Method CGLIB$clone$5$Method;
private static final MethodProxy CGLIB$clone$5$Proxy;
static void CGLIB$STATICHOOK1() {
CGLIB$THREAD_CALLBACKS = new ThreadLocal();
CGLIB$emptyArgs = new Object[0];
Class var0 = Class.forName("cn.suning.cglib.Human$$EnhancerByCGLIB$$b7eedfef");
Class var1;
Method[] var10000 = ReflectUtils.findMethods(new String[]{"finalize", "()V", "equals", "(Ljava/lang/Object;)Z", "toString", "()Ljava/lang/String;", "hashCode", "()I", "clone", "()Ljava/lang/Object;"}, (var1 = Class.forName("java.lang.Object")).getDeclaredMethods());
CGLIB$finalize$1$Method = var10000[0];
CGLIB$finalize$1$Proxy = MethodProxy.create(var1, var0, "()V", "finalize", "CGLIB$finalize$1");
CGLIB$equals$2$Method = var10000[1];
CGLIB$equals$2$Proxy = MethodProxy.create(var1, var0, "(Ljava/lang/Object;)Z", "equals", "CGLIB$equals$2");
CGLIB$toString$3$Method = var10000[2];
CGLIB$toString$3$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/String;", "toString", "CGLIB$toString$3");
CGLIB$hashCode$4$Method = var10000[3];
CGLIB$hashCode$4$Proxy = MethodProxy.create(var1, var0, "()I", "hashCode", "CGLIB$hashCode$4");
CGLIB$clone$5$Method = var10000[4];
CGLIB$clone$5$Proxy = MethodProxy.create(var1, var0, "()Ljava/lang/Object;", "clone", "CGLIB$clone$5");
CGLIB$doThings$0$Method = ReflectUtils.findMethods(new String[]{"doThings", "()V"}, (var1 = Class.forName("cn.suning.cglib.Human")).getDeclaredMethods())[0];
CGLIB$doThings$0$Proxy = MethodProxy.create(var1, var0, "()V", "doThings", "CGLIB$doThings$0");
}
final void CGLIB$doThings$0() {
super.doThings();
}
/*
* humanProxy.doThings()
*/
final void doThings() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
// CglibProxyHuman 类中 intercept
var10000.intercept(this, CGLIB$doThings$0$Method, CGLIB$emptyArgs, CGLIB$doThings$0$Proxy);
} else {
super.doThings();
}
}
....................省略部分无关代码............................
public Human$$EnhancerByCGLIB$$b7eedfef() {
CGLIB$BIND_CALLBACKS(this);
}
public static void CGLIB$SET_THREAD_CALLBACKS(Callback[] var0) {
CGLIB$THREAD_CALLBACKS.set(var0);
}
public static void CGLIB$SET_STATIC_CALLBACKS(Callback[] var0) {
CGLIB$STATIC_CALLBACKS = var0;
}
private static final void CGLIB$BIND_CALLBACKS(Object var0) {
Human$$EnhancerByCGLIB$$b7eedfef var1 = (Human$$EnhancerByCGLIB$$b7eedfef)var0;
if (!var1.CGLIB$BOUND) {
var1.CGLIB$BOUND = true;
Object var10000 = CGLIB$THREAD_CALLBACKS.get();
if (var10000 == null) {
var10000 = CGLIB$STATIC_CALLBACKS;
if (var10000 == null) {
return;
}
}
var1.CGLIB$CALLBACK_0 = (MethodInterceptor)((Callback[])var10000)[0];
}
}
public Object newInstance(Callback[] var1) {
CGLIB$SET_THREAD_CALLBACKS(var1);
Human$$EnhancerByCGLIB$$b7eedfef var10000 = new Human$$EnhancerByCGLIB$$b7eedfef();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Callback var1) {
CGLIB$SET_THREAD_CALLBACKS(new Callback[]{var1});
Human$$EnhancerByCGLIB$$b7eedfef var10000 = new Human$$EnhancerByCGLIB$$b7eedfef();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
}
public Object newInstance(Class[] var1, Object[] var2, Callback[] var3) {
CGLIB$SET_THREAD_CALLBACKS(var3);
Human$$EnhancerByCGLIB$$b7eedfef var10000 = new Human$$EnhancerByCGLIB$$b7eedfef;
switch(var1.length) {
case 0:
var10000.();
CGLIB$SET_THREAD_CALLBACKS((Callback[])null);
return var10000;
default:
throw new IllegalArgumentException("Constructor not found");
}
}
public Callback getCallback(int var1) {
CGLIB$BIND_CALLBACKS(this);
MethodInterceptor var10000;
switch(var1) {
case 0:
var10000 = this.CGLIB$CALLBACK_0;
break;
default:
var10000 = null;
}
return var10000;
}
public void setCallback(int var1, Callback var2) {
switch(var1) {
case 0:
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var2;
default:
}
}
public Callback[] getCallbacks() {
CGLIB$BIND_CALLBACKS(this);
return new Callback[]{this.CGLIB$CALLBACK_0};
}
public void setCallbacks(Callback[] var1) {
this.CGLIB$CALLBACK_0 = (MethodInterceptor)var1[0];
}
static {
CGLIB$STATICHOOK1();
}
}
humanProxy.doThings() 相当于调用 Human$ $EnhancerByCGLIB$$b7eedfef 类中的doThings方法
final void doThings() {
MethodInterceptor var10000 = this.CGLIB$CALLBACK_0;
if (var10000 == null) {
CGLIB$BIND_CALLBACKS(this);
var10000 = this.CGLIB$CALLBACK_0;
}
if (var10000 != null) {
// CglibProxyHuman 类中 intercept
var10000.intercept(this, CGLIB$doThings$0$Method, CGLIB$emptyArgs, CGLIB$doThings$0$Proxy);
} else {
super.doThings();
}
}
然后 var10000.intercept 相当于调用 CglibProxyHuman 类中 intercept 方法
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
System.out.println("before target method...");
Object result = methodProxy.invokeSuper(o, objects);
System.out.println("after target method...");
return result;
}
然后执行methodProxy.invokeSuper(o, objects)
public Object invokeSuper(Object obj, Object[] args) throws Throwable {
try {
this.init();
MethodProxy.FastClassInfo fci = this.fastClassInfo;
return fci.f2.invoke(fci.i2, obj, args);
} catch (InvocationTargetException var4) {
throw var4.getTargetException();
}
}
fci.f2 的类型是动态代理类 Human$ $EnhancerByCGLIB$ $1a9e6f49$ $FastClassByCGLIB$$662b6cec 关键代码如下
import java.lang.reflect.InvocationTargetException;
import net.sf.cglib.core.Signature;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.reflect.FastClass;
public class Human$$EnhancerByCGLIB$$b7eedfef$$FastClassByCGLIB$$662b6cec extends FastClass {
public Human$$EnhancerByCGLIB$$b7eedfef$$FastClassByCGLIB$$662b6cec(Class var1) {
super(var1);
}
............此处省略部分代码.................
public int getIndex(Class[] var1) {
switch(var1.length) {
case 0:
return 0;
default:
return -1;
}
}
public Object invoke(int var1, Object var2, Object[] var3) throws InvocationTargetException {
1a9e6f49 var10000 = (1a9e6f49)var2;
int var10001 = var1;
try {
switch(var10001) {
case 0:
return new Boolean(var10000.equals(var3[0]));
case 1:
return var10000.toString();
case 2:
return new Integer(var10000.hashCode());
case 3:
return var10000.newInstance((Class[])var3[0], (Object[])var3[1], (Callback[])var3[2]);
case 4:
return var10000.newInstance((Callback)var3[0]);
case 5:
return var10000.newInstance((Callback[])var3[0]);
case 6:
var10000.doThings();
return null;
case 7:
var10000.setCallback(((Number)var3[0]).intValue(), (Callback)var3[1]);
return null;
case 8:
var10000.setCallbacks((Callback[])var3[0]);
return null;
case 9:
return var10000.getCallback(((Number)var3[0]).intValue());
case 10:
return var10000.getCallbacks();
case 11:
1a9e6f49.CGLIB$SET_STATIC_CALLBACKS((Callback[])var3[0]);
return null;
case 12:
1a9e6f49.CGLIB$SET_THREAD_CALLBACKS((Callback[])var3[0]);
return null;
case 13:
return 1a9e6f49.CGLIB$findMethodProxy((Signature)var3[0]);
case 14:
1a9e6f49.CGLIB$STATICHOOK1();
return null;
case 15:
var10000.CGLIB$finalize$1();
return null;
case 16:
return var10000.CGLIB$toString$3();
case 17:
return new Boolean(var10000.CGLIB$equals$2(var3[0]));
case 18:
return new Integer(var10000.CGLIB$hashCode$4());
case 19:
var10000.CGLIB$doThings$0();
return null;
case 20:
return var10000.CGLIB$clone$5();
case 21:
var10000.wait();
return null;
case 22:
var10000.wait(((Number)var3[0]).longValue(), ((Number)var3[1]).intValue());
return null;
case 23:
var10000.wait(((Number)var3[0]).longValue());
return null;
case 24:
return var10000.getClass();
case 25:
var10000.notify();
return null;
case 26:
var10000.notifyAll();
return null;
}
} catch (Throwable var4) {
throw new InvocationTargetException(var4);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public Object newInstance(int var1, Object[] var2) throws InvocationTargetException {
1a9e6f49 var10000 = new 1a9e6f49;
1a9e6f49 var10001 = var10000;
int var10002 = var1;
try {
switch(var10002) {
case 0:
var10001.();
return var10000;
}
} catch (Throwable var3) {
throw new InvocationTargetException(var3);
}
throw new IllegalArgumentException("Cannot find matching method/constructor");
}
public int getMaxIndex() {
return 26;
}
}
fci.f2.invoke(fci.i2, obj, args) 其实调用是 Human$ $EnhancerByCGLIB$ $b7eedfef$ $FastClassByCGLIB$$662b6cec 类的 invoke 的方法,然后 fci.i2 的值是19
case 19:
var10000.CGLIB$doThings$0();
return null;
然后调用 var10000.CGLIB$doThings$0 方法,var10000 的类型是 Human$ $EnhancerByCGLIB$$b7eedfef 类,相当于又调用Human$ $EnhancerByCGLIB$$b7eedfef 类中的 CGLIB$doThings$0 方法
final void CGLIB$doThings$0() {
super.doThings();
}
super 是什么,super 是 Human 类
public class Human {
void doThings() {
System.out.println("Human");
}
}
这样就调用 Human 类中的 doThings 方法