java代理
2021-03-04 07:27
public static void main(String[] args) { IUserService target = new UserServiceImpl(); MyInvocationHandler handler = new MyInvocationHandler(target); IUserService proxyObject = (IUserService) Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(), target.getClass().getInterfaces(), handler); proxyObject.add("张玉龙"); }使用上非常简单、网上demo也很多,不做充分讲解,对jdk代理用法的小伙伴如果还不熟悉这块代码,就先了解一下jdk代理的使用方式,然后在回来继续看下面的源码分析
Proxy.newProxyInstance( ClassLoaderloader, Class[] interfaces, InvocationHandler h)
产生了代理对象,所以我们进到 newProxyInstance
的实现:
public static Object newProxyInstance(ClassLoader loader,
Class>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
Objects.requireNonNull(h);
final Class>[] intfs = interfaces.clone();
final SecurityManager sm = System.getSecurityManager();
if (sm != null) {
checkProxyAccess(Reflection.getCallerClass(), loader, intfs);
}
/*
* Look up or generate the designated proxy class.
*/
Class> cl = getProxyClass0(loader, intfs);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
if (sm != null) {
checkNewProxyPermission(Reflection.getCallerClass(), cl);
}
final Constructor> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doPrivileged(new PrivilegedAction() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
return cons.newInstance(new Object[]{h});
} catch (IllegalAccessException|InstantiationException e) {
throw new InternalError(e.toString(), e);
} catch (InvocationTargetException e) {
Throwable t = e.getCause();
if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else {
throw new InternalError(t.toString(), t);
}
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString(), e);
}
}
getProxyClass0(loader, intfs)
得到代理类的Class对象,然后通过Class对象得到构造方法,进而创建代理对象。下一步看 getProxyClass0
这个方法。//此方法也是Proxy类下的方法
private static Class> getProxyClass0(ClassLoader loader,
Class>... interfaces) {
if (interfaces.length > 65535) {
throw new IllegalArgumentException("interface limit exceeded");
}
// If the proxy class defined by the given loader implementing
// the given interfaces exists, this will simply return the cached copy;
// otherwise, it will create the proxy class via the ProxyClassFactory
//意思是:如果代理类被指定的类加载器loader定义了,并实现了给定的接口interfaces,
//那么就返回缓存的代理类对象,否则使用ProxyClassFactory创建代理类。
return proxyClassCache.get(loader, interfaces);
}
这里看到proxyClassCache,有Cache便知道是缓存的意思,正好呼应了前面Look up or generate the designated proxy class。查询(在缓存中已经有)或生成指定的代理类的class对象这段注释。
proxyClassCache
是个WeakCache类的对象,调用proxyClassCache.get(loader, interfaces); 可以得到缓存的代理类或创建代理类(没有缓存的情况)。说明WeakCache中有 get
这个方法。先看下WeakCache类的定义(这里先只给出变量的定义和构造函数):
//K代表key的类型,P代表参数的类型,V代表value的类型。
// WeakCache>[], Class>> proxyClassCache 说明proxyClassCache存的值是Class>对象,正是我们需要的代理类对象。
final class WeakCacheK, P, V> {
private final ReferenceQueue refQueue
= new ReferenceQueue();
// the key type is Object for supporting null key
private final ConcurrentMap
(key, sub-key) -> value
。其中key是传进来的Classloader进行包装后的对象,sub-key是由WeakCache构造函数传人的 KeyFactory()
生成的。value就是产生代理类的对象,是由WeakCache构造函数传人的 ProxyClassFactory()
生成的好,大体上说完WeakCache这个类的作用,我们回到刚才
proxyClassCache.get(loader, interfaces);
这句代码。get是WeakCache里的方法。源码如下//K和P就是WeakCache定义中的泛型,key是类加载器,parameter是接口类数组
public V get(K key, P parameter) {
//检查parameter不为空
Objects.requireNonNull(parameter);
//清除无效的缓存
expungeStaleEntries();
// cacheKey就是(key, sub-key) -> value里的一级key,
Object cacheKey = CacheKey.valueOf(key, refQueue);
// lazily install the 2nd level valuesMap for the particular cacheKey
//根据一级key得到 ConcurrentMap
所以接下来我们看Factory类中的get方法。
public synchronized V get() { // serialize access
// re-check
Supplier supplier = valuesMap.get(subKey);
//重新检查得到的supplier是不是当前对象
if (supplier != this) {
// something changed while we were waiting:
// might be that we were replaced by a CacheValue
// or were removed because of failure ->
// return null to signal WeakCache.get() to retry
// the loop
return null;
}
// else still us (supplier == this)
// create new value
V value = null;
try {
//代理类就是在这个位置调用valueFactory生成的
//valueFactory就是我们传入的 new ProxyClassFactory()
//一会我们分析ProxyClassFactory()的apply方法
value = Objects.requireNonNull(valueFactory.apply(key, parameter));
} finally {
if (value == null) { // remove us on failure
valuesMap.remove(subKey, this);
}
}
// the only path to reach here is with non-null value
assert value != null;
// wrap value with CacheValue (WeakReference)
//把value包装成弱引用
CacheValue cacheValue = new CacheValue(value);
// put into reverseMap
// reverseMap是用来实现缓存的有效性
reverseMap.put(cacheValue, Boolean.TRUE);
// try replacing us with CacheValue (this should always succeed)
if (!valuesMap.replace(subKey, this, cacheValue)) {
throw new AssertionError("Should not reach here");
}
// successfully replaced us with new CacheValue -> return the value
// wrapped by it
return value;
}
}
拨云见日,来到ProxyClassFactory的apply方法,代理类就是在这里生成的。
//这里的BiFunction是个函数式接口,可以理解为用T,U两种类型做参数,得到R类型的返回值
private static final class ProxyClassFactory
implements BiFunction[], Class>>
{
// prefix for all proxy class names
//所有代理类名字的前缀
private static final String proxyClassNamePrefix = "$Proxy";
// next number to use for generation of unique proxy class names
//用于生成代理类名字的计数器
private static final AtomicLong nextUniqueNumber = new AtomicLong();
@Override
public Class> apply(ClassLoader loader, Class>[] interfaces) {
Map, Boolean> interfaceSet = new IdentityHashMap(interfaces.length);
//验证代理接口,可不看
for (Class> intf : interfaces) {
/*
* Verify that the class loader resolves the name of this
* interface to the same Class object.
*/
Class> interfaceClass = null;
try {
interfaceClass = Class.forName(intf.getName(), false, loader);
} catch (ClassNotFoundException e) {
}
if (interfaceClass != intf) {
throw new IllegalArgumentException(
intf + " is not visible from class loader");
}
/*
* Verify that the Class object actually represents an
* interface.
*/
if (!interfaceClass.isInterface()) {
throw new IllegalArgumentException(
interfaceClass.getName() + " is not an interface");
}
/*
* Verify that this interface is not a duplicate.
*/
if (interfaceSet.put(interfaceClass, Boolean.TRUE) != null) {
throw new IllegalArgumentException(
"repeated interface: " + interfaceClass.getName());
}
}
//生成的代理类的包名
String proxyPkg = null; // package to define proxy class in
//代理类访问控制符: public ,final
int accessFlags = Modifier.PUBLIC | Modifier.FINAL;
/*
* Record the package of a non-public proxy interface so that the
* proxy class will be defined in the same package. Verify that
* all non-public proxy interfaces are in the same package.
*/
//验证所有非公共的接口在同一个包内;公共的就无需处理
//生成包名和类名的逻辑,包名默认是com.sun.proxy,类名默认是$Proxy 加上一个自增的整数值
//如果被代理类是 non-public proxy interface ,则用和被代理类接口一样的包名
for (Class> intf : interfaces) {
int flags = intf.getModifiers();
if (!Modifier.isPublic(flags)) {
accessFlags = Modifier.FINAL;
String name = intf.getName();
int n = name.lastIndexOf(‘.‘);
String pkg = ((n == -1) ? "" : name.substring(0, n + 1));
if (proxyPkg == null) {
proxyPkg = pkg;
} else if (!pkg.equals(proxyPkg)) {
throw new IllegalArgumentException(
"non-public interfaces from different packages");
}
}
}
if (proxyPkg == null) {
// if no non-public proxy interfaces, use com.sun.proxy package
proxyPkg = ReflectUtil.PROXY_PACKAGE + ".";
}
/*
* Choose a name for the proxy class to generate.
*/
long num = nextUniqueNumber.getAndIncrement();
//代理类的完全限定名,如com.sun.proxy.$Proxy0.calss
String proxyName = proxyPkg + proxyClassNamePrefix + num;
/*
* Generate the specified proxy class.
*/
//核心部分,生成代理类的字节码
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
//把代理类加载到JVM中,至此动态代理过程基本结束了
return defineClass0(loader, proxyName,
proxyClassFile, 0, proxyClassFile.length);
} catch (ClassFormatError e) {
/*
* A ClassFormatError here means that (barring bugs in the
* proxy class generation code) there was some other
* invalid aspect of the arguments supplied to the proxy
* class creation (such as virtual machine limitations
* exceeded).
*/
throw new IllegalArgumentException(e.toString());
}
}
}
到这里其实已经分析完了,但是本着深究的态度,决定看看JDK生成的动态代理字节码是什么,于是我们将字节码保存到磁盘上的class文件中。代码如下:
public static void main(String[] args) {
IUserService target = new UserServiceImpl();
MyInvocationHandler handler = new MyInvocationHandler(target);
//第一个参数是指定代理类的类加载器(我们传入当前测试类的类加载器)
//第二个参数是代理类需要实现的接口(我们传入被代理类实现的接口,这样生成的代理类和被代理类就实现了相同的接口)
//第三个参数是invocation handler,用来处理方法的调用。这里传入我们自己实现的handler
IUserService proxyObject = (IUserService) Proxy.newProxyInstance(DynamicProxyTest.class.getClassLoader(),
target.getClass().getInterfaces(), handler);
proxyObject.add("张玉龙");
String path = "D:/$Proxy0.class";
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy0", HelloworldImpl.class.getInterfaces());
FileOutputStream out = null;
try {
out = new FileOutputStream(path);
out.write(classFile);
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行这段代码,会在D盘生成一个名为$Proxy0.class的文件。通过反编译工具,得到JDK为我们生成的代理类是这样的:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
import com.zhb.jdk.proxy.IUserService;
import java.lang.reflect.*;
public final class $Proxy0 extends Proxy
implements IUserService
{
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
//代理类的构造函数,其参数正是是InvocationHandler实例,
//Proxy.newInstance方法就是通过通过这个构造函数来创建代理实例的
public $Proxy0(InvocationHandler invocationhandler)
{
super(invocationhandler);
}
// Object类中的三个方法,equals,toString, hashCode
public final boolean equals(Object obj)
{
try
{
return ((Boolean)super.h.invoke(this, m1, new Object[] {
obj
})).booleanValue();
}
catch (Error ) { }
catch (Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
public final String toString()
{
try
{
return (String)super.h.invoke(this, m2, null);
}
catch (Error ) { }
catch (Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
//接口代理方法
public final void add(String s)
{
try
{
// invocation handler的 invoke方法在这里被调用
super.h.invoke(this, m3, new Object[] {
s
});
return;
}
catch (Error ) { }
catch (Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
public final int hashCode()
{
try
{
// 在这里调用了invoke方法。
return ((Integer)super.h.invoke(this, m0, null)).intValue();
}
catch (Error ) { }
catch (Throwable throwable)
{
throw new UndeclaredThrowableException(throwable);
}
}
// 静态代码块对变量进行一些初始化工作
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("com.zhb.jdk.proxy.IUserService").getMethod("add", new Class[] {
Class.forName("java.lang.String")
});
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
}
catch (NoSuchMethodException nosuchmethodexception)
{
throw new NoSuchMethodError(nosuchmethodexception.getMessage());
}
catch (ClassNotFoundException classnotfoundexception)
{
throw new NoClassDefFoundError(classnotfoundexception.getMessage());
}
}
}
生成了Object类的三个方法:toString,hashCode,equals。还有我们需要被代理的方法。
JDK代理类的cache clear机制
大家都知道、在项目中被代理的class越来越多,所以jdk会搞一个cache的方式来防止相同的代理接口重复生成class,影响性能不说,实现也不是很优雅,那么现在就会有一个问题了,当classloader已经在内存中没有依赖的时候,被代理的proxy class其实也没有什么意义了,这样就需要清空无用的cache,java Proxy采用了非常巧妙的“弱引用机制”,我们来看下面的代码
我们还是继续看get方法的源码
public V get(K key, P parameter) {
Objects.requireNonNull(parameter);
expungeStaleEntries();
Object cacheKey = CacheKey.valueOf(key, refQueue);
// lazily install the 2nd level valuesMap for the particular cacheKey
ConcurrentMapObject, Supplier> valuesMap = map.get(cacheKey);
if (valuesMap == null) {
ConcurrentMapObject, Supplier> oldValuesMap
= map.putIfAbsent(cacheKey,
valuesMap = new ConcurrentHashMap());
if (oldValuesMap != null) {
valuesMap = oldValuesMap;
}
}
.......
}
其中源码中有一个方法expungeStaleEntries、我们进去这个方法一窥究竟
private void expungeStaleEntries() {
CacheKey cacheKey;
while ((cacheKey = (CacheKey)refQueue.poll()) != null) {
cacheKey.expungeFrom(map, reverseMap);
}
}
在看看expungeFrom方法源码干了些什么
void expungeFrom(ConcurrentMap, ? extends ConcurrentMap, ?>> map,
ConcurrentMap, Boolean> reverseMap) {
// removing just by key is always safe here because after a CacheKey
// is cleared and enqueue-ed it is only equal to itself
// (see equals method)...
ConcurrentMap, ?> valuesMap = map.remove(this);
// remove also from reverseMap if needed
if (valuesMap != null) {
for (Object cacheValue : valuesMap.values()) {
reverseMap.remove(cacheValue);
}
}
}
代码很清晰了,清空被代理的对象。现在的关键就是refQueue对象是怎么来的。我们继续找一下跟refQueue相关的源码、在get中还有一段代码是这样的
Object cacheKey = CacheKey.valueOf(key, refQueue);
private static final class CacheKeyK> extends WeakReferenceK> {
// a replacement for null keys
private static final Object NULL_KEY = new Object();
static Object valueOf(K key, ReferenceQueue refQueue) {
return key == null
// null key means we can‘t weakly reference it,
// so we use a NULL_KEY singleton as cache key
? NULL_KEY
// non-null key requires wrapping with a WeakReference
: new CacheKey(key, refQueue);
}
private final int hash;
private CacheKey(K key, ReferenceQueue refQueue) {
super(key, refQueue);
this.hash = System.identityHashCode(key); // compare by identity
}
.....
}
这样看就非常清晰了、原来是CacheKey继承了WeakReference弱引用机制,当弱引用依赖的key没有引用的时候,当前失效的对象就会进入ReferenceQueue中来实现清空cache的功能、这种实现思路和ThreadLocal的实现原理是一样的、大家有兴趣可以去阅读以下相关源码。
三:手把手写基于接口的java代理
上面我们分析了jdk动态代理源码、那我们是不是可以自己用自己的方式去写一个属于自己的jdk代理呢,答案是可以的
首先我们写一个基类,当然我并没有在基类里面写什么东西,只是模拟java中的proxy类而已,当然我们也可以丰富的去拓展一下这个类的方法,来实现更多的功能,读者可以通过读完这篇文章之后自己去考虑一下如何来拓展。
1 package meituan.zylproxy.handlder;
2 public class ZylProxy {
3 public ZylProxy(){
4 }
5 }
代理的核心接口,我们去做代理的时候一定是通过反射去调用的,不管jdk也好还是cglib也好,永远也无法脱离反射,我们照猫画虎,自己写一个代理接口核心类,这并不是什么难题,看起来和jdk的核心类接口也没有什么区别。
1 package meituan.zylproxy.handlder;
2
3 import java.lang.reflect.Method;
4
5 public interface ZYLInvocationHandler {
6
7 public Object invoke(Object proxy, Method method, Object[] args)
8 throws Exception;
9 }
说明一下 第一个参数proxy是代表代理类,而不是用户自己写的原生类实现。参数Method是接口的方法,args是运行时参数列表,在运行时传递过来的实际上就是实现类的参数,好了,下面让我们去深入核心。
我们自定义两个接口和接口的实现Idto,Idto2,和Dtoimpl如下:
1 package meituan.zylproxy.test.i;
2
3 public interface Idto {
4
5 public void add();
6
7 public String get();
8
9 }
package meituan.zylproxy.test.i;
public interface Idto2 {
public void adda();
public String geta();
}
package meituan.zylproxy.test.i.impl;
import meituan.zylproxy.test.i.Idto;
import meituan.zylproxy.test.i.Idto2;
public class DtoImpl implements Idto,Idto2{
@Override
public void add() {
System.out.println("add");
}
@Override
public String get() {
System.out.println("get");
return "return get";
}
@Override
public void adda() {
System.out.println("adda");
}
@Override
public String geta() {
System.out.println("geta");
return "return geta";
}
}
这是几个再简单不过的接口和实现类了,也没有什么可说的。接下来我们想对接口进行代理,无非是我们动态将接口进行实现,从而达到对使用者进行自定义handle接口暴露而已,下面看一下我们需要生成一个什么样的代理类。
import java.lang.reflect.Method;
import meituan.zylproxy.handlder.ZylProxy;
import meituan.zylproxy.handlder.ZYLInvocationHandler;
import meituan.zylproxy.test.i.Idto;
import meituan.zylproxy.test.i.Idto2;
public class IdtoPorxy extends ZylProxy implements Idto, Idto2 {
public ZYLInvocationHandler zYLInvocationHandler;
public static Method add1;
public static Method get2;
public static Method adda3;
public static Method geta4;
static {
try {
add1 = Class.forName ( "meituan.zylproxy.test.i.Idto" ).getMethod ( "add", new Class[0] );
get2 = Class.forName ( "meituan.zylproxy.test.i.Idto" ).getMethod ( "get", new Class[0] );
adda3 = Class.forName ( "meituan.zylproxy.test.i.Idto2" ).getMethod ( "adda", new Class[0] );
geta4 = Class.forName ( "meituan.zylproxy.test.i.Idto2" ).getMethod ( "geta", new Class[0] );
} catch (Exception e) {
}
}
public IdtoPorxy(ZYLInvocationHandler zYLInvocationHandler) {
this.zYLInvocationHandler = zYLInvocationHandler;
}
public void add() {
Object[] o = {};
try {
this.zYLInvocationHandler.invoke ( this, add1, o );
return;
} catch (Throwable e) {
e.printStackTrace ();
}
}
public java.lang.String get() {
Object[] o = {};
try {
return (java.lang.String) this.zYLInvocationHandler.invoke ( this, get2, o );
} catch (Exception e) {
e.printStackTrace ();
}
return null;
}
public void adda() {
Object[] o = {};
try {
this.zYLInvocationHandler.invoke ( this, adda3, o );
return;
} catch (Throwable e) {
e.printStackTrace ();
}
}
public java.lang.String geta() {
Object[] o = {};
try {
return (java.lang.String) this.zYLInvocationHandler.invoke ( this, geta4, o );
} catch (Exception e) {
e.printStackTrace ();
}
return null;
}
}
这个类不是由用户写的,而是我们动态生成的,对于jdk来说是生成了字节码,对cglib来说是通过字节码增强,其实实现的方式有多种,后面为了更方便大家理解我用字符串的形式来动态生成这么一个"家伙",先看看这个类干了些什么吧,也很简单。
public class IdtoPorxy extends ZylProxy implements Idto, Idto2
首先是继承了刚才我们所说的ZylProxy,留着今后拓展,可以参照java的Proxy,然后并且动态的实现了这两个接口。很简单
public ZYLInvocationHandler zYLInvocationHandler;
public IdtoPorxy(ZYLInvocationHandler zYLInvocationHandler) {
this.zYLInvocationHandler = zYLInvocationHandler;
}
这个是通过构造函数传进来一个handler对象,对实现类的操作都靠它了。
public static Method add1;
public static Method get2;
public static Method adda3;
public static Method geta4;
static {
try {
add1 = Class.forName ( "meituan.zylproxy.test.i.Idto" ).getMethod ( "add", new Class[0] );
get2 = Class.forName ( "meituan.zylproxy.test.i.Idto" ).getMethod ( "get", new Class[0] );
adda3 = Class.forName ( "meituan.zylproxy.test.i.Idto2" ).getMethod ( "adda", new Class[0] );
geta4 = Class.forName ( "meituan.zylproxy.test.i.Idto2" ).getMethod ( "geta", new Class[0] );
} catch (Exception e) {
}
}
枚举出来所有的接口的方法,通过class.forname来获取到Method元数据。备用
public void add() {
Object[] o = {};
try {
this.zYLInvocationHandler.invoke ( this, add1, o );
return;
} catch (Throwable e) {
e.printStackTrace ();
}
}
public java.lang.String get() {
Object[] o = {};
try {
return (java.lang.String) this.zYLInvocationHandler.invoke ( this, get2, o );
} catch (Exception e) {
e.printStackTrace ();
}
return null;
}
public void adda() {
Object[] o = {};
try {
this.zYLInvocationHandler.invoke ( this, adda3, o );
return;
} catch (Throwable e) {
e.printStackTrace ();
}
}
public java.lang.String geta() {
Object[] o = {};
try {
return (java.lang.String) this.zYLInvocationHandler.invoke ( this, geta4, o );
} catch (Exception e) {
e.printStackTrace ();
}
return null;
}
上面是要枚举出来所有的方法的实现,很简单都一个模样,把实现交给handler去做就可以了。至于怎么实现靠handler,我们动态生成的这个类只负责委托,不做任何事情。看到这里大家一定急不可待的想知道这个类怎么生成的了,我把我写的源码给大家贴出来看一下。
package meituan.zylproxy.util;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import meituan.zylproxy.test.i.Idto;
import meituan.zylproxy.test.i.Idto2;
public class ClassUtil {
public static String mackProxyClass(Class> c) throws Exception{
if(!c.isInterface()){
throw new Exception("代理的类必须是接口");
}
StringBuffer importsp = new StringBuffer();
importsp.append("import java.lang.reflect.Method;\n");
importsp.append("import meituan.zylproxy.handlder.ZylProxy;\n");
importsp.append("import meituan.zylproxy.handlder.ZYLInvocationHandler;\n");
importsp.append("import " +c.getName() + ";\n");
StringBuilder publicStaticMethods = new StringBuilder();
//public static Method add;
StringBuilder publicMethods = new StringBuilder();
publicMethods.append("public ZYLInvocationHandler zYLInvocationHandler;\n");
StringBuilder constructorsp = new StringBuilder();
String interFaceName = c.getName().substring(c.getName().lastIndexOf(".")+1);
constructorsp.append("public ").append("" + interFaceName + "Porxy").
append("(ZYLInvocationHandler zYLInvocationHandler) { "
+ "this.zYLInvocationHandler = zYLInvocationHandler;"
+ "}");
publicStaticMethods.append(" static { try { ");
StringBuilder classsp = new StringBuilder();
classsp.append("public class").append(" " + interFaceName + "Porxy").append(" extends ZylProxy implements ").append(interFaceName).append("{");
StringBuilder allMethods = new StringBuilder();
Method[] Methods = c.getMethods();
int curr=0;
for (Method m_:Methods) {
curr++;
publicMethods.append("public static Method ").append(m_.getName() + String.valueOf(curr)).append(";\n");
publicStaticMethods.append("").append(m_.getName() + String.valueOf(curr)).append("=");
publicStaticMethods.append("Class.forName(\"" + c.getName() + "\")" + ".getMethod(\""+ m_.getName() +"\", ");
StringBuilder sp =new StringBuilder();
StringBuilder spArgs = new StringBuilder();
spArgs.append("Object[] o ={");
//public
sp.append(Modifier.toString(m_.getModifiers()).replace("abstract", "")).append(" ");
//void | java.lang.String
sp.append(m_.getReturnType().getName()).append(" ");
//add()|get()
sp.append(m_.getName().concat("("));
StringBuilder methodCLass = new StringBuilder();
if(m_.getParameterTypes().length>0){
Class>[] claszz = m_.getParameterTypes();
int methodOffset = 0;
methodCLass.append("new Class[] { ");
for (Class> c_ : claszz) {
String paramStr = "obj" + String.valueOf(++methodOffset);
spArgs.append(paramStr.concat(","));
sp.append(c_.getName().toString().concat(" ").concat(paramStr)).append(",");
methodCLass.append("Class.forName(\"" + c_.getName()).append("\"),");
}
sp = new StringBuilder(sp.substring(0, sp.length()-1));
spArgs = new StringBuilder(spArgs.substring(0, spArgs.length()-1));
methodCLass = new StringBuilder(methodCLass.substring(0, methodCLass.length()-1));
}
if(methodCLass.length()>0){
methodCLass.append("}");
} else{
methodCLass.append("new Class[0]");
}
sp.append("){\n");
spArgs.append("}");
sp.append(spArgs+";\n");
if(sp.toString().contains("void")){
sp.append("try {\n this.zYLInvocationHandler.invoke(this,").append(m_.getName() + String.valueOf(curr)).append(",").append("o);\n return;\n");
sp.append("} catch (Throwable e) {e.printStackTrace();}}");
} else{
sp.append("try {return "
+ "("
+ m_.getReturnType().getName()
+ ")"
+ "this.zYLInvocationHandler.invoke(this,").append(m_.getName() + String.valueOf(curr)).append(",").append("o);\n");
sp.append("} catch (Exception e) {e.printStackTrace();} return null;");
}
publicStaticMethods.append(methodCLass).append(");\n");
allMethods.append(sp);
}
publicStaticMethods.append("} catch(Exception e){}}");
classsp.append(publicMethods)
.append(publicStaticMethods)
.append(constructorsp).append(allMethods).append("}");
classsp.append("}");
importsp.append(classsp);
return importsp.toString();
}
public static String mackMultiProxyClass(Class>[] cs) throws Exception{
StringBuffer importsp = new StringBuffer();
importsp.append("import java.lang.reflect.Method;\n");
importsp.append("import meituan.zylproxy.handlder.ZylProxy;\n");
importsp.append("import meituan.zylproxy.handlder.ZYLInvocationHandler;\n");
StringBuilder publicStaticMethods = new StringBuilder();
publicStaticMethods.append(" static { try { ");
//public static Method add;
StringBuilder publicMethods = new StringBuilder();
publicMethods.append("public ZYLInvocationHandler zYLInvocationHandler;\n");
int curr=0;
StringBuilder constructorsp = new StringBuilder();
String interFaceName = cs[0].getName().substring(cs[0].getName().lastIndexOf(".")+1);
constructorsp.append("public ").append("" + interFaceName + "Porxy").
append("(ZYLInvocationHandler zYLInvocationHa
下一篇:几种语言的比较(WIP)