50_create a dynamic class and view its method list, 50 dynamic
Package com. itcast. day3; import java. lang. reflect. constructor; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. util. collection; public class ProxyTest {public static void main (String [] args) throws Exception {Class clazzProxy1 = Proxy. getProxyClass (Collection. class. getClassLoader (), Collection. class); System. out. println (clazzProxy1.getName (); // com. sun. proxy. $ Proxy0/*** print dynamic The constructor list of the generated proxy class ***** // ***** $ Proxy0 () * $ Proxy0 (InvoketionHandler) */System. out. println ("-------- begin constructors list ----------"); Constructor [] constructors = clazzProxy1.getConstructors (); for (Constructor constructor: constructors) {String name = constructor. getName (); StringBuilder sb = new StringBuilder (name); sb. append ("("); Class [] clazzParams = constructor. getParameterTypes (); for (Class clazz: clazz Params) {sb. append (clazz. getName (); sb. append (',');} if (clazzParams! = Null & clazzParams. length! = 0) sb. deleteCharAt (sb. length ()-1); sb. append (")"); System. out. println (sb);}/** only one constructor with parameters -------- begin constructors list ---------- com. sun. proxy. $ Proxy0 (java. lang. reflect. invocationHandler) * // ***** prints the list of dynamically generated proxy classes. *****/System. out. println ("-------- begin methods list ----------"); Method [] methods = clazzProxy1.getMethods (); for (Method method: methods) {String name = method. getName (); StringBuilder sb = New StringBuilder (name); sb. append ("("); Class [] clazzParams = method. getParameterTypes (); for (Class clazz: clazzParams) {sb. append (clazz. getName (); sb. append (',');} if (clazzParams! = Null & clazzParams. length! = 0) sb. deleteCharAt (sb. length ()-1); sb. append (")"); System. out. println (sb);}/*** are the methods in Collection and Object * -------- begin methods list ---------- add (java. lang. object)Equals (java. lang. Object) are distributed to InvocationHandler. Other methods such as getClass () are implemented by the Proxy toString () hashCode ()Clear () contains (java. lang. object) isEmpty () addAll (java. util. collection) iterator () size () toArray ([Ljava. lang. object;) toArray () remove (java. lang. object) containsAll (java. util. collection) removeAll (java. util. collection) retainAll (java. util. collection) isProxyClass (java. lang. class) getProxyClass (java. lang. classLoader, [Ljava. lang. class;) getInvocationHandler (java. lang. object) newProxyInstance (java. lang. classLoader, [Ljava. lang. class;, java. lang. reflect. invocationHandler) wait (long, int) getClass () y () notifyAll ()*/}}