可见,通过代理模式增加统一日志处理生效了,而且即便是给多个不同类的对象添加统一日志处理,写一个LogHandler就够了,不用为每个类额外写一个对应的代理类。
实现原理 System.setProperty("sun.misc.ProxyGenerator.saveGeneratedFiles", "true");上面代码中有这样一行,加上这个之后就能把运行时生成的代理class文件写到文件中(在项目根目录的com/sun/proxy下),关键奥秘就在于生成的这个class文件。
运行之后,在当前项目的根目录的com/sun/proxy下,会多出一个$Proxy0.class文件,反编译查看源代码(这里去除了equals()、toString()、hashCode()方法),如下:
package com.sun.proxy; import com.github.itwild.proxy.CommonService; 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 CommonService { private static Method m4; private static Method m3; public $Proxy0(InvocationHandler var1) { super(var1); } public final Object query(Long var1) { try { return (Object)super.h.invoke(this, m4, new Object[]{var1}); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } public final void delete(Long var1) { try { super.h.invoke(this, m3, new Object[]{var1}); } catch (RuntimeException | Error var3) { throw var3; } catch (Throwable var4) { throw new UndeclaredThrowableException(var4); } } static { try { m4 = Class.forName("com.github.itwild.proxy.CommonService").getMethod("query", Class.forName("java.lang.Long")); m3 = Class.forName("com.github.itwild.proxy.CommonService").getMethod("delete", Class.forName("java.lang.Long")); } catch (NoSuchMethodException var2) { throw new NoSuchMethodError(var2.getMessage()); } catch (ClassNotFoundException var3) { throw new NoClassDefFoundError(var3.getMessage()); } } }看到上面的代码,你有没有似曾相识的感觉,这不正是博客一开篇介绍的实现代理模式的第一种方式吗(代理类关联目标对象,实现目标对象实现的接口)。
我们再理一下生成的代理类的代码逻辑,$Proxy0继承了java.lang.reflect.Proxy,并实现了CommonService接口,对代理类的方法调用(比如说query())实际上都会转发到super.h对象的invoke()方法调用,再看下super.h到底是啥,追踪一下父类java.lang.reflect.Proxy可知
/** * the invocation handler for this proxy instance. */ protected InvocationHandler h;这正是快速入门中我们编写的LogHandler所实现的InvocationHandler接口。这样整个过程就理清了,这里通过super.h调用了我们前面编写的LogHandler中的处理逻辑。
那么,新的问题又来了,代理类是怎么生成的,我们没有写任何相关的代码,它是怎么知道我需要代理的方法以及方法参数等等。我们在创建代理对象的时候调用Proxy.newProxyInstance传入了代理类需要实现的接口
/** * Returns an instance of a proxy class for the specified interfaces * that dispatches method invocations to the specified invocation * handler. * * @param loader the class loader to define the proxy class * @param interfaces the list of interfaces for the proxy class * to implement * @param h the invocation handler to dispatch method invocations to */ @CallerSensitive public static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)至于一步步如何生成class的byte[]可先追踪java.lang.reflect.Proxy中的ProxyClassFactory相关代码
/** * A factory function that generates, defines and returns the proxy class given * the ClassLoader and array of interfaces. */ private static final class ProxyClassFactory implements BiFunction<ClassLoader, Class<?>[], 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<Class<?>, 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 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. */ 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(); String proxyName = proxyPkg + proxyClassNamePrefix + num; /* * Generate the specified proxy class. */ byte[] proxyClassFile = ProxyGenerator.generateProxyClass( proxyName, interfaces, accessFlags); try { return defineClass0(loader, proxyName, proxyClassFile, 0, proxyClassFile.length); } catch (ClassFormatError e) { throw new IllegalArgumentException(e.toString()); } } }