在客戶類(即如下代碼的Client類),語句Manager managerProxy = java
(Manager) Proxy.newProxyInstance(managerImpl.getClass().getClassLoader(), managerImpl.getClass().getInterfaces(), securityHandler); ide
能夠看到,Proxy(庫類)中的newProxyInstance方法被調用,該方法返回一個被代理對象的實例,而後向上轉型爲其對應的接口。 this
問題是該方法在返回以前已經作了什麼?咱們看看其參數的第三個:securityHandler,它就是InvocationHandler接口實現類的一個實例:securityHandler。由於InvocationHandler接口中本來有個invoke的方法,因此其實現類固然須要實現這個方法,即其實現類(在此是BusinessHandler類)中有invoke方法,而若是invoke方法要被調用,只能經過BusinessHandler類對象來調用。 代理
而在下面的源代碼類$Proxy0 中,有三個地方已經用BusinessHandler類對象來調用invoke方法,他們分別在這些方法的代碼中:public final boolean equals(Object obj) ;public final int hashCode() ;public final String toString()。也許你會有疑問,這個方法也沒有看到在哪裏被 對象
$Proxy0 對象調用過,怎麼能執行invoke方法呢?可是請看$Proxy0中的static代碼塊,這個模塊是特殊的,由於當newProxyInstance建立$Proxy0 時,它就被初始化。而這個static模塊中的getMethod方法加載了這個三個方法,於是它們裏面的代碼(h.invoke())被執行。 繼承
另外咱們還能夠看到,invoke方法中的第一個參數表明什麼。在源代碼中咱們看到return ((Boolean) super.h.invoke(this, m1, new Object[] { obj })) .booleanValue(); 那麼this就是$Proxy0 對象。這裏須要注意的是,它並不是是被代理對象(ManagerImpl)。其第二個參數傳遞的是$Proxy0 類中數據域,其類型是Method,是被封裝過的被代理對象(ManagerImpl)的方法。 接口
小結:invoke方法的調用過程,就是先新建其類對象(實例),而後把它傳入newProxyInstance方法中,在裏面解析並用它來調用invoke方法。 get
一.相關類及其方法: 源碼
java.lang.reflect.Proxy類的newProxyInstance(),是用於建立動態代理類和實例的靜態方法.返回一個指定接口的代理類實例,該接口能夠將方法調用指派到指定的調用處理程序。
java.lang.reflect.InvocationHandler接口中的invoke(),在代理實例上處理方法調用並返回結果。當與方法關聯的代理實例上調用方法時,將在調用處理程序上調用此方法。 hash
二.探討的源代碼:
被代理對象的接口及實現類:
public interface Manager {
public void modify();
}
public class ManagerImpl implements Manager {
public void modify() {
System.out.println("*******modify()方法被調用");
}
}
動態代理類:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
public class BusinessHandler implements InvocationHandler {
private Object object = null;
public BusinessHandler(Object object) {
this.object = object;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
System.out.println("do something before method");
Object ret = method.invoke(this.object, args);
System.out.println("do something after method");
return ret;
}
}
客戶端類:
import java.lang.reflect.Proxy;
public class Client {
public static void main(String[] args) {
// 元對象(被代理對象)
ManagerImpl managerImpl = new ManagerImpl();
// 業務代理類
BusinessHandler securityHandler = new BusinessHandler(managerImpl);
// 得到代理類($Proxy0 extends Proxy implements Manager)的實例.
Manager managerProxy =
(Manager) Proxy.newProxyInstance(managerImpl.getClass().getClassLoader(), managerImpl.getClass()
.getInterfaces(), securityHandler);
managerProxy.modify();
}
}
三.執行結果:
do something before method
*******modify()方法被調用
do something after method
四.機制分析:
Proxy.newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)作了如下幾件事.
(1)根據參數loader和interfaces調用方法 getProxyClass(loader, interfaces)建立代理類$Proxy.$Proxy0類 實現了interfaces的接口,並繼承了Proxy類.
(2)實例化$Proxy0並在構造方法中把BusinessHandler傳過去,接着$Proxy0調用父類Proxy的構造器,爲h賦值,以下:
class Proxy{
InvocationHandler h=null;
protected Proxy(InvocationHandler h) {
this.h = h;
}
...
}
下面是本例的$Proxy0類的源碼(好不容易纔把它提出來):
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class $Proxy0 extends Proxy implements Manager {
private static Method m1;
private static Method m0;
private static Method m3;
private static Method m2;
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals",
new Class[] { Class.forName("java.lang.Object") });
m0 = Class.forName("java.lang.Object").getMethod("hashCode",
new Class[0]);
m3 = Class.forName("com.ml.test.Manager").getMethod("modify",
new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString",
new Class[0]);
} catch (NoSuchMethodException nosuchmethodexception) {
throw new NoSuchMethodError(nosuchmethodexception.getMessage());
} catch (ClassNotFoundException classnotfoundexception) {
throw new NoClassDefFoundError(classnotfoundexception.getMessage());
}
} //static
public $Proxy0(InvocationHandler invocationhandler) {
super(invocationhandler);
}
@Override
public final boolean equals(Object obj) {
try {
return ((Boolean) super.h.invoke(this, m1, new Object[] { obj })) .booleanValue();
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
@Override
public final int hashCode() {
try {
return ((Integer) super.h.invoke(this, m0, null)).intValue();
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
public final void modify() {
try {
super.h.invoke(this, m3, null);
return;
} catch (Error e) {
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
@Override
public final String toString() {
try {
return (String) super.h.invoke(this, m2, null);
} catch (Throwable throwable) {
throw new UndeclaredThrowableException(throwable);
}
}
}
接着把獲得的$Proxy0實例強制轉換成Manager.當執行managerProxy.modify()方法時(managerProxy是Manager的一個已經向上轉型的實例),就調用了$Proxy0類中的modify()方法.
在modify方法中,調用父類Proxy中的h的invoke()方法.即InvocationHandler.invoke();