Sample applets that use reflection in Java to analyze classes

Source: Internet
Author: User
Tags modifiers

ImportJava.util.*;Importjava.lang.reflect.*;/*** This program uses reflection to print all features of a class. *@version1.1 2004-02-21 *@authorCay Horstmann*/ Public classreflectiontest{ Public Static voidMain (string[] args) {//read class name from command line args or user inputString name; if(Args.length > 0) name = Args[0]; Else{Scanner in=NewScanner (system.in); System.out.println ("Enter class name (e.g. java.util.Date):"); Name=In.next (); }      Try      {         //Print class name and superclass name (if! = Object)Class cl = class.forname (name);//to get the class object from a stringClass SUPERCL =Cl.getsuperclass (); //cl.getmodifiers () returns an integer value, using different bit switches to describe the usage of modifiers like public//The modifier.tostring method returns a string that describes the bit switch. A string that converts an integer value to the corresponding modifierString modifiers =modifier.tostring (Cl.getmodifiers ()); if(Modifiers.length () > 0) System.out.print (modifiers + ""); System.out.print ("Class" +name); if(SUPERCL! =NULL&& SUPERCL! = Object.class) System.out.print ("extends" +supercl.getname ()); System.out.print ("\n{\n");         Printconstructors (CL);         System.out.println ();         Printmethods (CL);         System.out.println ();         Printfields (CL); System.out.println ("}"); }      Catch(ClassNotFoundException e) {e.printstacktrace (); } system.exit (0); }   /*** Prints All constructors of a class *@paramcl A class*/    Public Static voidprintconstructors (Class cl) {//with the class object, you get an array containing the constructor object , which contains the class description//information for all constructors of the classConstructor[] Constructors =cl.getdeclaredconstructors (); //extracting information from a constructor       for(Constructor c:constructors) {String name=C.getname (); System.out.print ("   "); String modifiers=modifier.tostring (C.getmodifiers ()); if(Modifiers.length () > 0) System.out.print (modifiers + ""); System.out.print (Name+ "("); //Print parameter Types//returns an array of class objects that describe the type of the parameterclass[] Paramtypes =c.getparametertypes ();  for(intj = 0; J < Paramtypes.length; J + +)         {            if(J > 0) System.out.print (",");         System.out.print (Paramtypes[j].getname ()); } System.out.println (");"); }   }   /*** Prints All methods of a class *@paramcl A class*/    Public Static voidPrintmethods (Class cl) {//returns an array containing the method object, returning all methods of the class or interface, but not including methods inherited by the superclassMethod[] Methods =Cl.getdeclaredmethods ();  for(Method m:methods) {//returns a class object that describes the return typeClass RetType =M.getreturntype (); String name=M.getname (); System.out.print ("   "); //print modifiers, return type and method nameString modifiers =modifier.tostring (M.getmodifiers ()); if(Modifiers.length () > 0) System.out.print (modifiers + ""); System.out.print (Rettype.getname ()+ "+ name +" ("); //Print parameter Types//returns an array of class objects that describe the type of the parameterclass[] Paramtypes =m.getparametertypes ();  for(intj = 0; J < Paramtypes.length; J + +)         {            if(J > 0) System.out.print (",");         System.out.print (Paramtypes[j].getname ()); } System.out.println (");"); }   }   /*** Prints All fields of a class *@paramcl A class*/    Public Static voidPrintfields (Class cl) {//returns an array containing Field objects that record all the fields of this classfield[] Fields =Cl.getdeclaredfields ();  for(Field f:fields) {Class type=F.gettype (); String name=F.getname (); System.out.print ("   "); String modifiers=modifier.tostring (F.getmodifiers ()); if(Modifiers.length () > 0) System.out.print (modifiers + ""); System.out.println (Type.getname ()+ "+ name +"; "); }   }}

Sample applets that use reflection in Java to analyze classes

Related Article

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.