Obtain class information through Java reflection

Source: Internet
Author: User
Import  Java. Lang. annotation. annotation;  Import  Java. Lang. Reflect. constructor;  Import  Java. Lang. Reflect. method;  //  Use two annotations to modify the class @ Suppresswarnings (value = "unchecked" ) @ Deprecated  Public   Class  Classtest {  //  Define a private constructor for this class      Private Classtest (){}  //  Define a constructor with Parameters      Public  Classtest (string name) {system. Out. println ( "Execute the constructor with Parameters" );}  //  Define a info method without Parameters      Public   Void  Info (string Str) {system. Out. println ( "Execute the info method with Parameters" + ". In fact, the STR parameter value is:" + Str );}  //  Define an internal class for testing     Class  Inner {}  Public   Static   Void Main (string [] ARGs) Throws  Exception {  //  BelowCodeYou can obtain the class corresponding to classtest. Class <classtest> clazz = classtest. Class  ;  //  Obtains all constructors of the class corresponding to this class object. Constructor [] ctors =Clazz. getdeclaredconstructors (); system. Out. println ( "All constructors of classtest are as follows :" );  For  (Constructor C: ctors) {system. Out. println (c );}  //  Obtains all the public methods of the Class Object. Constructor [] publicctors = Clazz. getconstructors (); system. Out. println ( "All public constructors of classtest are as follows :" );  For  (Constructor C: publicctors) {system. Out. println (c );} //  Obtains the specified method of the class corresponding to the class object. Method [] MTDS = Clazz. getmethods (); system. Out. println ( "All public methods of classtest are as follows :" );  For  (Method MD: MTDs) {system. Out. println (MD );}  //  Obtains the specified method of the class corresponding to the class object. System. Out. println ("the info method with a string parameter in classtest is:" + clazz. getmethod ("info", String. Class  ));  // Obtains all comments on the corresponding class of the Class Object. Annotation [] Anns = Clazz. getannotations (); system. Out. println ( "All annotattion of classtest is as follows :" );  For  (Annotation an: ANNs) {system. Out. println (an);} system. Out. println ( "The @ suppresswarnings comment on this class element is:" + Clazz. getannotation (suppresswarnings.  Class  ));  //  Obtains all internal classes of the Class Object. Class <?> [] Inners =Clazz. getdeclaredclasses (); system. Out. println ( "All internal classes of classtest are as follows :" );  For  (Class C: inners) {system. Out. println (c );}  //  Use the class. forname method to load the inner class of classtest Class inclazz = Class. forname ("classtest $ inner" );  //  Use getdeclaringclass () to protect the external class of this class System. Out. println ("the external class of the inclazz class is:" + Inclazz. getdeclaredclasses (); system. Out. println ( "Classtest package:" +Clazz. getpackage (); system. Out. println ( "The parent class of classtest is:" + Clazz. getsuperclass ());}} 

 

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.