@OverridepublicsynchronizedVget(){// serialize access// re-checkSupplier<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 loopreturnnull;}// else still us (supplier == this)// create new valueV value =null;try{// <<<<<<<<<<<创建代理类>>>>>>>>>>>
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 valueassert value !=null;// wrap value with CacheValue (WeakReference)CacheValue<V> cacheValue =newCacheValue<>(value);// put into reverseMap
reverseMap.put(cacheValue,Boolean.TRUE);// try replacing us with CacheValue (this should always succeed)if(!valuesMap.replace(subKey,this, cacheValue)){thrownewAssertionError("Should not reach here");}// successfully replaced us with new CacheValue -> return the value// wrapped by itreturn value;}}
@OverridepublicClass<?>apply(ClassLoader loader,Class<?>[] interfaces){Map<Class<?>,Boolean> interfaceSet =newIdentityHashMap<>(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){thrownewIllegalArgumentException(
intf +" is not visible from class loader");}/*
* Verify that the Class object actually represents an
* interface.
*/if(!interfaceClass.isInterface()){thrownewIllegalArgumentException(
interfaceClass.getName()+" is not an interface");}/*
* Verify that this interface is not a duplicate.
*/if(interfaceSet.put(interfaceClass,Boolean.TRUE)!=null){thrownewIllegalArgumentException("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<?> 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;}elseif(!pkg.equals(proxyPkg)){thrownewIllegalArgumentException("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{returndefineClass0(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).
*/thrownewIllegalArgumentException(e.toString());}}}