Java-proxy is when to call Invocationhandler's Invoke method

Source: Internet
Author: User
Tags getmessage throwable

Recently saw spring dynamic agent, grilled to the depths to see when proxy.newproxyinstance (ClassLoader, proxiedinterfaces, this); see this sentence, suddenly more confused force, Still didn't understand the invocation of the Invoke method, and then searched for a moment, and met a great God article

Special to record, hey: First here is the original link

In fact, the front can be omitted (too late, to sleep), what to create an interface, implement a class, create a class that implements Invocationhandler (not create a row), and so on, the direct focus, the proxy object to execute the Invoke place:

First, we compile, we will come up with a proxy object class file, open the class file to solve the puzzle (at that time is also crazy, this trick didn't think, ah haha):

In our proxy class, there is a protected Invocationhandler member property:

 Public classProxyImplementsjava.io.Serializable {Private Static Final LongSerialversionuid = -2222568056686623797l; /**parameter types of a proxy class constructor*/    Private Static Finalclass<?>[] Constructorparams ={Invocationhandler.class }; /*** A cache of proxy classes*/    Private Static FinalWeakcache<classloader, class<?>[], class<?>>Proxyclasscache=NewWeakcache<> (NewKeyfactory (),Newproxyclassfactory ()); /*** The invocation handler for this proxy instance. * @serial     */    protectedInvocationhandler h;//is here! /*** prohibits instantiation. */    PrivateProxy () {}/*** Constructs a new {@codeProxy} instance from a subclass * (typically, a dynamic Proxy Class) with the specified value * for its INVOC     ation handler. *     * @paramh The invocation handler for this proxy instance * *@throwsNullPointerException If the given invocation handler, {@codeh}, * is {@codenull}. */    protectedProxy (Invocationhandler h) {objects.requirenonnull (h);  This. H =h; }

Then look at the implementation of the proxy object:

 Public Final class$Proxy 0extendsProxyImplementsSubject (target class, class being proxied) {Private StaticMethod M1; Private StaticMethod M0; Private StaticMethod m3; Private StaticMethod m2; Static {          Try{M1= Class.forName ("Java.lang.Object"). GetMethod ("equals"),                      NewClass[] {class.forname ("Java.lang.Object") }); M0= Class.forName ("Java.lang.Object"). GetMethod ("Hashcode"),                      NewClass[0]); M3= Class.forName ("* * *. Realsubject "). GetMethod (" Request ",//proxy method nameNewClass[0]); M2= Class.forName ("Java.lang.Object"). GetMethod ("toString"),                      NewClass[0]); } Catch(nosuchmethodexception nosuchmethodexception) {Throw NewNosuchmethoderror (Nosuchmethodexception.getmessage ()); } Catch(ClassNotFoundException classnotfoundexception) {Throw NewNoclassdeffounderror (Classnotfoundexception.getmessage ()); }      } //Static       Public$Proxy 0 (Invocationhandler invocationhandler) {Super(Invocationhandler); } @Override Public Final Booleanequals (Object obj) {Try {              return((Boolean)Super. H.invoke ( This, M1,Newobject[] {obj}))          . Booleanvalue (); } Catch(Throwable throwable) {Throw Newundeclaredthrowableexception (Throwable); }} @Override Public Final inthashcode () {Try {              return(Integer)Super. H.invoke ( This, M0,NULL) . Intvalue (); } Catch(Throwable throwable) {Throw Newundeclaredthrowableexception (Throwable); }      }         Public Final voidrequest () {//proxy methodTry {              Super. H.invoke ( This, M3,NULL); return; } Catch(Error e) {}Catch(Throwable throwable) {Throw Newundeclaredthrowableexception (Throwable); }} @Override Public FinalString toString () {Try {              return(String)Super. H.invoke ( This, M2,NULL); } Catch(Throwable throwable) {Throw Newundeclaredthrowableexception (Throwable); }      }  }  

See here to understand. The newly returned proxy object, called the Parent Class (proxy) H method, here is the Invoke method related to the place of the proxy.

Java-proxy is when to call Invocationhandler's Invoke method

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.