今天稍微看了下JDK与CGLib动态代理,对两者的区别有所了解。
但是同样是生成字节码,在哪里会有所不同,粗略的思考了一下,记录如下(暂时记录,以后有时间再细细研究)
JDK动态代理
生成的字节码示例如下:
//这里很清楚了,代理类继承了Proxy类,并且实现了Proxy.newProxyInstance这个方法中传入的接口
public final class $Proxy0 extends Proxy implements ITest {
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public $Proxy0(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return ((Boolean)super.h.invoke(this, m1, new Object[]{var1})).booleanValue();
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final void test(String var1) throws {
try {
super.h.invoke(this, m3, new Object[]{var1});
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final int hashCode() throws {
try {
return ((Integer)super.h.invoke(this, m0, (Object[])null)).intValue();
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[]{Class.forName("java.lang.Object")});
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m3 = Class.forName("ITest").getMethod("test", new Class[]{Class.forName("java.lang.String")});
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}
利用java反射调用目标类方法:super.h.invoke(this, m3, new Object[]{var1});
Cglib动态代理
生成的字节码文件会有多个文件:
public class Test$$EnhancerByCGLIB$$4e16a0e7 extends Test implements net.sf.cglib.proxy.Factory {
private boolean CGLIB$BOUND;
private static final java.lang.ThreadLocal CGLIB$THREAD_CALLBACKS;
private static final net.sf.cglib.proxy.Callback[] CGLIB$STATIC_CALLBACKS;
private net.sf.cglib.proxy.MethodInterceptor CGLIB$CALLBACK_0;
private static final java.lang.reflect.Method CGLIB$test$0$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$test$0$Proxy;
private static final java.lang.Object[] CGLIB$emptyArgs;
private static final java.lang.reflect.Method CGLIB$finalize$1$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$finalize$1$Proxy;
private static final java.lang.reflect.Method CGLIB$equals$2$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$equals$2$Proxy;
private static final java.lang.reflect.Method CGLIB$toString$3$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$toString$3$Proxy;
private static final java.lang.reflect.Method CGLIB$hashCode$4$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$hashCode$4$Proxy;
private static final java.lang.reflect.Method CGLIB$clone$5$Method;
private static final net.sf.cglib.proxy.MethodProxy CGLIB$clone$5$Proxy;
static void CGLIB$STATICHOOK1() { /* compiled code */ }
final void CGLIB$test$0(java.lang.String s) { /* compiled code */ }
public final void test(java.lang.String s) { /* compiled code */ }
final void CGLIB$finalize$1() throws java.lang.Throwable { /* compiled code */ }
protected final void finalize() throws java.lang.Throwable { /* compiled code */ }
final boolean CGLIB$equals$2(java.lang.Object o) { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
...
方法中没有具体的代码,看来得借助其他工具查看class文件(待续)
参看 cglib原理分析
Jdk动态代理的拦截对象是通过反射的机制来调用被拦截方法的,反射的效率比较低,所以cglib采用了FastClass的机制来实现对被拦截方法的调用。FastClass机制就是对一个类的方法建立索引,通过索引来直接调用相应的方法,