1、简介
由于最近在学习框架,发现用到了动态编译、动态的生成二进制class字节码。
在框架中用到了Javassist工具,然后就联系到了JDK动态代理和Cglib动态代理。 JDK动态代理和Cglib动态代理,想必大家都已经很熟了,因为在我们开发中用的最频繁的框架Spring中,用到了这两种代理方式,而且是作为SpringAop中的核心而存在的。
后续我会把Cglib和Javassist的学习思路也整理出来,现在先让我们根据源码学习一下,Jdk动态代理的原理吧。
相比于静态代理,动态代理避免了开发人员编写各个繁锁的静态代理类,只需简单地指定一组接口及目标类对象就能动态的获得代理对象。
代理模式
使用代理模式必须要让代理类和目标类实现相同的接口,客户端通过代理类来调用目标方法,代理类会将所有的方法调用分派到目标对象上反射执行,还可以在分派过程中添加”前置通知”和后置处理(如在调用目标方法前校验权限,在调用完目标方法后打印日志等)等功能。
使用动态代理的五大步骤
1.通过实现InvocationHandler接口来自定义自己的InvocationHandler;
2.通过Proxy.getProxyClass获得动态代理类
3.通过反射机制获得代理类的构造方法,方法签名为getConstructor(InvocationHandler.class)
4.通过构造函数获得代理对象并将自定义的InvocationHandler实例对象传为参数传入
5.通过代理对象调用目标方法
下面的这段代码是我写的一个JDK动态代理的例子,由于代码粘贴方便,就把所有的类写在了同一个文件中,没有规范出来,忘谅解。
下面来看代码吧:
2、代码中涉及到的类
1、Person(人)类为一个接口类,因为JDK动态代理和Cglib不同,需要继承一个接口。
2、Programer(程序猿) 作为Person的一个子类存在,实现了Person中的方法。(此处Programer作为被代理对象存在)。
3、 MyInvocationHandler 类 ,实现动态代理调后提供给代理类调用。
3、调用过程
这是代码调用:
Person person = new Programer();
MyInvocationHandler invocation = new MyInvocationHandler(person);
Person proxyPerson = (Person) invocation.getProxy();
proxyPerson.wear()
4、代码的调用结果
------------------before 洗澡找衣服------------------
穿衣服
-------------------after 去玩,去逛街------------------
5、调用中的核心部分
public Object getProxy() {
return Proxy.newProxyInstance(this.getClass().getClassLoader(),
target.getClass().getInterfaces(), this);
}
newProxyInstance方法最终将返回一个实现了指定接口的类的实例,其三个参数分别是:ClassLoader,指定的接口及我们自己定义的InvocationHandler类
Proxy是整个JDK代理的核心,下面我们看一下源码中怎么通过Proxy对象调用newProxyInstance方法来创建代理对象的:
源码部分 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<Void>() {
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);
}
}
生成代理类部分
/*
* Look up or generate the designated proxy class.
* 上面的是一些初始化,明显不是重点,这一句写着:查找或生成指定的代理类。
* 明显这是我们需要的
*/
Class<?> cl = getProxyClass0(loader, intfs);
然后进入 getProxyClass0 方法中
/**
* Generate a proxy class. Must call the checkProxyAccess method
* to perform permission checks before calling this.
*/
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);
}
这里用到了缓存,先从缓存里查一下,如果存在,直接返回,不存在就新创建。在这个get方法里,我们看到了如下代码:
Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
此处提到了apply(),是Proxy类的内部类ProxyClassFactory实现其接口的一个方法,具体实现如下:
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<Object, Supplier<V>> valuesMap = map.get(cacheKey);
if (valuesMap == null) {
ConcurrentMap<Object, Supplier<V>> oldValuesMap
= map.putIfAbsent(cacheKey,
valuesMap = new ConcurrentHashMap<>());
if (oldValuesMap != null) {
valuesMap = oldValuesMap;
}
}
// create subKey and retrieve the possible Supplier<V> stored by that
// subKey from valuesMap 此处为生成实现接口的的代理对象
Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
Supplier<V> supplier = valuesMap.get(subKey);
Factory factory = null;
while (true) {
if (supplier != null) {
// supplier might be a Factory or a CacheValue<V> instance
V value = supplier.get();
if (value != null) {
return value;
}
}
// else no supplier in cache
// or a supplier that returned null (could be a cleared CacheValue
// or a Factory that wasn't successful in installing the CacheValue)
// lazily construct a Factory
if (factory == null) {
factory = new Factory(key, parameter, subKey, valuesMap);
}
if (supplier == null) {
supplier = valuesMap.putIfAbsent(subKey, factory);
if (supplier == null) {
// successfully installed Factory
supplier = factory;
}
// else retry with winning supplier
} else {
if (valuesMap.replace(subKey, supplier, factory)) {
// successfully replaced
// cleared CacheEntry / unsuccessful Factory
// with our Factory
supplier = factory;
} else {
// retry with current supplier
supplier = valuesMap.get(subKey);
}
}
}
}
// create subKey and retrieve the possible Supplier<V> stored by that
// subKey from valuesMap 此处为生成实现接口的的代理对象
Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
@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.
* 看此处应该就是最核心的部分了,因为这个地方根据上面生成的代理名称来生成具体的代理对象的字
* 节码文件。而这就是代理对象的.class二进制文件
*/
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces, accessFlags);
try {
// 此处是根据二进制文件生成 Class ,返回给上层。
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());
}
}
}
最后生成了接口的代理类对象
我们可以写一段代码来看一下源码中生成了的二进制字节码是什么样子的我们首先看一下生成代理类的方法:
/**
* 把代理类的字节码写到硬盘上
* @param path 保存路径
*/
public static void writeProxyClassToHardDisk(String path) {
// 第一种方法,这种方式在刚才分析ProxyGenerator时已经知道了
// System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);
// 第二种方法
// 获取代理类的字节码
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy11", Programer.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();
}
}
}
代理对象通过反编译工具查看:
import com.jdk.Person;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
public final class $Proxy11 extends Proxy
implements Person
{
private static Method m1;
private static Method m2;
private static Method m3;
private static Method m0;
public $Proxy11(InvocationHandler paramInvocationHandler)
throws
{
super(paramInvocationHandler);
}
public final boolean equals(Object paramObject)
throws
{
try
{
return ((Boolean)this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final String toString()
throws
{
try
{
return (String)this.h.invoke(this, m2, null);
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final void wear()
throws
{
try
{
this.h.invoke(this, m3, null);
return;
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
public final int hashCode()
throws
{
try
{
return ((Integer)this.h.invoke(this, m0, null)).intValue();
}
catch (RuntimeException localRuntimeException)
{
throw localRuntimeException;
}
catch (Throwable localThrowable)
{
}
throw new UndeclaredThrowableException(localThrowable);
}
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.jdk.Person").getMethod("wear", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
return;
}
catch (NoSuchMethodException localNoSuchMethodException)
{
throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
}
catch (ClassNotFoundException localClassNotFoundException)
{
}
throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
}
}
这段代码是将生成的代理类通过反射拿到构造函数,通过构造函数为将InvocationHandler 对象赋值到并创建代理对象。所以调用代理对象中得wear()穿衣服 方法时才会调用 InvocationHandler 中得 public Object invoke(Object proxy, Method method, Object[] args) 方法。
/*
* 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<Void>() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
return cons.newInstance(new Object[]{h});
整体的代码例子:
package com.jdk;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import sun.misc.ProxyGenerator;
public class MyInvocationHandler implements InvocationHandler {
// 目标对象
private Object target;
/**
* 构造方法
* @param target 目标对象
*/
public MyInvocationHandler(Object target) {
super();
this.target = target;
}
/**
* 执行目标对象的方法
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 在目标对象的方法执行之前简单的打印一下
System.out.println("------------------before 洗澡找衣服------------------");
// 执行目标对象的方法
Object result = method.invoke(target, args);
// 在目标对象的方法执行之后简单的打印一下
System.out.println("-------------------after 去玩,去逛街------------------");
return result;
}
/**
* 获取目标对象的代理对象
* @return 代理对象
*/
public Object getProxy() {
return Proxy.newProxyInstance(this.getClass().getClassLoader(),
target.getClass().getInterfaces(), this);
}
/**
* main方法测试JDK代理
* @param args
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws ClassNotFoundException {
/*1、Person person = new Programer();
MyInvocationHandler invocation = new MyInvocationHandler(person);
Person proxyPerson = (Person) invocation.getProxy();
proxyPerson.wear();*/
/*Class<?>[] interfaces = new Programer().getClass().getInterfaces();
System.out.println();
Class interfaceClass = Class.forName(interfaces[0].getName(), false, invocation.getClass().getClassLoader());
System.out.println(interfaces[0]);
System.out.println(interfaceClass);*/
/*
* 测试proxy代理类的字节码生成原理
*/
ProxyTest test = new ProxyTest();
try {
test.testProxy();
test.testGenerateProxyClass();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*Class<?> caller = Reflection.getCallerClass(3);
System.out.println(caller);
*/
}
}
//*************************************实现接口的类,用于jdk动态代理的测试*********************************************
/**
* 定义的接口类,因为jdk动态代理对象需要实现接口,源码中会有判断
* 因为jdk代理的是接口,只有实现接口中的方法的对象才可以被代理。
* @author gfw2306
*
*/
interface Person{
public void wear();
}
/**
* person
* @author gfw2306
*
*/
class Programer implements Person{
public void wear(){
System.out.println("穿衣服");
}
}
//*********************************************proxy代理类字节码文件生成原理解析*******************************************
class ProxyGeneratorUtils {
/**
* 把代理类的字节码写到硬盘上
* @param path 保存路径
*/
public static void writeProxyClassToHardDisk(String path) {
// 第一种方法,这种方式在刚才分析ProxyGenerator时已经知道了
// System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles", true);
// 第二种方法
// 获取代理类的字节码
byte[] classFile = ProxyGenerator.generateProxyClass("$Proxy11", Programer.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();
}
}
}
}
/**
* 动态代理测试类
* @author gfw2306
* @since 2017-8-11
*
*/
class ProxyTest {
public void testProxy() throws Throwable {
// 实例化目标对象
Person programer = new Programer();
// 实例化InvocationHandler
MyInvocationHandler invocation = new MyInvocationHandler(programer);
// 根据目标对象生成代理对象
Person proxy = (Person) invocation.getProxy();
// 调用代理对象的方法
proxy.wear();
}
public void testGenerateProxyClass() {
ProxyGeneratorUtils.writeProxyClassToHardDisk("E:/$Proxy11.class");
}
}