com.sun.proxy.$Proxy0 before Method:public abstract void info.victorchu.j8.proxy.Subject.logIn() Logging In ... after Method:public abstract void info.victorchu.j8.proxy.Subject.logIn() before Method:public abstract void info.victorchu.j8.proxy.Subject.playGames() Playing Games ... after Method:public abstract void info.victorchu.j8.proxy.Subject.playGames() before Method:public abstract void info.victorchu.j8.proxy.Subject.logOut() Logging Out ... after Method:public abstract void info.victorchu.j8.proxy.Subject.logOut()
// 摘自 openJDk privatestaticfinalclassProxyClassFactoryimplementsBiFunction<ClassLoader, Class<?>[], Class<?>> { // prefix for all proxy class names privatestaticfinalStringproxyClassNamePrefix="$Proxy";
// next number to use for generation of unique proxy class names privatestaticfinalAtomicLongnextUniqueNumber=newAtomicLong();
@Override 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 + "."; } /* * 生成动态代理类类名 */ longnum= nextUniqueNumber.getAndIncrement(); StringproxyName= proxyPkg + proxyClassNamePrefix + num; /* * 此处生成了动态代理类 */ 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()); } } }