Java reflection-using reflection to generate a JDK dynamic agent

Source: Internet
Author: User

1. Core Classes & Interfaces

A proxy class and a Invocationhandler interface are provided under the Java Java.lang.reflect package to generate a JDK dynamic proxy class or dynamic proxy object by using this class and interface.

Proxy is the parent class for all dynamic proxy classes, and it provides two static methods to create dynamic proxy classes and dynamic proxy objects, as follows:

  ? static Class <?> getproxyclass ( ClassLoader  loader, class<?>... interfaces)

  ? static Object   newproxyinstance ( ClassLoader  loader, Class <?>[] interfaces, Invocationhandler h)

getproxyclass Returns the Java.lang.Class object of the proxy class and provides it with a class loader and an array of interfaces. newproxyinstance Returns a proxy class instance for the specified interface that forwards the method call to the specified invocation handler. Each method of executing a proxy object is replaced by the Invoke method that executes the Invocationhandler object. In fact, even if you get a dynamic proxy class in the first way, you need to pass in a Invocationhandler object when the program needs to create an object from that proxy class. That is, each system-generated proxy object has a Invocationhandler object associated with it.

As can be seen from the parameters of the above two methods, only give me one (or more) interface, we can generate the implementation of this (or more) interface proxy class and proxy object.


2. Two ways to generate proxy objects

Here we will use the above mentioned two ways, respectively, to generate an interface (person) of the proxy object.

2.1 Interface person
Package com.tgb.reflect;/** * Target interface *  * @author Wangzhipeng *  */public interface Person {public void walk ();p ublic void SayHello ();}

