0x01 description
"Dynamic agent dynamics, is the use of Java code to actually write a proxy class of" Static "proxy, its advantage is not to omit the writing agent class of the workload, but to realize that the original class and interface is unknown, you can determine the behavior of the proxy class, When the proxy class is disconnected from the original class, it can be flexibly reused in different scenarios "from the deep understanding of Java Virtual Machine Java advanced features and best practices."
Its disadvantage: Because the class in Java only supports single inheritance, and the proxy class has a common parent proxy, all of them are doomed to the fault that the dynamic proxy cannot act as proxy for the class member method.
0x02 Demo
Package test; Public Interface IWorker { object Work (Object ... objects);}
Package test; Public class Implements IWorker { @Override public object Work (Object ... objects) { System.out.println ( "Working ..."); return "Work Method"; }}
Packagetest;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method; Public classHandlerImplementsInvocationhandler {IWorker mworker; Handler (IWorker IWorker) { This. Mworker =IWorker; } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsThrowable {if(Method.getname (). Equals ("Equals") {System.out.println ("The Equals method would always return true"); return true; } Else if(Method.getname (). Equals ("Hashcode") {System.out.println ("Yes, the hashcode is"); } Else if(Method.getname (). Equals ("ToString") {System.out.println ("ToString method has been called"); } returnMethod.invoke (Mworker, args); }}
Packagetest;ImportJava.lang.reflect.Proxy; Public classTest { Public Static voidMain (string[] args) {system.getproperties (). Put ("Sun.misc.ProxyGenerator.saveGeneratedFiles", "true"); IWorker worker=(IWorker) proxy.newproxyinstance (IWorker.class. getClassLoader (), Worker.class. Getinterfaces (),NewHandler (NewWorker ())); System.out.println (Worker.work ()); System.out.println (Worker.equals (NULL)); System.out.println (Worker.tostring ()); System.out.println (Worker.hashcode ()); }}
0X03 code for dynamically generated proxy classes
PackageCom.sun.proxy;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;Importjava.lang.reflect.UndeclaredThrowableException;Importtest. IWorker; Public Final class$Proxy 0extendsProxyImplementsiworker{Private StaticMethod M1; Private StaticMethod m2; Private StaticMethod m3; Private StaticMethod M0; Public$Proxy 0 (invocationhandler paraminvocationhandler)throws { Super(Paraminvocationhandler); } Public Final Booleanequals (Object paramobject)throws { Try { return((Boolean) This. H.invoke ( This, M1,Newobject[] {paramobject}). Booleanvalue (); } Catch(error|runtimeexception Localerror) { ThrowLocalerror; } Catch(Throwable localthrowable) {Throw Newundeclaredthrowableexception (localthrowable); } } Public FinalString toString ()throws { Try { return(String) This. H.invoke ( This, M2,NULL); } Catch(error|runtimeexception Localerror) { ThrowLocalerror; } Catch(Throwable localthrowable) {Throw Newundeclaredthrowableexception (localthrowable); } } Public FinalObject work (object[] paramarrayofobject)throws { Try { return(Object) This. H.invoke ( This, M3,Newobject[] {paramarrayofobject}); } Catch(error|runtimeexception Localerror) { ThrowLocalerror; } Catch(Throwable localthrowable) {Throw Newundeclaredthrowableexception (localthrowable); } } Public Final inthashcode ()throws { Try { return(Integer) This. H.invoke ( This, M0,NULL) . Intvalue (); } Catch(error|runtimeexception Localerror) { ThrowLocalerror; } Catch(Throwable localthrowable) {Throw Newundeclaredthrowableexception (localthrowable); } } Static { Try{M1= Class.forName ("Java.lang.Object"). GetMethod ("equals",NewClass[] {class.forname ("Java.lang.Object") }); M2= Class.forName ("Java.lang.Object"). GetMethod ("toString"),NewClass[0]); M3= Class.forName ("Test. IWorker "). GetMethod (" Work ",NewClass[] {class.forname ("[Ljava.lang.Object;") }); M0= Class.forName ("Java.lang.Object"). GetMethod ("Hashcode",NewClass[0]); return; } Catch(nosuchmethodexception localnosuchmethodexception) {Throw NewNosuchmethoderror (Localnosuchmethodexception.getmessage ()); } Catch(ClassNotFoundException localclassnotfoundexception) {Throw NewNoclassdeffounderror (Localclassnotfoundexception.getmessage ()); } }}0X04 Note
1, the dynamic generation of methods are final
2, in addition to the methods in the interface, by default it also covers the ToString, Hashcode, and Equals methods in the object class
3, each method is the exception of the capture, although the exception is a simple classification, but still use the strange syntax to catch the run-time exception is thrown, so in the use of the proxy class can still use the normal exception logic to handle the run-time thrown exception
Java Dynamic Agent