今天稍微看了下JDK與CGLib動(dòng)態(tài)代理,對(duì)兩者的區(qū)別有所了解捌浩。
但是同樣是生成字節(jié)碼,在哪里會(huì)有所不同工秩,粗略的思考了一下尸饺,記錄如下(暫時(shí)記錄,以后有時(shí)間再細(xì)細(xì)研究)
JDK動(dòng)態(tài)代理
生成的字節(jié)碼示例如下:
//這里很清楚了,代理類繼承了Proxy類,并且實(shí)現(xiàn)了Proxy.newProxyInstance這個(gè)方法中傳入的接口
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反射調(diào)用目標(biāo)類方法:super.h.invoke(this, m3, new Object[]{var1});
Cglib動(dòng)態(tài)代理
生成的字節(jié)碼文件會(huì)有多個(gè)文件:
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文件(待續(xù))
參看 cglib原理分析
Jdk動(dòng)態(tài)代理的攔截對(duì)象是通過反射的機(jī)制來調(diào)用被攔截方法的浪听,反射的效率比較低,所以cglib采用了FastClass的機(jī)制來實(shí)現(xiàn)對(duì)被攔截方法的調(diào)用眉菱。FastClass機(jī)制就是對(duì)一個(gè)類的方法建立索引迹栓,通過索引來直接調(diào)用相應(yīng)的方法,