?get方法核心代码解释获取一级和二级键 。
//根据类加载器计算一级键 cacheKey=f(loader);Object cacheKey = CacheKey.valueOf(key, refQueue);?//根据接口计算二级键 subKey=g(interfaces);Object subKey = Objects.requireNonNull(subKeyFactory.apply(key, parameter));?注意这里的subKeyFactory是Proxy初始化WeakCache类对象proxyClassCache时的入参new KeyFactory();KeyFactory是Proxy的内部类 。 不妨进入apply简单看一下:@Overridepublic Object apply(ClassLoader classLoader, Class>[] interfaces) {switch (interfaces.length) {case 1: return new Key1(interfaces[0]); // the most frequentcase 2: return new Key2(interfaces[0], interfaces[1]);case 0: return key0;default: return new KeyX(interfaces);}}?循环 接下来是一个while循环 , 第一次循环前factory和supplier都为null, 所以循环体内只是做了一些初始化的工作 , 并将factory赋值给了supplier(Factory类是WeakCache的内部类 , 且继承了Supplier类) 。 第二次循环时 , 执行V value = http://kandian.youth.cn/index/supplier.get() , 实际上就是调用factory.get()方法获取动态代理类的Class实例。 factory.get()方法方法完整代码如下:
@Overridepublic synchronized V get() { // serialize access// re-checkSupplier 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 loopreturn null;}// else still us (supplier == this)?// create new valueV value = http://kandian.youth.cn/index/null;try {value = Objects.requireNonNull(valueFactory.apply(key, parameter));} finally {if (value == null) { // remove us on failurevaluesMap.remove(subKey, this);}}// the only path to reach here is with non-null valueassert value != null;?// wrap value with CacheValue (WeakReference)CacheValue cacheValue = http://kandian.youth.cn/index/new CacheValue<>(value);?// try replacing us with CacheValue (this should always succeed)if (valuesMap.replace(subKey, this, cacheValue)) {// put also in reverseMapreverseMap.put(cacheValue, Boolean.TRUE);} else {throw new AssertionError("Should not reach here");}?// successfully replaced us with new CacheValue -> return the value// wrapped by itreturn value;}?第4行代码Supplier supplier = valuesMap.get(subKey)通过subKey获取方才的supplier也就是factory 。 然后在第18行调用了valueFactory.apply方法创建动态代理类并将结果赋值给变量value 。valueFactory是初始化WeakCache对象调用构造方法时传入的参数 即Prxoy的内部类ProxyClassFactory对象 。
ProxyClassFactory类进入apply方法
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 inint 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
推荐阅读
-
[蓝菇凉说]对得起价格,基本三年不卡!,目前公认受欢迎的四款华为
-
-
『环球新军事』切尔诺贝利辐射增加,放射元素直扑俄罗斯,美国卫星拍下恐怖一幕
-
澎湃新闻|伊朗演习首次公开地下发射导弹,美国或许知晓导弹埋藏地点
-
『zol中关村在线』更便宜的全新23寸iMac 边框变窄 配置升级
-
明星穿搭|马蓉晒日本游照,皮衣短裙配高筒袜身材不输樱花妹,生活惬意
-
二合一|看规划馆、博物馆二合一的数字博物馆,了解包公故里的前世今生
-
-
央视网|美国明尼苏达州州长:该州的新冠病毒感染中 70%与私人聚会、酒吧和餐馆有关
-
中国新闻网|山西太原出台“人才购房”新政暂试行至2020年12月31日
-
我为肌肉狂|睡前5个小动作,教你轻松练出天鹅臂,告别拜拜肉
-
-
『旷世君玄』三星S10沦为百元机大降价!,联发科P65+水滴屏三星A41发布
-
-
早安隆回|《早安隆回》越火,袁树雄的争议就会越大!
-
大花搞笑|和富豪认识90分钟后闪婚,如今出入豪车接送,曾在春晚上出尽洋相
-
-
挖贝网 业务订单减少,骅盛车电2020年上半年净利344.74万同比增长45.93%
-
太平鸟育儿经|父母晚年有着落,心理学:孩子的孝心哪里来?对他做好3件事
-
啥情况?周深工作室注册少管我商标 具体发生了什么?