Implementation of the dynamic proxy (proxy class implementation)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.