Java Dynamic Agent Deep Learning (Proxy,invocationhandler), including $proxy0 source code

Source: Internet
Author: User
Tags object object

Java Dynamic Agent Deep learning,

I. Related classes and their methods:

Java.lang.reflect.Proxy,
Proxy provides a static method for creating dynamic proxy classes and instances.
Newproxyinstance ()
Returns a proxy class instance of a specified interface that can assign a method call to a specified invocation handler
(See API documentation)

Java.lang.reflect.InvocationHandler,
Invocationhandler is the interface that is implemented by the calling handler for the proxy instance.
Invoke ()
Processes the method call on the proxy instance and returns the result. This method is called on the calling handler when the method is called on the proxy instance associated with the method.
(See API documentation)

Two. Source code:

interface and implementation class of the object being proxied:
Package com.ml.test;

Public interface Manager {
public void modify ();
}

Package com.ml.test;

public class Managerimpl implements Manager {

@Override
public void Modify () {
System.out.println ("*******modify () method called");
}
}

Business proxy class:
Package com.ml.test;

Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;

public class Businesshandler implements Invocationhandler {

Private Object object = null;

Public Businesshandler (Object object) {
This.object = object;
}

@Override
public object invoke (object proxy, Method method, object[] args)
Throws Throwable {
System.out.println ("Do something Before Method");
Object ret = Method.invoke (this.object, args);
System.out.println ("Do something After Method");
return ret;

}
}



Client class:
Package com.ml.test;
Import Java.lang.reflect.Proxy;
public class Client {

public static void Main (string[] args) {
Meta-Object (Proxied object)
Managerimpl Managerimpl = new Managerimpl ();

Business proxy class
Businesshandler Securityhandler = new Businesshandler (MANAGERIMPL);

Gets an instance of the proxy class ($Proxy 0 extends proxy implements Manager).
Manager Managerproxy = (manager) proxy.newproxyinstance (Managerimpl
. GetClass (). getClassLoader (), Managerimpl.getclass ()
. Getinterfaces (), Securityhandler);

Managerproxy.modify ();
}
}

Three. Implementation results:
Do something before method
The Modify () method is called
Do something after method

Four. Mechanism Analysis:

Proxy. (ClassLoader loader, class<?>[] interfaces, Invocationhandler h) did the following several things.
(1) The proxy class interfaces is created according to the parameters loader and interfaces call method Getproxyclass (loader, $proxy).
$Proxy Class 0 implements the interfaces interface and inherits the proxy class.
(2) Instantiate the $proxy0 and pass the Businesshandler in the construction method, then $proxy0 call the constructor of the parent class proxy, assign the value to H, as follows:
Class proxy{
Invocationhandler H=null;
Protected Proxy (Invocationhandler h) {
This.h = h;
}
...
}



Here is the source code for the $proxy0 class of this example (it is very hard to bring it up):


Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;
Import java.lang.reflect.UndeclaredThrowableException;

Public final class $Proxy 0 extends Proxy implements Manager {

private static Method M1;
private static Method M0;
private static Method m3;
private static Method m2;

static {
try {
M1 = Class.forName ("Java.lang.Object"). GetMethod ("equals",
New class[] {class.forname ("Java.lang.Object")});
M0 = Class.forName ("Java.lang.Object"). GetMethod ("Hashcode",
New Class[0]);
M3 = Class.forName ("Com.ml.test.Manager"). GetMethod ("Modify",
New Class[0]);
M2 = Class.forName ("Java.lang.Object"). GetMethod ("ToString",
New Class[0]);
} catch (Nosuchmethodexception nosuchmethodexception) {
throw new Nosuchmethoderror (Nosuchmethodexception.getmessage ());
} catch (ClassNotFoundException classnotfoundexception) {
throw new Noclassdeffounderror (Classnotfoundexception.getmessage ());
}
}

Public $Proxy 0 (Invocationhandler invocationhandler) {
Super (Invocationhandler);
}

@Override
Public final Boolean equals (Object obj) {
try {
Return ((Boolean) Super.h.invoke (this, M1, new object[] {obj}))
. Booleanvalue ();
} catch (Throwable throwable) {
throw new Undeclaredthrowableexception (Throwable);
}
}

@Override
Public final int hashcode () {
try {
Return ((Integer) Super.h.invoke (this, M0, null)). Intvalue ();
} catch (Throwable throwable) {
throw new Undeclaredthrowableexception (Throwable);
}
}

Public final void Modify () {
try {
Super.h.invoke (this, M3, null);
Return
} catch (Error e) {
} catch (Throwable throwable) {
throw new Undeclaredthrowableexception (Throwable);
}
}

@Override
Public final String toString () {
try {
Return (String) Super.h.invoke (this, M2, null);
} catch (Throwable throwable) {
throw new Undeclaredthrowableexception (Throwable);
}
}
}

The resulting $proxy0 instance is then forced into the manager.
When the Managerproxy.modify () method is executed, the Modify () method in the $proxy0 class is called.
In the Modify method, invoke the Invoke () method of H in the parent class proxy.
namely Invocationhandler.invoke ();

Java Dynamic Agent Deep Learning (Proxy,invocationhandler), including $proxy0 source code

Related Article

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.