JAVA设计模式-动态代理(Proxy)源码分析

在文章:JAVA设计模式-动态代理(Proxy)示例及说明中,为动态代理设计模式举了一个小小的例子,那么这篇文章就来分析一下源码的实现。

一,Proxy.newProxyInstance方法

 @CallerSensitive
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException{
// null检查,h为null就抛出NullPointerException
Objects.requireNonNull(h);
// 将接口类对象数组clone一份。
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);
}
// 是static final 修饰的,源码: private static final Class<?>[] constructorParams ={ InvocationHandler.class };
// 从代理类对象中查找参数为InvocationHandler的构造器
final Constructor<?> cons = cl.getConstructor(constructorParams);
final InvocationHandler ih = h;
// 检测构造器是否是Public修饰,如果不是则强行转换为可以访问的。
if (!Modifier.isPublic(cl.getModifiers())) {
AccessController.doPrivileged(new PrivilegedAction<Void>() {
public Void run() {
cons.setAccessible(true);
return null;
}
});
}
// 通过反射,将h作为参数,实例化代理类,返回代理类实例。
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);
}
}

  newProxyInstance是Proxy的静态方法,代码并不难理解出去权限关的代码外,就剩下两步:

  1,获取代理类对象(21行)

  2,利用反射技术实例化代理类,并返回实例化对象(44行)

  想要详细的了解放射技术,请查阅:Java程序语言的后门-反射机制

  接下来分析21行中的getProxyClass0方法

二,Proxy.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) {
// 接口类对象数组不能大于65535个,否则抛出异常
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缓存中读取的,来看一下这个缓存的声明:

//static final
private static final WeakCache<ClassLoader, Class<?>[], Class<?>> proxyClassCache = new WeakCache<>(new KeyFactory(), new ProxyClassFactory());

  这里涉及到三个类:WeakCache,KeyFactory,ProxyClassFactory,其中后面两个类都是Proxy类的静态内部类,从类名可以大概猜测到,keyFactory是用来生产key的,ProxyClassFactory是用来生产代理类对象的,这个稍后会提到。

  首先看一下WeakCache类的大概结构:

 final class WeakCache<K, P, V> {

     private final ReferenceQueue<K> refQueue= new ReferenceQueue<>();
// the key type is Object for supporting null key
// key的类型为Object,支持null key,这里的null key并不是真的可以使用null最为key,而是一个new Objdec()对象实例。ConcurrentHashMap,不允许键或值null,而HashMap可以。ConcurrentHashMap是线程安全的,HashMap不是。
private final ConcurrentMap<Supplier<V>, Boolean> reverseMap= new ConcurrentHashMap<>(); private final BiFunction<K, P, ?> subKeyFactory;
private final BiFunction<K, P, V> valueFactory; //构造方法
public WeakCache(BiFunction<K, P, ?> subKeyFactory,
BiFunction<K, P, V> valueFactory) {
this.subKeyFactory = Objects.requireNonNull(subKeyFactory);
this.valueFactory = Objects.requireNonNull(valueFactory);
} public V get(K key, P parameter) {
//下面会详细介绍这个方法
} ......
}

  上面的源代码中写明,代理类对象是从proxyClassCache镇南关读取的,接下来就分析WeakCache的get方法

三,WeakCache.get方法

 // key是类加载器,parameter为接口类对象数组
public V get(K key, P parameter) {
// 接口类对象数组null检查。
Objects.requireNonNull(parameter); // 删除过时的条目
expungeStaleEntries();
// 生成缓存key对象实例,如果key = null,cacheKey = new Object();
Object cacheKey = CacheKey.valueOf(key, refQueue); // lazily install the 2nd level valuesMap for the particular cacheKey
// 从缓存map中读取指定cacheKey的缓存数据valuesMap
ConcurrentMap<Object, Supplier<V>> valuesMap = map.get(cacheKey); if (valuesMap == null) {
//如果valuesMap为null,则新增
//putIfAbsent方法解释:如果值存在则返回值,并且不对原来的值做任何更改,如果不存在则新增,并返回null
//Absent的意思是缺席,不在
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
// 获取subKey,这里用到了上面提到的Proxy的静态内部类KeyFactory:subKeyFactory.apply(ket,parameter)
Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));
// 从valuesMap中获取supplier
Supplier<V> supplier = valuesMap.get(subKey);
Factory factory = null; while (true) {
if (supplier != null) {
// supplier might be a Factory or a CacheValue<V> instance
// 4,从工厂中获取代理类对象
V value = supplier.get();
if (value != null) {
// 5,返回
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) {
//1,实例化工厂
factory = new Factory(key, parameter, subKey, valuesMap);
} if (supplier == null) {
//2,保存到valuesMap中
supplier = valuesMap.putIfAbsent(subKey, factory);
if (supplier == null) {
// successfully installed Factory
// 3,赋值
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);
}
}
}
}

  因为程序中Proxy.newProxyInstance是第一次执行,所以while循环开始的时候,supplier,valuesMap都是null。在这个前提下,我为代码的执行顺序做了一个编号,从1-5执行。

  可以看到第5步,也就是源代码的第40行将结果返回,那么,代理类对象就是在第4步,也就是第37行生成的。而且也可以从第3步,也就是第59行发现supplier就是factory。

  那么接下来,就分析一下Factory.get方法。

