51_52 creating an instance object of a dynamic class and calling its methods

Source: Internet
Author: User

 PackageCom.itcast.day3;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;Importjava.util.ArrayList;Importjava.util.Collection;/** Three ways to get proxy class objects * One, intuitive * Two, anonymous internal type * Three, from the byte code one step type (Recommended use)*/ Public classProxytest { Public Static voidMain (string[] args)throwsException {//get the byte code of the proxy classClass Clazzproxy1=proxy.getproxyclass (Collection.class. getClassLoader (), Collection.class); //clazzproxy1.newinstance ();//you can't do this because the newinstance is calling the constructor with no arguments.                /**One, the most intuitive way to create a proxy class object * **/        //01. Get a parameterized construction methodSYSTEM.OUT.PRINTLN ("Begin Create Instance"); Constructor Constructor=clazzproxy1.getconstructor (Invocationhandler.class); //a Invocationhandler instance is required for newinstance, but Invocationhandler is an interface, so this class is defined        classInvocationhandlerimpl_1Implementsinvocationhandler{@Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {//TODO auto-generated Method Stub                return NULL; }                    }        //02. Get the Instance objectCollection proxy1= (Collection) constructor.newinstance (NewInvocationhandlerimpl_1 ()); System.out.println (Proxy1.tostring ());//Proxy1.clear ();//no return value function//proxy1.size ();//There is a return value function--report NULL pointer--no target class                        /**second, use anonymous inner class to get proxy class object * * **/Collection Proxy2= (Collection) constructor.newinstance (NewInvocationhandler () {@Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {return NULL;                        }        }); /**three or one step in place get proxy class object * * **/Collection Proxy3=(Collection) proxy.newproxyinstance (Collection.class. getClassLoader (),NewClass[]{collection.class},                 NewInvocationhandler () {ArrayList target=NewArrayList (); @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {LongBegintime=System.currenttimemillis (); Object ReVal=Method.invoke (target, args); LongEndtime=System.currenttimemillis (); System.out.println (Method.getname ()+ "Running" + (endtime-beginTime)); returnReVal;                }                }            ); Proxy3.add ("123"); Proxy3.add ("456");                System.out.println (Proxy3.size ()); System.out.println (Proxy3.getclass (). GetName ());//Com.sun.proxy. $Proxy 0, why not ArrayList? //reason object does not send all the methods to the proxy class constructor method parameters---Invocationhandler to handle                /*The invocation of the hashcode, equals, or ToString method declared in Java.lang.Object on the proxy instance by the Java.lang.reflect.Proxy API is followed by the encoding and assigning interface party        The method is called the same way it is encoded, and is assigned to the Invoke method that invokes the handler, as described above.        The Declaration class for the Method object passed to invoke is Java.lang.Object. The proxy class does not rewrite other public methods for proxy instances inherited from Java.lang.Object, so these methods have the same invocation behavior as they do for java.lang.Object instances. */    }}

51_52 creating an instance object of a dynamic class and calling its methods

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.