Implementation of dynamic proxy for imitation JDK

Source: Internet
Author: User

Below I imitated the JDK dynamic agent implementation, the source did not see, here is not just according to his function according to his own way to achieve.


Use of technology

The key is the use of callback functions.


Document:

Since we are using dynamic proxies, we want to add some operations to existing methods. Therefore, the callback function is called in the subclass method that is created.

Creating subclasses involving dynamic bytecode technology, I'm going to write dead a subclass instead

Creating a proxy class is responsible for creating child class objects


One: callback function module:

1: Create an interface

Package callback;

Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;

Public interface Callbackinterface {public
	
	Object callback (Method method,object obj,object[] args) throws IllegalArgumentException, Illegalaccessexception, invocationtargetexception;

}


2: User-to-interface implementation:

Package callback;

Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;


public class Clientts implements callbackinterface{

	
	/** callback function **/public
	Object Callback (method method, Object obj , object[] args) throws IllegalArgumentException, Illegalaccessexception, invocationtargetexception {
		SYSTEM.OUT.PRINTLN ("Transaction processing Execution");
		Object result = Method.invoke (obj, args);
		System.out.println ("End of transaction processing");
		return result;
	}
	
	
	
}

3: Service implementation:

Package callback;

Import java.lang.reflect.InvocationTargetException;
Import Java.lang.reflect.Method;

public class Callbackts {
	
	private callbackinterface CBI;
	
	
	Public callbackts (CBI callbackinterface) {
		THIS.CBI = CBI;
	}
	
	/** 
	 * calls the callback () function in the client
	 * specific subclasses, which are implemented in different ways
	 *
	/Public Object Interceptor (method method, Object obj, object[] args) {
		Object result = null;
		try {
			result = Cbi.callback (method, obj, args);
		} catch (IllegalArgumentException e) {
			e.printstacktrace ();
		} catch (Illegalaccessexception e) {
			e.printstacktrace ();
		} catch (InvocationTargetException e) {
			E.printstacktrace ();
		}
		return result;
		
	}
}

Two: Agent module

1: Business interface: It is also possible not to use an interface

Package callback;

Public interface Fatherinter {public
	
	void Add (String name,string love);
	public void Answer (String text);


2: Interface Implementation class:

Package callback;

public class Father implements fatherinter{public
	
	void Add (String name,string Love) {
		System.out.println ("My Name is "+name");
		System.out.println ("and My favorites are" +love);
	}
	public void Answer (String text) {
		System.out.println ("Didn't you feel surprise about" +text);
	}
}

3: Create a subclass of Father:

Package callback;

Import Java.lang.reflect.Method; /** * Subclassts is a dynamic subclass of Father simulation Fatherinter is their interface * @author Trace * * */public class Subclassts extends Father implement
	s Fatherinter {private Object target;
	Private CALLBACKTS CBT = NULL;
		Public subclassts (callbackts cbt,object target) {THIS.CBT = CBT;
	This.target = target; The public void of Add (String name,string Love) {try {//] extends Father//this.add refers to the current add, recursion, dead loop//this.add that is overwritten
			("One", "one");
			method = Target.getclass (). GetMethod ("Add", New Class[]{string.class,string.class});
			object[] args = {Name,love};
		Cbt.interceptor (method, Target, args);
		} catch (SecurityException e) {e.printstacktrace ();
		} catch (Nosuchmethodexception e) {e.printstacktrace (); }} public void Answer (String text) {try {method = Target.getclass (). GetMethod ("Answer", New Class[]{stri
			Ng.class});
			object[] args = {text};
		Cbt.interceptor (method, Target, args); } catch (SecurityException e) {e.printstacktrace ();
		} catch (Nosuchmethodexception e) {e.printstacktrace ();
 }
	}
}


4: Create a proxy class to implement the creation of subclasses:

Package callback;

public class Proxy {
	
	/** Create subclass Object *
	 * @throws illegalaccessexception 
	 * @throws instantiationexception
	* * public static Object Newproxy (Callbackinterface cif,class clazz) throws Instantiationexception, Illegalaccessexception {
		Callbackts CBT = new Callbackts (CIF);
		return new Subclassts (Cbt,clazz.newinstance ());
	}
}

5: Create the object and test:

Package callback;

public class Proxyts {public
	
	
	static object Getproxyts (class Clazz) {
		//here simply creates a subclass subclass object
		obj = null ;
		try {
			obj = proxy.newproxy (new Clientts (), Clazz),
		} catch (Instantiationexception e) {
			e.printstacktrace () ;
		} catch (Illegalaccessexception e) {
			e.printstacktrace ();
		}
		return obj;
	}
	
	public static void Main (string[] args) throws Instantiationexception, illegalaccessexception {
		
		Fatherinter father =  (Fatherinter) Proxyts.getproxyts (father.class);
		Father.add ("Trace", "Write Code");
		Father.answer ("Bleak Future");
	}
}

Results:

Transaction execution My name is the trace and
My favorites is write code
transaction end
transaction execution done you
feel surprise about future Slim 
   End of transaction processing

There are a lot of places to go, at least with the Cglib class enhancer gap is still very large.



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.