1.1.
view various information about the dynamic proxy class
through the reflection mechanism can obtain Proxy creates a variety of information about the dynamic proxy class, including implemented interfaces, constructors, methods, and so on. Once you see this information, you can better understand why dynamic proxies are used.
/*** @Title: proxytest.java* @Package com.test.javatechnology.proxy* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 26, 2017 pm 5:36:28*@versionV1.0*/ PackageCom.test.javatechnology.proxy;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Parameter;ImportJava.lang.reflect.Proxy;/*** @ClassName: proxytest* @Description:*@author http://www.cnblogs.com/coe2coe/* @date March 26, 2017 pm 5:36:28**/ Public classProxyTest3 {//InterfaceStatic Interfaceexample{intWorkintAintb);} //class of the actual objectStatic classSomeexampleImplementsexample{@Override Public intWorkintAintb) {System.out.println ("Someexample.work () called.");returnA +b;}} //the handler used to create the proxy object instance for the proxy class. Public Static voidMain (string[] args) {Try {//The actual object. FinalSomeexample Realobject =Newsomeexample ();//to create an object instance of a proxy class using the Newproxyinstance () methodExample Example=(Example) proxy.newproxyinstance (someexample.class. getClassLoader (),//class loader. someexample.class. Getinterfaces (),//the implemented interface. NewInvocationhandler () {//Handler//This function is executed when the method of the proxy object instance is invoked. @Override PublicObject Invoke (Object proxy, Method method, object[] args)throwsthrowable {System.out.println ("Invoke before:" +method.getname ());//forwards to the actual object. Object Result=Method.invoke (Realobject, args); System.out.println ("Invoke after:" +method.getname ());returnresult;}); //gets the class information for the proxy class. Class Clazz=Example.getclass ();//gets the interface implemented by the proxy class. System.out.println ("Interfaces:"); for(Class i:clazz.getinterfaces ()) {System.out.println (I.gettypename ());}//gets the constructor for the proxy classSystem.out.println ("Constructos:"); for(Constructor c:clazz.getconstructors ()) {System.out.print (C.getname () )+ "(" );Booleanfirst=true; for(Parameter p:c.getparameters ()) {if(!First ) {System.out.print (" , ");}Else{ First=false;} System.out.print (P.gettype (). Gettypename ()+ " " +p.getname ());} System.out.println (")");} //gets the method of the proxy class. System.out.println ("Methods:"); for(Method m:clazz.getmethods ()) {System.out.print (M.getname () )+ "(" );Booleanfirst=true; for(Parameter p:m.getparameters ()) {if(!First ) {System.out.print (" , ");}Else{ First=false;} System.out.print (P.gettype (). Gettypename ()+ " " +p.getname ());} System.out.println (")");} } Catch(IllegalArgumentException e) {e.printstacktrace ();}Catch(SecurityException e) {e.printstacktrace ();} } }
Run Results :
Interfaces
Com.test.javatechnology.proxy.proxytest3$example
Constructos:
Com.test.javatechnology.proxy. $Proxy 0 (Java.lang.reflect.InvocationHandler arg0)
Methods
Equals (Java.lang.Object arg0)
ToString ()
Hashcode ()
Work (int arg0, int arg1)
Isproxyclass (Java.lang.Class arg0)
Newproxyinstance (Java.lang.ClassLoader arg0, java.lang.class[] arg1, Java.lang.reflect.InvocationHandler arg2)
Getinvocationhandler (Java.lang.Object arg0)
Getproxyclass (Java.lang.ClassLoader arg0, java.lang.class[] arg1)
Wait ()
Wait (long arg0, int arg1)
Wait (long arg0)
GetClass ()
Notify ()
Notifyall ()
Java Key Technologies (27) Dynamic proxy view type information for proxy objects