1. The proxy class
Implementation of Newproxyinstance:
1. Get the proxy class, this proxy class is somewhat similar to the subclass of the proxy class. This proxy class has an accepted
The constructor for Proxy (Invocationhandler h).
public static Object newproxyinstance (ClassLoader loader,
Class<?>[] interfaces,
Invocationhandler h) {
class<?> cl = Getproxyclass(loader, interfaces);
Constructor cons = Cl.getconstructor (constructorparams);
return cons.newinstance (new object[] {h});
}
2. Get the proxy (invocationhandler H) Constructor method.
3. Obtain an instance of the proxy class through the Constructor cons construction method.
2. The following focus is on how getproxyclass constructs the proxy class?
public static class<?> Getproxyclass (ClassLoader loader,
Class<?> interfaces)
byte [] proxyclassfile = Proxygenerator.generateproxyclass (
Proxyname, interfaces);
Proxyclass = DEFINECLASS0 (loader, proxyname,proxyclassfile, 0, proxyclassfile.length);
return Proxyclass;
}
the above implementation is the simplified core code. You can use the proxy class Builder (proxygenerator) to generate the proxy class file (the class file) in the first step. Step Two: Load the class with ClassLoader.
Three: the generateproxyclass method of Proxygenerator class
Proxygenerator Gen = New Proxygenerator (name, interfaces);
Finalbyte[] classfile = Gen.generateclassfile ();
Generateclassfile The implementation of this method is roughly: Generating an inheritance from
/** name of the superclass of proxy classes */
Privatefinalstatic String superclassname = "Java/lang/reflect/proxy" ;
the subclass of the class. To implement each interfaces, the mechanism is: call directly
/** name of field for storing a proxy instance ' s invocation handler * *
Privatefinalstatic String handlerfieldname = "H" ;
The Invoke method.
Note that the implementation strategy here is to generate a binary file directly instead of generating a Java file and then generating the class file in the compilation, instead of generating the class file directly.
Implementation of the dynamic proxy (proxy class implementation)