>[] 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
可以看到 , 获得代理类的代码是Class cl = (, intfs);
并由此获得代理类的构造函数 , 生成代理类的实例返回给该方法的调用者 。
继续跟进()方法:
/***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 ProxyClassFactoryreturn proxyClassCache.get(loader, interfaces);}
通过注释可以看到 , 代理类是从 取的 , 如果代理类已经通过实现给定接口的类加载器创建了 , 则返回缓存中的该类的副本;否则将通过创建代理类 。
我们查看的初始化代码
/*** a cache of proxy classes*/private static final WeakCache
可以发现是个用来缓存代理类的类变量[ final] , 大家知道类变量的特点是与类一一对应 , 在一个虚拟机中类只有一个 , 对应着在一个虚拟机中类变量也只有一个【这是一个单例模式】 , 且在此处 , 在Proxy类被加载的时候就赋值了 。
注意这里是虚引用 。虚引用并不会决定对象的生命周期 。如果一个对象仅持有虚引用 , 那么它就和没有任何引用一样 , 在任何时候都可能被垃圾回收器回收 。具体请参考 Java:对象的强、软、弱和虚引用
在赋值操作的参数中有()这么一个构造函数 , 这个是动态代理中的关键:生成代理类的类文件字节码 。继续跟进去 , 找到代理类的生成之处了:
/*** A factory function that generates, defines and returns the proxy class given* the ClassLoader and array of interfaces.* 根据给定的类加载器和接口数组生成代理类的工厂类*/private static final class ProxyClassFactoryimplements BiFunction
- 虹之玉养殖时有哪些注意事项
- [附源码]Java计算机毕业设计SSM翻转课堂微视频管理系统
- java null
- ARM单片机工程之间的切换
- Object javascript基础--对象封装
- 09-06 周二 Python之KNN算法的实现
- 刨析源码,深层讲解 Java-集合框架
- Android 自定义绘制之文字测量知识点
- 三 电系魔法师成长之路—仪器仪表学习设计一个五倍共射放大器
- 建立编程思想 Java 2.0--基础阶段