Java reflection extracts class information, dynamic proxy, and filtering methods
Package org. rui. classts. reflects; import java. lang. reflect. constructor; import java. lang. reflect. method; import java. util. regex. pattern;/*** method constructor of the extraction class * @ author lenovo ** // {args: showMethods} // view all methods and constructor of a class public class ShowMethods {private static String usage = "usage: showMethod qualified. class. mane "; private static Pattern p = Pattern. compile ("\ w + \\. "); public static void main (String [] args) throws ClassNotFoundException {// System. out. println (args [0] + ":" + args [1]); args = new String [1]; args [0] = "org. rui. classts. reflects. showMethods "; // args [1] =" java. awt. color "; if (args. length <1) {System. out. println (usage); System. exit (0);} int lines = 0; Class
C = Class. forName (args [0]); Method [] m = c. getMethods (); Constructor [] constructor = c. getConstructors (); if (args. length = 1) {for (Method mt: m) {// System. out. println ("tostring:" + mt. toString (); // remove the dot and the front character, such as xx. ss. system. out. println ("m1:" + p. matcher (mt. toString ()). replaceAll ("");} // ---------------------------------------- for (Constructor con: constructor) System. out. println ("c1:" + p. matcher (con. toString ()). replac EAll (""); lines = m. length + constructor. length;} else {for (Method mt: m) {if (mt. toString (). indexOf (args [1])! =-1) {System. out. println ("m2:" + p. matcher (mt. toString ()). replaceAll (""); lines ++ ;}// -------------------------------------------- for (Constructor con: constructor) {if (con. toString (). indexOf (args [1])! =-1) {System. out. println ("c2:" + p. matcher (con. toString ()). replaceAll (""); lines ++ ;}}}}}
Package org. rui. classts. reflects;/*** simple dynamic proxy implementation * @ author lenovo **/interface Interface {void doSomething (); void somethingElse (String arg );} // class ------------------ class RealObject implements Interface {public void doSomething () {System. out. println ("RealObject doSomething");} public void somethingElse (String arg) {System. out. println ("RealObject somethingElse:" + arg) ;}// class-SimpleProxy ----------------- class SimpleProxy implements Interface {private Interface proxied; public SimpleProxy (Interface proxied) {this. proxied = proxied;} // methodpublic void doSomething () {System. out. println ("SimpleProxy doSomething"); proxied. doSomething ();} public void somethingElse (String arg) {System. out. println ("SimpleProxy somethingElse:" + arg); proxied. somethingElse (arg) ;}} public class SimpleProxyDemo {public static void consumer (Interface inta) {inta. doSomething (); inta. somethingElse ("bonobo");} // main ---------- public static void main (String [] args) {// Interface o = new RealObject (); // implement consumer (new RealObject () using real objects; // implement consumer (new SimpleProxy (new RealObject () Using proxy ()));}}
Package org. rui. classts. reflects; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy; // DynamicProxyHandlerclass DynamicProxyHandler implements InvocationHandler {private Object proxied; public DynamicProxyHandler (Object proxied) {this. proxied = proxied;} @ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {System. Out. println ("proxy: =" + proxy. getClass () + "\ nmethod: =" + method + "\ nargs:" + args); if (args! = Null) for (Object o: args) System. out. println ("arg:" + o); // returns the actual object to proxiedreturn method. invoke (proxied, args) ;}} class SimpleDynamicProxy {public static void consumer (Interface iface) {iface. doSomething (); iface. somethingElse ("bonobo =");} public static void main (String [] args) {RealObject robj = new RealObject (); consumer (robj ); // proxy // return the proxy class instance of a specified interface. This interface can assign method calls to the specified calling handler. Interface proxy = (Interface) Proxy. newProxyInstance (Interface. class. getClassLoader (), // defines the Class loader of the proxy Class new Class [] {Interface. class}, // list of interfaces to be implemented by the proxy class new DynamicProxyHandler (robj) // call handler for method calling); consumer (proxy );}}
Package org. rui. classts. reflects; import java. lang. reflect. invocationHandler; import java. lang. reflect. method; import java. lang. reflect. proxy;/*** demo of dynamic Proxy Filtering for some methods * @ author lenovo **/class MethodSelector implements InvocationHandler {private Object obj; public MethodSelector (Object o) {this. obj = o;} public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {// System. out. println ("proxy:" + proxy. getClass (). getSimpleName (); // You can intercept special if (method. getName (). equals ("interesting") {System. out. println ("proxy detected the interesting method");} return method. invoke (obj, args) ;}}// interfaceinterface SomeMethod {void boring1 (); void interesting (String arg); void boring2 (); void boring3 ();} // ------------------------------ class Implementation implements SomeMethod {public void boring1 () {System. out. println ("boring1");} public void interesting (String arg) {System. out. println ("interesting:" + arg);} public void boring2 () {System. out. println ("boring2");} public void boring3 () {System. out. println ("boring3") ;}// public class SelectingMethods {public static void main (String [] args) {// proxy test SomeMethod Proxy = (SomeMethod) proxy. newProxyInstance (SomeMethod. class. getClassLoader (), // defines the Class loader of the proxy Class new Class [] {SomeMethod. class}, // list of interfaces to be implemented by the proxy class new MethodSelector (new Implementation () // call handler for Method Invocation); proxy. boring1 (); proxy. boring2 (); proxy. interesting ("bonobo"); proxy. boring3 ();}}
Comparison Test
Package org. rui. classts; import org. rui. classts. chilnd. *; public class PetCount4 {public static void main (String [] args) {Pet p = new Dog (); Class c = Pet. class; Class c1 = Dog. class; Class c2 = Cat. class; // object comparison class if (p instanceof Dog) {System. out. println ("true");} elseSystem. out. println ("fales"); // class comparison object if (c. isInstance (p) {System. out. println ("true");} elseSystem. out. println ("fales"); // class comparison classif (c. isAssignableFrom (c1) {System. out. println ("true");} elseSystem. out. println ("fales"); if (c2.isAssignableFrom (c1) {System. out. println ("true");} elseSystem. out. println ("fales"); System. out. println ("c = c1:" + (c = c1); System. out. println ("c. equals (c1: "+ (c. equals (c1); System. out. println ("c = Pet. class: "+ (c = Pet. class); System. out. println ("c. equals (Pet. class: "+ (c. equals (Pet. class )));}}