直接上圖
public class JavassistProxyFactory extends AbstractProxyFactory {
@SuppressWarnings("unchecked")
public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker));
}
public <T> Invoker<T> getInvoker(T proxy, Class<T> type, URL url) {
// TODO Wrapper類(lèi)不能正確處理帶$的類(lèi)名
final Wrapper wrapper = Wrapper.getWrapper(proxy.getClass().getName().indexOf('$') < 0 ? proxy.getClass() : type);
return new AbstractProxyInvoker<T>(proxy, type, url) {
@Override
protected Object doInvoke(T proxy, String methodName,
Class<?>[] parameterTypes,
Object[] arguments) throws Throwable {
return wrapper.invokeMethod(proxy, methodName, parameterTypes, arguments);
}
};
}
}
Dubbo在創(chuàng)建Invoker的時(shí)候先將ref實(shí)現(xiàn)類(lèi)包裝成了一個(gè)Wrapper,然后Invoker被調(diào)用的時(shí)候會(huì)觸發(fā)doInvoke()方法,然后調(diào)用Wrapper的invokeMethod()方法旋恼。由于Wrapper是一個(gè)抽象類(lèi),故Wrapper.getWrapper()被調(diào)用的時(shí)候肯定是利用了字節(jié)碼增強(qiáng)的技術(shù)為Wrapper創(chuàng)建了一個(gè)實(shí)現(xiàn)類(lèi)。
查看實(shí)現(xiàn)類(lèi)代碼
由于Wrapper子類(lèi)生成是利用javassist技術(shù)來(lái)實(shí)現(xiàn)的,所以直接將字節(jié)碼輸出到文件Wrapper20.java(最好使用IDEA自帶的反編譯插件,本人jd顯示有問(wèn)題).
package com.xx.xx.samples.loader;
import com.alibaba.dubbo.common.bytecode.NoSuchPropertyException;
import com.xx.xx.samples.loader.ClassGenerator2.DC;
import com.xx.xx.samples.loader.service.impl.UserServiceImpl;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
public class Wrapper20 extends Wrapper2 implements DC {
public static String[] pns;
public static Map pts;
public static String[] mns;
public static String[] dmns;
public static Class[] mts0;
public static Class[] mts1;
public static Class[] mts2;
public String[] getPropertyNames() {
return pns;
}
public boolean hasProperty(String var1) {
return pts.containsKey(var1);
}
public Class getPropertyType(String var1) {
return (Class)pts.get(var1);
}
public String[] getMethodNames() {
return mns;
}
public String[] getDeclaredMethodNames() {
return dmns;
}
public void setPropertyValue(Object var1, String var2, Object var3) {
try {
UserServiceImpl var4 = (UserServiceImpl)var1;
} catch (Throwable var6) {
throw new IllegalArgumentException(var6);
}
throw new NoSuchPropertyException("Not found property \"" + var2 + "\" filed or setter method in class com.xx.xx.samples.loader.service.impl.UserServiceImpl.");
}
public Object getPropertyValue(Object var1, String var2) {
try {
UserServiceImpl var3 = (UserServiceImpl)var1;
} catch (Throwable var5) {
throw new IllegalArgumentException(var5);
}
throw new NoSuchPropertyException("Not found property \"" + var2 + "\" filed or setter method in class com.xx.xx.samples.loader.service.impl.UserServiceImpl.");
}
public Object invokeMethod(Object var1, String var2, Class[] var3, Object[] var4) throws InvocationTargetException {
UserServiceImpl var5;
try {
var5 = (UserServiceImpl)var1;
} catch (Throwable var8) {
throw new IllegalArgumentException(var8);
}
try {
if ("findUserList".equals(var2) && var3.length == 0) {
return var5.findUserList();
}
if ("findById".equals(var2) && var3.length == 1) {
return var5.findById((Integer)var4[0]);
}
if ("updateById".equals(var2) && var3.length == 1) {
var5.updateById((Integer)var4[0]);
return null;
}
} catch (Throwable var9) {
throw new InvocationTargetException(var9);
}
throw new NoSuchMethodException("Not found method \"" + var2 + "\" in class com.xx.xx.samples.loader.service.impl.UserServiceImpl.");
}
public Wrapper20() {
}
}
可知invokeMethod只是做了一次轉(zhuǎn)發(fā),深層次的作用等待挖掘镀娶。。举娩。