为什么使用动态代理
在一些场景,我们需要对我们的业务做一些统一的逻辑处理。比如说统一的日志记录,统一的异常处理,统一的权限控制管理。在这些业务场景使用动态代理就是一个很好的编程模式。统一的逻辑处理满足面向对象编程中的单一职责原则。同时也满足业务逻辑解耦实现高内聚低耦合。
动态代理的一个简单示例
在上面的业务场景中使用动态代理就是一个很好的实践场景。现在我们通过JDK动态代理的一个简单示例一步一步来探索JDK动态代理原理。JDK要使用动态代理目标类必须要实现一个接口。
目标接口
package com.demo.jdk;
public interface IDemo {
public String sayHello();
}
目标实现类
package com.demo.jdk;
public class Demo implements IDemo{
public String sayHello() {
System.out.println("hello");
return null;
}
}
代理类的调用处理器
package com.demo.jdk;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class JDKProxy implements InvocationHandler{
private Object targer;
public JDKProxy(final Object obj) {
super();
targer=obj;
}
public Object invoke(Object object, Method method, Object[] args) throws Throwable {
Object result=null;
System.out.println("befor");
method.invoke(targer, args);
System.out.println("after");
return result;
}
public Object getProxy() {
return Proxy.newProxyInstance(this.getClass().getClassLoader(), targer.getClass().getInterfaces(),this);
}
}
首先代理类调用处理类必须实现InvocationHandler接口,目标类必须通过一种方式传递给代理调用处理类。该示例通过构造方法将目标类传入到代理类中。然后重写InvocationHandler接口的invoke方法来添加我们的代理处理逻辑(比如说:添加权限管理逻辑。统一处理日志,统一异常处理)。该处理类同时也定义了获取代理对象的方法。该方法由Proxy类的newProxyInstance方法创建。JDK动态代理其核心逻辑就在该方法中。最后我们看下如何调用代理类。示例如下:
package com.demo.jdk;
public class Main {
public static void main(String[] args) {
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);
JDKProxy jdkProxy=new JDKProxy(new Demo());
IDemo demo=(IDemo)jdkProxy.getProxy();
demo.sayHello();
}
}
示例非常简单,首先创建一个代理处理对象并且通过构造方法将目标处理类传入其中。然后通过调用getProxy()方法创建代理类。最后调用代理对象方法。代理类的的每一个方法调用都会转化为invoke的调用。
运行结果如下:
befor
hello
after
要理解JDK动态代理原理就要搞清楚代理对象如何生成以及代理对象对任一方法调用如何转换成invoke方法调用。
显然为了搞清楚这个问题。我们只需要弄明白Proxy.newProxyInstance(ClassLoader loader, Class>[] interfaces,InvocationHandler h);实现逻辑即可找到答案。跟踪其源码如下:
@CallerSensitive
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)方法中。跟踪方法源码如下:
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
return proxyClassCache.get(loader, interfaces);
}
此处显然逻辑在proxyClassCache.get(loader, interfaces);中继续跟踪其源码如下:
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
ConcurrentMap
这部分逻辑可以看出前面是为了保证缓存对象不为空。后面的逻辑就是为了创建并且缓存subKey对象。此处重点应该关注Objects.requireNonNull(subKeyFactory.apply(key, parameter));跟踪源码如下:
@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
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) {
/*
* 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());
}
}
源码前部分可以看出是验证接口的一些信息。接口可见性,实现的是否是接口对象,接口是否重复实现。过后就是生成代理类的包名与类名信息。最后是生成代理类调用方法为:ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);再跟踪代码发现JDK中已经是非开放源代码。下面方法实现来自OPENJDK1.8。据说两者代码相识度很高。下面给出OPENJDK1.8该方法源码。
public static byte[] generateProxyClass(final String name,
Class>[] interfaces,
int accessFlags)
{
ProxyGenerator gen = new ProxyGenerator(name, interfaces, accessFlags);
final byte[] classFile = gen.generateClassFile();
if (saveGeneratedFiles) {
java.security.AccessController.doPrivileged(
new java.security.PrivilegedAction() {
public Void run() {
try {
int i = name.lastIndexOf('.');
Path path;
if (i > 0) {
Path dir = Paths.get(name.substring(0, i).replace('.', File.separatorChar));
Files.createDirectories(dir);
path = dir.resolve(name.substring(i+1, name.length()) + ".class");
} else {
path = Paths.get(name + ".class");
}
Files.write(path, classFile);
return null;
} catch (IOException e) {
throw new InternalError(
"I/O exception saving generated file: " + e);
}
}
});
}
return classFile;
}
生成代理类的方法generateClassFile()如下:
private byte[] generateClassFile() {
/* ============================================================
* Step 1: Assemble ProxyMethod objects for all methods to
* generate proxy dispatching code for.
*/
/*
* Record that proxy methods are needed for the hashCode, equals,
* and toString methods of java.lang.Object. This is done before
* the methods from the proxy interfaces so that the methods from
* java.lang.Object take precedence over duplicate methods in the
* proxy interfaces.
*/
addProxyMethod(hashCodeMethod, Object.class);
addProxyMethod(equalsMethod, Object.class);
addProxyMethod(toStringMethod, Object.class);
/*
* Now record all of the methods from the proxy interfaces, giving
* earlier interfaces precedence over later ones with duplicate
* methods.
*/
for (Class> intf : interfaces) {
for (Method m : intf.getMethods()) {
addProxyMethod(m, intf);
}
}
/*
* For each set of proxy methods with the same signature,
* verify that the methods' return types are compatible.
*/
for (List sigmethods : proxyMethods.values()) {
checkReturnTypes(sigmethods);
}
/* ============================================================
* Step 2: Assemble FieldInfo and MethodInfo structs for all of
* fields and methods in the class we are generating.
*/
try {
methods.add(generateConstructor());
for (List sigmethods : proxyMethods.values()) {
for (ProxyMethod pm : sigmethods) {
// add static field for method's Method object
fields.add(new FieldInfo(pm.methodFieldName,
"Ljava/lang/reflect/Method;",
ACC_PRIVATE | ACC_STATIC));
// generate code for proxy method and add it
methods.add(pm.generateMethod());
}
}
methods.add(generateStaticInitializer());
} catch (IOException e) {
throw new InternalError("unexpected I/O Exception", e);
}
if (methods.size() > 65535) {
throw new IllegalArgumentException("method limit exceeded");
}
if (fields.size() > 65535) {
throw new IllegalArgumentException("field limit exceeded");
}
/* ============================================================
* Step 3: Write the final class file.
*/
/*
* Make sure that constant pool indexes are reserved for the
* following items before starting to write the final class file.
*/
cp.getClass(dotToSlash(className));
cp.getClass(superclassName);
for (Class> intf: interfaces) {
cp.getClass(dotToSlash(intf.getName()));
}
/*
* Disallow new constant pool additions beyond this point, since
* we are about to write the final constant pool table.
*/
cp.setReadOnly();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
try {
/*
* Write all the items of the "ClassFile" structure.
* See JVMS section 4.1.
*/
// u4 magic;
dout.writeInt(0xCAFEBABE);
// u2 minor_version;
dout.writeShort(CLASSFILE_MINOR_VERSION);
// u2 major_version;
dout.writeShort(CLASSFILE_MAJOR_VERSION);
cp.write(dout); // (write constant pool)
// u2 access_flags;
dout.writeShort(accessFlags);
// u2 this_class;
dout.writeShort(cp.getClass(dotToSlash(className)));
// u2 super_class;
dout.writeShort(cp.getClass(superclassName));
// u2 interfaces_count;
dout.writeShort(interfaces.length);
// u2 interfaces[interfaces_count];
for (Class> intf : interfaces) {
dout.writeShort(cp.getClass(
dotToSlash(intf.getName())));
}
// u2 fields_count;
dout.writeShort(fields.size());
// field_info fields[fields_count];
for (FieldInfo f : fields) {
f.write(dout);
}
// u2 methods_count;
dout.writeShort(methods.size());
// method_info methods[methods_count];
for (MethodInfo m : methods) {
m.write(dout);
}
// u2 attributes_count;
dout.writeShort(0); // (no ClassFile attributes for proxy classes)
} catch (IOException e) {
throw new InternalError("unexpected I/O Exception", e);
}
return bout.toByteArray();
}
此处逻辑就是首先是添加了固定(toString,equals,hashcode)的几个代理方法。然后通过遍历接口方法添加代理方法。添加完成后将原来方法调用转换成为代理方法调用。接下来贴出示例中生成的代理类:
package com.sun.proxy;
import com.demo.jdk.IDemo;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
/* renamed from: com.sun.proxy.$Proxy0 */
public final class C$Proxy0 extends Proxy implements IDemo {
private static Method m0;
private static Method m1;
private static Method m2;
private static Method m3;
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[]{Class.forName("java.lang.Object")});
m3 = Class.forName("com.demo.jdk.IDemo").getMethod("sayHello", new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
} catch (Throwable e) {
throw new NoSuchMethodError(e.getMessage());
} catch (Throwable e2) {
throw new NoClassDefFoundError(e2.getMessage());
}
}
public C$Proxy0(InvocationHandler invocationHandler) {
super(invocationHandler);
}
public final boolean equals(Object obj) {
RuntimeException e;
try {
return ((Boolean) this.h.invoke(this, m1, new Object[]{obj})).booleanValue();
} catch (Error e2) {
e = e2;
} catch (RuntimeException e3) {
e = e3;
} catch (Throwable th) {
UndeclaredThrowableException undeclaredThrowableException = new UndeclaredThrowableException(th);
}
throw e;
}
public final int hashCode() {
RuntimeException e;
try {
return ((Integer) this.h.invoke(this, m0, null)).intValue();
} catch (Error e2) {
e = e2;
} catch (RuntimeException e3) {
e = e3;
} catch (Throwable th) {
UndeclaredThrowableException undeclaredThrowableException = new UndeclaredThrowableException(th);
}
throw e;
}
public final String sayHello() {
RuntimeException e;
try {
return (String) this.h.invoke(this, m3, null);
} catch (Error e2) {
e = e2;
throw e;
} catch (RuntimeException e3) {
e = e3;
throw e;
} catch (Throwable th) {
UndeclaredThrowableException undeclaredThrowableException = new UndeclaredThrowableException(th);
}
}
public final String toString() {
RuntimeException e;
try {
return (String) this.h.invoke(this, m2, null);
} catch (Error e2) {
e = e2;
throw e;
} catch (RuntimeException e3) {
e = e3;
throw e;
} catch (Throwable th) {
UndeclaredThrowableException undeclaredThrowableException = new UndeclaredThrowableException(th);
}
}
}
从代理类中可以看出所有的方法都被转换成invoke方法的调用了。现在再回到开始的newProxyInstance方法中
@CallerSensitive
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);
}
}
cl对象就是生成的代理类对象。接下来通过一个带InvocationHandler接口参数的实例来实化新代理类并且返回給调用者。到这里我们就理解了JDK动态代理实现逻辑。