Create a dynamic class using the Java proxy mechanism and view its method list information

Source: Internet
Author: User

[Java] package com. pzf; import java. lang. reflect. constructor; import java. lang. reflect. method; import java. lang. reflect. proxy; import java. util. collection;/* create a dynamic class and view its method list information */public Class ProxyTest {public static void main (String [] args) {class clazzProxy1 = Proxy. getProxyClass (Collection. class. getClassLoader (), Collection. class); // parameter class 1 loader, class 2 interface (multiple) System. out. println (clazzProxy1.getName (); // 2, View the class method // 2.1 view the constructor System. out. println ("---- Constructor list ----"); Constructor [] constructors = clazzProxy1.getConstructors (); for (constructor: constructors) {// obtain the Constructor name String name = Constructor. getName (); StringBuilder sb = new StringBuilder (name); sb. append ('); // view the method parameter Class [] clazzParames = constructor. getParameterTypes (); for (Class clazzParame: clazzParames) {// retrieve the type name and append it to StringBuilder. Each parameter Use commas to separate sb. append (clazzParame. getName (). append (",") ;}// remove the last comma if (clazzParames! = Null & clazzParames. length! = 0) sb. deleteCharAt (sb. length ()-1); sb. append ('); System. out. println (sb. toString ();} System. out. println ("---- Method list ----"); Method [] methods = clazzProxy1.getMethods (); for (method: methods) {// obtain the name of the constructor String name = Method. getName (); StringBuilder sb = new StringBuilder (name); sb. append ('); // view the method parameter Class [] clazzParames = method. getParameterTypes (); for (Class clazzParame: clazzParames) {// retrieve the type name And append it to StringBuilder. Each parameter is separated by commas. append (clazzParame. getName ()). append (",") ;}// remove the last comma if (clazzParames! = Null & clazzParames. length! = 0) sb. deleteCharAt (sb. length ()-1); sb. append ('); System. out. println (sb. result: $ Proxy0 ---- constructor list ---- $ Proxy0 (java. lang. reflect. invocationHandler) ---- method list ---- add (java. lang. object) hashCode () clear () equals (java. lang. object) toString () 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) wait (long) getClass () y () notifyAll ()

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.