/** * A factory function that generates, defines and returns the proxy class given * the ClassLoader and array of interfaces. */ privatestaticfinalclassProxyClassFactory implementsBiFunction<ClassLoader, Class<?>[], Class<?>> { // prefix for all proxy class names 定义前缀 privatestaticfinalStringproxyClassNamePrefix="$Proxy";
// next number to use for generation of unique proxy class names 原子操作,适用于多线程 privatestaticfinalAtomicLongnextUniqueNumber=newAtomicLong(); public Class<?> 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()); } }
StringproxyPkg=null; // package to define proxy class in intaccessFlags= 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) { intflags= intf.getModifiers(); if (!Modifier.isPublic(flags)) { accessFlags = Modifier.FINAL; Stringname= intf.getName(); intn= name.lastIndexOf('.'); Stringpkg= ((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. */ longnum= nextUniqueNumber.getAndIncrement(); // 生产代理类的名字 StringproxyName= proxyPkg + proxyClassNamePrefix + num; // 一些验证、缓存、同步的操作,不是我们研究的重点 /* * Generate the specified proxy 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). */ thrownewIllegalArgumentException(e.toString()); } } }