【迪士尼彩乐园】完整源码分享下载:http://hxforum.com/thread-254-1-2.html
参考相关文章文献:
ProxyFactory
ProxyFactory Hierarchy:
AbstractProxyFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public <T> T getProxy(Invoker<T> invoker) throws RpcException { Class<?>[] interfaces = null; // createProxy时创建invoker时已将interface传入 String config = invoker.getUrl().getParameter("interfaces"); if (config != null && config.length() > 0) { String[] types = Constants.COMMA_SPLIT_PATTERN.split(config); if (types != null && types.length > 0) { interfaces = new Class<?>[types.length + 2]; interfaces[0] = invoker.getInterface(); interfaces[1] = EchoService.class; for (int i = 0; i < types.length; i ++) { interfaces[i + 1] = ReflectUtils.forName(types[i]); } } } if (interfaces == null) { interfaces = new Class<?>[] {invoker.getInterface(), EchoService.class}; } // 调用子类的实现 return getProxy(invoker, interfaces); |
JavassistProxyFactory
1 2 3 |
public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) { return (T) Proxy.getProxy(interfaces).newInstance(new InvokerInvocationHandler(invoker)); } |
getProxy
的相关代码:
1 2 3 4 |
public static Proxy getProxy(Class<?>... ics) { return getProxy(ClassHelper.getCallerClassLoader(Proxy.class), ics); } |
动态类的实现:
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
public static Proxy getProxy(ClassLoader cl, Class<?>... ics) { //some ops... try { ccp = ClassGenerator.newInstance(cl); Set<String> worked = new HashSet<String>(); List<Method> methods = new ArrayList<Method>(); // 反射获取interface的相关信息并build code string,然后交给javassist动态生成实现类。 for(int i=0;i<ics.length;i++) { if( !Modifier.isPublic(ics[i].getModifiers()) ) { String npkg = ics[i].getPackage().getName(); if( pkg == null ) { pkg = npkg; } else { if( !pkg.equals(npkg) ) throw new IllegalArgumentException("non-public interfaces from different packages"); } } ccp.addInterface(ics[i]); for( Method method : ics[i].getMethods() ) { String desc = ReflectUtils.getDesc(method); if( worked.contains(desc) ) continue; worked.add(desc); int ix = methods.size(); Class<?> rt = method.getReturnType(); Class<?>[] pts = method.getParameterTypes(); StringBuilder code = new StringBuilder("Object[] args = new Object[").append(pts.length).append("];"); for(int j=0;j<pts.length;j++) code.append(" args[").append(j).append("] = ($w)$").append(j+1).append(";"); // 注意这里 handler.invoke(),代理的统一处理 code.append(" Object ret = handler.invoke(this, methods[" + ix + "], args);"); if( !Void.TYPE.equals(rt) ) code.append(" return ").append(asArgument(rt, "ret")).append(";"); methods.add(method); ccp.addMethod(method.getName(), method.getModifiers(), rt, pts, method.getExceptionTypes(), code.toString()); } } if( pkg == null ) pkg = PACKAGE_NAME; // 接口的实现类 String pcn = pkg + ".proxy" + id; ccp.setClassName(pcn); ccp.addField("public static java.lang.reflect.Method[] methods;"); ccp.addField("private " + InvocationHandler.class.getName() + " handler;"); ccp.addConstructor(Modifier.PUBLIC, new Class<?>[]{ InvocationHandler.class }, new Class<?>[0],"handler=$1;"); // $1等表示传入的参数,具体参考javassist官方文档 ccp.addDefaultConstructor(); Class<?> clazz = ccp.toClass(); clazz.getField("methods").set(null, methods.toArray(new Method[0])); // 生成当前Proxy的子类,实现newInstance()方法 String fcn = Proxy.class.getName() + id; ccm = ClassGenerator.newInstance(cl); ccm.setClassName(fcn); ccm.addDefaultConstructor(); ccm.setSuperClass(Proxy.class); ccm.addMethod("public Object newInstance(" + InvocationHandler.class.getName() + " h){ return new " + pcn + "($1); }"); Class<?> pc = ccm.toClass(); proxy = (Proxy)pc.newInstance(); } // some ops... return proxy; } |