publicstaticObjectnewProxyInstance(ClassLoaderloader,Class<?>[]interfaces,InvocationHandlerh)throwsIllegalArgumentException{Objects.requireNonNull(h);finalClass<?>[]intfs=interfaces.clone();finalSecurityManagersm=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);}finalConstructor<?>cons=cl.getConstructor(constructorParams);finalInvocationHandlerih=h;if(!Modifier.isPublic(cl.getModifiers())){AccessController.doPrivileged(newPrivilegedAction<Void>(){publicVoidrun(){cons.setAccessible(true);returnnull;}});}returncons.newInstance(newObject[]{h});}catch(IllegalAccessException|InstantiationExceptione){thrownewInternalError(e.toString(),e);}catch(InvocationTargetExceptione){Throwablet=e.getCause();if(tinstanceofRuntimeException){throw(RuntimeException)t;}else{thrownewInternalError(t.toString(),t);}}catch(NoSuchMethodExceptione){thrownewInternalError(e.toString(),e);}}
可以看到通过cl这个class反射调用其构造函数返回了一个实例
1
2
3
4
5
6
7
8
9
10
11
privatestaticClass<?>getProxyClass0(ClassLoaderloader,Class<?>...interfaces){if(interfaces.length>65535){thrownewIllegalArgumentException("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 ProxyClassFactoryreturnproxyClassCache.get(loader,interfaces);}
privatestaticfinalclassProxyClassFactoryimplementsBiFunction<ClassLoader,Class<?>[],Class<?>>{// prefix for all proxy class namesprivatestaticfinalStringproxyClassNamePrefix="$Proxy";// next number to use for generation of unique proxy class namesprivatestaticfinalAtomicLongnextUniqueNumber=newAtomicLong();@OverridepublicClass<?>apply(ClassLoaderloader,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(ClassNotFoundExceptione){}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 inintaccessFlags=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 packageproxyPkg=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{returndefineClass0(loader,proxyName,proxyClassFile,0,proxyClassFile.length);}catch(ClassFormatErrore){/*
* 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());}}}