四,Factory.get方法

  Factory类是WeakCache的内部类。这个类中出去构造方法外,就是get方法了,下面就将这个类的代码给出:

 private final class Factory implements Supplier<V> {

         private final K key;
private final P parameter;
private final Object subKey;
private final ConcurrentMap<Object, Supplier<V>> valuesMap; Factory(K key, P parameter, Object subKey,
ConcurrentMap<Object, Supplier<V>> valuesMap) {
this.key = key;
this.parameter = parameter;
this.subKey = subKey;
this.valuesMap = valuesMap;
} @Override
public synchronized V get() { // serialize access
// re-check
// 检查
Supplier<V> supplier = valuesMap.get(subKey);
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就是WeakCache的valueFactory属性,因为Factory是WeakCache的内部类,所以可以直接访问WeakCache的valueFactory属性
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)
CacheValue<V> cacheValue = new CacheValue<>(value); // try replacing us with CacheValue (this should always succeed)
if (valuesMap.replace(subKey, this, cacheValue)) {
// put also in reverseMap
reverseMap.put(cacheValue, Boolean.TRUE);
} else {
throw new AssertionError("Should not reach here");
} // successfully replaced us with new CacheValue -> return the value
// wrapped by it
return value;
}
}

  关键代码第35行:valueFactory.apply(key, parameter)

  这里的valueFactory就是Proxy的静态内部类ProxyClassFactory,上面也提到过,那么就接着分析ProxyClassFactory的apply方法吧。

五,ProxyClassFactory.apply方法

 /**
* 一个利用给定的类加载器和接口类数组生成,定义并返回代理类对象的工厂方法
* 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 {
// 加载接口类,获得接口类的类对象,第二个参数为false表示不进行实例化
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());
}
}
// package to define proxy class in
// 代理类的包名
String proxyPkg = null;
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 = 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 {
// 返回代理类对象
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());
}
}
}
}

  在代码的第96行,生成了代理类的class文件,并且在99行返回了我们需要的代理类对象。那么怎么找到这个生成的代理类class文件呢?两个步骤:

  1,添加一行代码:

//实例化调用处理类(编好的故事)
MyStoryInvocationHandler handler = new MyStoryInvocationHandler(liHong); //添加这一句是生成代理类的class文件,前提是你需要在工程根目录下创建com/sun/proxy目录
System.getProperties().put("sun.misc.ProxyGenerator.saveGeneratedFiles","true");

  2,创建com/sun/proxy目录:

  JAVA设计模式-动态代理(Proxy)源码分析

  当这个程序执行完之后就会有$Proxy0.class文件出现:

  JAVA设计模式-动态代理(Proxy)源码分析

  

六,反编译$Proxy0.class

 package com.sun.proxy;

 import com.zcz.proxyTest.testtwo.Singer;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException; //继承了Proxy类,实现了Singer接口
public final class $Proxy0 extends Proxy implements Singer{
private static Method m1;
private static Method m4;
private static Method m2;
private static Method m3;
private static Method m0; //构造方法,直接调用了父类,也就是Proxy的构造方法,参数paramInvocationHandler就是我们的MyStoryInvocationHandler实例化对象handler
public $Proxy0(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 (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
} //实现了sayGoodBye
public final void sayGoodBye(String paramString)
throws {
try
{
// 这里的h就是我们的MyStoryInvocationHandler实例化对象handler,原因在下方解释。
// 这里直接调用了MyStoryInvocationHandler的invoke方法
this.h.invoke(this, m4, new Object[] { paramString });
return;
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
} public final String toString()
throws
{
try
{
return (String)this.h.invoke(this, m2, null);
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
}
//实现了orderSong方法
public final void orderSong(String paramString)
throws
{
try
{
// 这里的h就是我们的MyStoryInvocationHandler实例化对象handler,原因在下方解释。
// 这里直接调用了MyStoryInvocationHandler的invoke方法
this.h.invoke(this, m3, new Object[] { paramString });
return;
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
} public final int hashCode()
throws
{
try
{
return ((Integer)this.h.invoke(this, m0, null)).intValue();
}
catch (Error|RuntimeException localError)
{
throw localError;
}
catch (Throwable localThrowable)
{
throw new UndeclaredThrowableException(localThrowable);
}
} //静态代码块,做初始化操作
static
{
try
{
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[] { Class.forName("java.lang.Object") });
//通过反射,获取sayGoodBye方法对象实例
m4 = Class.forName("com.zcz.proxyTest.testtwo.Singer").getMethod("sayGoodBye", new Class[] { Class.forName("java.lang.String") });
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
//通过放射,获取orderSong方法对象实例
m3 = Class.forName("com.zcz.proxyTest.testtwo.Singer").getMethod("orderSong", new Class[] { Class.forName("java.lang.String") });
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());
}
}
}

  从第一条中,可以得知,代理类实例化的代码是:cons.newInstance(new Object[]{h})。这里是通过反射调用代理类对象的构造方法,传入了参数h(我们的MyStoryInvocationHandler实例化对象handler)。

  如果你想要更详细的了解反射机制,请查阅:Java程序语言的后门-反射机制

  这个构造方法,就是上述反编译代码里的构造方法,而上述反编译代码里的构造方法调用了Proxy类的构造方法,来看一下Proxy类的构造方法:

     /**
* the invocation handler for this proxy instance.
* @serial
*/
protected InvocationHandler h;
private Proxy() {
}
protected Proxy(InvocationHandler h) {
Objects.requireNonNull(h);
this.h = h;
}

  这里将我们传入的handler直接赋值给了InvocationHandler h。上述反编译代码中的45行和82行使用的this.h 就是我们传入的handler。

  所以proxy.orderSong("就是现在");方法在执行的时候会去调用MyStoryInvocationHandler类的invoke方法。

相关java设计模式的文章:

  JAVA设计模式-动态代理(Proxy)示例及说明

  JAVA设计模式-动态代理(Proxy)源码分析

  JAVA设计模式-单例模式(Singleton)线程安全与效率


原创不易,转载请声明出处:https://www.cnblogs.com/zhangchengzi/p/9713807.html

上一篇:基于Cookie跨域的单点登录问题


下一篇:java集合系列之LinkedList源码分析