2.2 Constructing a Invocationhandler class
Package Com.tgb.reflect;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;public class Myinvocationhandler implements Invocationhandler {/** *) executes all methods of the proxy object and is replaced with the following invoke method *  Proxy: Represents the dynamic proxy object; Method: Represents the methods being executed; args: An argument that is passed in on behalf of a method that executes a proxy object */@Overridepublic object Invoke (Object proxy, method, object[] args) thro WS Throwable {//TODO auto-generated method StubSystem.out.println ("Executing methods:" + method.getname ()); if (args! = null) {System . OUT.PRINTLN ("The following is the actual argument passed in when executing the method:"); for (object Object:args) {System.out.println (object);}} else {System.out.println ("The method call has no arguments! ");} return null;}}

2.3 Way One: using Getproxyclass to generate proxy objects
Package Com.tgb.reflect;import Java.lang.reflect.constructor;import Java.lang.reflect.invocationhandler;import java.lang.reflect.proxy;/** * Generate proxy object using Getproxyclass *  * @author Wangzhipeng *  */public class Testreflectproxy { @SuppressWarnings ("unchecked") public static void main (string[] args) throws Exception {//1, Create a Invocationhandler object Invocationhandler Myinvocationhandler = new Myinvocationhandler ();//2, Create a proxy class for the person interface (gets the byte code of the proxy class) class Proxyclass = Proxy.getproxyclass (Person.class.getClassLoader (), new class[] { Person.class});//3, according to the byte code of the proxy class to get its parameters for the Invocationhandler type of constructor constructor constructor = Proxyclass.getconstructor (new Class[] {invocationhandler.class});//4, create a proxy object according to the constructor person person = (person) constructor.newinstance ( Myinvocationhandler);//5, the method that invokes the proxy object Person.walk ();p Erson.sayhello ();/** * Output is as follows: *  * Executing method: Walk the method call has no arguments! *  * Executing method: SayHello The method call has no arguments! */}}

2.4 Way two: using Newproxyinstance to generate proxy objects
Package Com.tgb.reflect;import Java.lang.reflect.invocationhandler;import java.lang.reflect.proxy;/** * Use Newproxyinstance to generate proxy objects *  * @author Wangzhipeng *  */public class TestReflectProxy2 {public static void main ( String[] args) {///1, create a Invocationhandler object Invocationhandler Myinvocationhandler = new Myinvocationhandler ();//2, Using the static method of the proxy class, newproxyinstance directly generates the proxy object of the person interface person person = (person) proxy.newproxyinstance ( Person.class.getClassLoader (), new class[] {person.class}, Myinvocationhandler);//3, method of invoking proxy object Person.walk (); Person.sayhello ();/** * Output results are as follows: *  * Executing method: Walk the method call has no arguments! *  * Executing method: SayHello The method call has no arguments! */}}

3. Dynamic Agent and AOP

Here we extend one small step on the basis of the above example, simulating the following AOP thought principles. In fact, it's very simple, as mentioned above, that each method of executing a proxy object is replaced by the Invoke method that executes the Invocationhandler object. So we just need to add our enhanced method before and after the "Reflection execution target Method (Method.invoke (Target,args))" In the Invoke method. The instance code is as follows:

3.1 Target Interface person
Package com.tgb.reflect.aop;/** * Target interface *  * @author Wangzhipeng *  */public interface Person {public void walk ();p UB LIC void SayHello ();}

3.2 Target class Zhansan
Package com.tgb.reflect.aop;/** * Target class (implements person interface) *  * @author Wangzhipeng *  */public class Zhansan implements Pe Rson {@Overridepublic void SayHello () {System.out.println ("I am Zhang San---->sayhello");} @Overridepublic void Walk () {System.out.println ("I am Zhang San---->walk");}}

3.3 Enhanced class extend
Package com.tgb.reflect.aop;/** * Enhanced class *  * @author Wangzhipeng *  */public class Extend {/** * Enhanced Method 1 */public void E XtendMethod1 () {System.out.println ("I am an enhancement method one");} /** * Enhancement Method 2 */public void ExtendMethod2 () {System.out.println ("I am Enhancement Method II");}}

3.4 Invocationhandler class Myinvocationhandler
Package Com.tgb.reflect.aop;import Java.lang.reflect.invocationhandler;import Java.lang.reflect.method;public Class Myinvocationhandler implements Invocationhandler {//target objects (objects that need to be proxied) private object target;public void Settarget ( Object target) {this.target = target;} /** * All methods of executing proxy objects will be replaced with the following Invoke method */@Overridepublic object Invoke (Object proxy, method, object[] args) throws T hrowable {//Instantiate an enhanced class extend Extend = new extend ();//Perform enhancement Method 1 (simulate spring pre-enhancement) extend. ExtendMethod1 (); object result = Method.invoke (target, args);//"Execute target Method"//Execute Enhancement Method 2 (Simulate spring post enhancement) extend. EXTENDMETHOD2 (); return result;}}

3.5 Agent-generated factory myproxyfactory
Package Com.tgb.reflect.aop;import java.lang.reflect.proxy;/** * Build Agent Factory *  * @author Wangzhipeng *  */public Class Myproxyfactory {/* * Gets a proxy object for a target object (the target class must implement an interface) */public static object GetProxy (object target) {//instantiates a Invocationhan Dler class, and incoming target object Myinvocationhandler Myinvocationhandler = new Myinvocationhandler (); Myinvocationhandler.settarget ( target);//Generate proxy Objects Object proxyobject = Proxy.newproxyinstance (Target.getclass (). getClassLoader (), Target.getclass (). Getinterfaces (), Myinvocationhandler); return proxyobject;}}

3.6 Testing Class Test
Package com.tgb.reflect.aop;/** * Testing class *  * @author Wangzhipeng *  */public class Test {public static void main (Strin G[] (args) {//Create an original Zhansan (implement the Person interface) object as Targetperson Zhansan = new Zhansan ();// Proxy object for generating Zhansan object (target) with Agent factory person Proxyzhansan = (person) myproxyfactory.getproxy (Zhansan);// The method that executes the proxy object (two enhancement methods that will perform the enhanced class of weaving (Extend)) Proxyzhansan.walk ();p Roxyzhansan.sayhello ();}}

3.7 results are as follows


4. Summary

The main methods of the proxy class are four (such as), the JDK dynamic agent mainly uses the proxy class's Getproxyclass and Newproxyinstance method to generate the interface dynamic proxy object, The Invoke method that executes the Invocationhandler object is substituted for each method that executes the proxy object. This provides the foundation for spring's AOP. In addition, in practice, we generally do not directly generate a proxy object for one or more interfaces, but instead generate a proxy object for an interface implementation class (The target Class).




In fact, we seldom use dynamic proxies in ordinary Java programming, and its greatest charm lies in writing frameworks or underlying code.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java reflection-using reflection to generate a JDK 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.