A detailed study of Java dynamic Agent

Source: Internet
Author: User

Before talking about the Java dynamic Agent, let's look at the proxy mode.

The proxy class is enhanced by the combination, and the Hello class is delegated.

Proxy Mode code:

Public class Proxypattern {

Interface ihello{

void say ();

}

Static class Hello implements ihello{

Public void say () {

System. out. println ("Hello World");

}

}

Static class Proxy implements ihello{

Ihello Ihello;

Public Proxy (Ihello Hello) {

Ihello = Hello;

}

@Override

Public void say () {

System.   out. println ("The Is Proxy"); The Hello class has been enhanced

Ihello.say ();

}

}

Public Static void Main (string[] args) {

Ihello Hello = new hello ();

Ihello proxy = new proxy (hello);

Proxy.say (); The proxy class implements the delegate of the implementation class, and the implementation class is enhanced.

}

}

Here are the dynamic agents:

Dynamic proxy class Dynamicproxy does not write directly to a proxy class, but by implementing the Invocationhandler interface. In the bind () method through the Ihello interface, class loader, call Proxy.newproxyinstance () method to dynamically generate $proxy0 bytecode, through the Jad decompile can get $proxy0 class, So replace the proxy.newproxyinstance () code with the new $Proxy 0 (this); is the same effect. At this point, the $proxy0 class is obtained by the Bind method, which is enhanced in the Invoke method, and then called to the say method of the Hello class by the $proxy0 reflection mechanism (because proxydynamic implements the Ihello interface, the $ that is generated when the Bind method The Proxy0 class also implements the Ihello interface, so that the proxy mode can be implemented by invoking the say method of the Hello class through the reflection mechanism.

Dynamic Proxy Code:

Import Java.lang.reflect.Field;

Import Java.lang.reflect.InvocationHandler;

Import Java.lang.reflect.Method;

Import Java.lang.reflect.Proxy;

Import java.util.Properties;

public class Dynamicproxytest {

Interface ihello{

void Say ();

}

Static class Hello implements ihello{

public void Say () {

System.out.println ("Hello World");

}

}

Static class Dynamicproxy implements invocationhandler{

Object obj;

Object bind (Object obj) {

This.obj = obj;

System.out.println ("Get Ihello Interface");

Return Proxy.newproxyinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), this);

Through the interface, implements the class, generates the byte code of the proxy class, the $proxy0 class after the decompile

return new $Proxy 0 (this); Equivalent to the above sentence, which means to generate a proxy class.

}

@Override

public object invoke (object proxy, Method method, object[] args)

Throws Throwable {

System.out.println ("Welcome");

return Method.invoke (obj, args);

Call this invoke () method from the Say () method in the $proxy0 class, which is the say () method of the Hello class, which invokes the Say () method of the Hello class through the reflection mechanism.

}

}

public static void Main (string[] args) {

Field field;

try {

field = System.class.getDeclaredField ("props");

Field.setaccessible (TRUE);

Properties props = (properties) field.get (null);

Props.put ("Sun.misc.ProxyGenerator.saveGeneratedFiles", "true");

} catch (Exception e) {

E.printstacktrace ();

}

New Dynamicproxy (). bind (New Hello ()) is actually a $proxy0 class that implements the Ihello class

Ihello Ihello = (Ihello) new Dynamicproxy (). bind (New Hello ());

Ihello.say ();

}

}

The byte code generated by the dynamic agent is $proxy0 by an inverse compilation, which is passed through Proxy.newproxyinstance (Obj.getclass (). getClassLoader (), Obj.getclass (). Getinterfaces (), this); the generated bytecode.

Decompiled by Jad V1.5.8e2. Copyright 2001 Pavel Kouznetsov.

Jad Home page:http://kpdus.tripod.com/jad.html

Decompiler Options:packimports (3)

Import java.lang.reflect.*;

Public final class $Proxy 0 extends Proxy

Implements dynamicproxytest.ihello{

Public $Proxy 0 (Invocationhandler invocationhandler) {

Super (Invocationhandler);

}

Public final void Say () {

Try

{

Super.h.invoke (this, M3, null);

Call the Invoke method of the Invocationhandler implementation class, implement the proxy pattern, and invoke the M3 method through the reflection mechanism, that is, the say () method of the Hello class.

Return

}

catch (Error _ex) {}

catch (Throwable throwable)

{

throw new Undeclaredthrowableexception (Throwable);

}

}

Public final Boolean equals (Object obj)

{

Try

{

Return ((Boolean) Super.h.invoke (this, M1, new object[] {

Obj

}). Booleanvalue ();

}

catch (Error _ex) {}

catch (Throwable throwable)

{

throw new Undeclaredthrowableexception (Throwable);

}

return false;

}

Public final int hashcode ()

{

Try

{

Return ((Integer) Super.h.invoke (this, M0, null)). Intvalue ();

}

catch (Error _ex) {}

catch (Throwable throwable)

{

throw new Undeclaredthrowableexception (Throwable);

}

return 0;

}

Public final String toString ()

{

Try

{

Return (String) Super.h.invoke (this, M2, null);

}

catch (Error _ex) {}

catch (Throwable throwable)

{

throw new Undeclaredthrowableexception (Throwable);

}

return null;

}

private static Method m3;

private static Method M1;

private static Method M0;

private static Method m2;

Static

{

Try

{

M3 = Class.forName ("Dynamicproxytest$ihello"). GetMethod ("Say", new class[0]);

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]);

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 ());

}

}

}

Not to be continued ... The following will add the learning of how to generate bytecode.

A detailed study of Java dynamic Agent

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.