Jdk dynamic proxy, jdk dynamic

Source: Internet
Author: User

Jdk dynamic proxy, jdk dynamic

Package com. itcast. day3; import java. io. objectInputStream. getField; import java. lang. reflect. constructor; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. util. arrayList; import java. util. collection; /*** print out the constructor, method, and parameter list of the proxy class through reflection * @ author liujl **/public class ProxyTest {public static void main (String [] args) throws Exception {linoleic Ss clazzProxy = Proxy. getProxyClass (Collection. class. getClassLoader (), Collection. class); System. out. println (clazzProxy); System. out. println ("--------------- begin constructors list ---------------"); Constructor [] constructors = clazzProxy. getConstructors (); for (Constructor constructor: constructors) {String name = constructor. getName (); StringBuilder sb = new StringBuilder (name); sb. append ("("); Class [] clazzPar Ams = constructor. getParameterTypes (); for (Class clazzParam: clazzParams) {sb. append (clazzParam. getName ();} if (clazzParams! = Null & clazzParams. length! = 0) sb. deleteCharAt (sb. length ()-1); sb. append (")"); System. out. println (sb);} System. out. println ("--------------- begin methods list ---------------"); Method [] methods = clazzProxy. getMethods (); for (Method method: methods) {String name = method. getName (); StringBuilder sb = new StringBuilder (name); sb. append ("("); Class [] clazzParams = method. getParameterTypes (); for (Class clazzParam: clazzParams) {sb. append (clazzParam. GetName (). append (",");} if (clazzParams! = Null & clazzParams. length! = 0) sb. deleteCharAt (sb. length ()-1); sb. append (")"); System. out. println (sb);} System. out. println ("--------------- begin create instance object ----------------"); // clazzProxy. newInstance (); // This does not work. This will only drop the construction method without parameters, and the proxy object does not have no parameters to construct. // The first method is to create the instance Constructor constructor = clazzProxy. getConstructor (InvocationHandler. class); class MyInvocationHander1 implements InvocationHandler {@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {return null;} Collection proxy1 = (Collection) constructor. newInstance (new MyInvocationHander1 (); System. out. println (proxy1.toString (); proxy1.clear (); // proxy1.size (); // java. lang. nullPointerException // Method 2: Create an instance Collection proxy2 = (Collection) constructor. newInstance (new InvocationHandler () {@ Overridepublic Object invoke (Object proxy, Method method, Object [] Args) throws Throwable {return null ;}}); // The third method is to create an instance of the Proxy Class. The Class and instance creation object are displayed in one step. Collection proxy3 = (Collection) Proxy. newProxyInstance (Collection. class. getClassLoader (), new Class [] {Collection. class}, new InvocationHandler () {ArrayList target = new ArrayList (); // class variable @ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {StringBuilder sbMethodAndParams = new StringBuilder (); SbMethodAndParams. append (method. getName (). append ("("); if (args! = Null) {for (Object obj: args) {sbMethodAndParams. append (obj. toString (). append (",") ;}if (args! = Null & args. length! = 0) {sbMethodAndParams. deleteCharAt (sbMethodAndParams. length ()-1) ;}} sbMethodAndParams. append (")"); System. out. println (sbMethodAndParams); long beginTime = System. currentTimeMillis (); Object retVal = method. invoke (target, args); long endTime = System. currentTimeMillis (); System. out. println (method. getName () + "execution time" + (endTime-beginTime) + "millisecond"); return retVal ;}}); proxy3.clear (); proxy3.add ("ljl "); proxy3.add ("wiseq "); Proxy3.add ("traits"); System. out. println ("number of set elements =" + proxy3.size (); // Proxy is certainly inherited from the Object, // proxy3.getClass () Why not call the getClass () of the target class () get the bytecode of ArrayList? // That is because only three methods of the Object are delegated to InvocationHander, which are toString, hashCode, and equals. While the getClass () method, the generated proxy class has its own implementation System. out. println (proxy3.getClass (). getName ());}}

 

The method of plane view is encapsulated as an object, and the object is passed to the proxy object. The method of executing the incoming object is equivalent to executing the code of plane view.

 

 

I think the hard-coded dynamic proxy shown above does not make any sense in actual development. To be more useful, We need to extract some things to implement parameterized proxy.

 

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.