PackageCom.ljy.chapter5;ImportJava.lang.reflect.Constructor;ImportJava.lang.reflect.Field;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Modifier;ImportJava.util.Scanner;/*** This program uses reflection to print all features of a class. *@authorLIAO Jianya **/ Public classreflectiontest{ Public Static voidMain (string[] args) {//read class name from command time 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); Class SUPERCL=Cl.getsuperclass (); String 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 (); System.out.println ("----------------------------------------"); Printmethods (CL); System.out.println (); System.out.println ("----------------------------------------"); Printfields (CL); System.out.println ("}"); } Catch(ClassNotFoundException e) {e.printstacktrace (); } system.exit (0); } /*** Prints All constructor of a class *@paramcl A class*/ Public Static voidprintconstructors (Class cl) {constructor[] constructors=cl.getdeclaredconstructors (); 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 Typesclass[] 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) {method[] methods=Cl.getdeclaredmethods (); for(Method m:methods) {Class 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 Typesclass[] Paramtypes =m.getparametertypes (); for(intj = 0; J < Paramtypes.length; J + +) { if(J > 0) System.out.print (", "); System.out.println (Paramtypes[j].getname ()); } System.out.println (");"); } } /*** Prints All fields of a class *@paramcl A class*/ Public Static voidPrintfields (Class cl) {field[] 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 +"; "); } }}
This program will remind the user to enter the class name and then output all methods in the class and the constructor's signature, as well as all domain names.
Output Result:
Enterclassname (e.g Java.util.Date): java.lang.Double Public Final classJava.lang.Doubleextendsjava.lang.number{ PublicJava.lang.Double (Double); Publicjava.lang.Double (java.lang.String);---------------------------------------- Public Booleanequals (Java.lang.Object); Public StaticJava.lang.String toString (Double); Publicjava.lang.String toString (); Public inthashcode (); Public Static native LongDoubletorawlongbits (Double); Public Static LongDoubletolongbits (Double); Public Static native DoubleLongbitstodouble (Long); Public intCompareTo (java.lang.Double); Public volatile intCompareTo (Java.lang.Object); Public byteBytevalue (); Public ShortShortvalue (); Public intintvalue (); Public LongLongvalue (); Public floatFloatvalue (); Public DoubleDoublevalue (); Public StaticJava.lang.Double ValueOf (Double); Public Staticjava.lang.Double valueOf (java.lang.String); Public StaticJava.lang.String tohexstring (Double); Public Static intCompareDouble, Double); Public Static BooleanIsNaN (Double); Public BooleanIsNaN (); Public Static BooleanIsinfinite (Double); Public BooleanIsinfinite (); Public Static Doubleparsedouble (java.lang.String);---------------------------------------- Public Static Final Doublepositive_infinity; Public Static Final Doublenegative_infinity; Public Static Final DoubleNaN; Public Static Final DoubleMax_value; Public Static Final DoubleMin_normal; Public Static Final DoubleMin_value; Public Static Final intmax_exponent; Public Static Final intmin_exponent; Public Static Final intSIZE; Public Static FinalJava.lang.Class TYPE; Private Final Doublevalue; Private Static Final LongSerialversionuid;}
There are three classes in the Java.lang.reflect package field, method, and constructor are the fields, methods, and constructors that describe the class, respectively.
These three classes have a method called GetName--to return the name of the project, the GetModifiers method--Returns an integer value, and uses a different bit switch to describe the use of modifiers such as public and static.
The field class has a GetType method--Used to return a class object that describes the type of the domain.
The GetFields, GetMethods, and GetConstructors methods in class classes return the public fields, methods, and constructed arrays provided by the class, including the members of the superclass.
The class Getdeclarefields, Getdeclaremethods, and Getdeclaredconstructors methods return all fields, methods, and constructors declared in the class, including private and protected members . But does not include members of the superclass.
Java.lang.reflection Printing all information for a class