Use reflection analysis structure in Java

zhaozj2021-02-16  62

Javaapi involved in this article is:

Class class in java.lang package, Field, Method, Constructor, Modifier class in java.lang.Reflect, MODIFIER class

Basic programming ideas:

First, you need to obtain the name of the class that needs to be analyzed, specify the class name to the FORNAME (String ClassName) method in the Class class as a parameter, calling the method, returns a Class object of the specified type, the Class object describes this type. Details, some of these important methods include: getFields (), getMethods (), getConstructors () These methods returned to the public fields, methods, and constructor arrays supported by this type, including super-class public members, and is the same The "declared" method is added in the above method name, returns all members defined in this type including private but not including super class members, and now we have obtained the type of fields, methods, and constructors, Next, we use the Field, Method, the Constructor class in the java.lang.Reflect package to get detailed information on fields, methods, and constructors, such as: We want to display all the fields in this type, pseudo code:

// getDeclaredfields Returns an array of all defined fields

Field [] Fields = Class.GetDeclaredFields ();

For (i = 0; i

{

Field f = Fields [i];

Class type = f.gettype (); // gettype () Returns the data type of the field

String name = f.getName (); // getName () Returns a field name

Modifier.toString (F.GetModifiers ()));

// getModifiers () Returns an integer representing the access control

//Modifier.tostring () converts the integer into a corresponding access control string

PRINTLN ("" type.getname () " name);

T //Type.getName () Returns the data type name

}

I look at a complete example below, and the example is very simple!

Import java.lang.reflect. *; import javax.swing. *;

Public class reflecttest {public static void main (string [] args) {string name; if (args.length> 0) Name = args [0]; else name = joptionpane.showinputdialog ("Enter a class name: Press (Java. Lang.double "); try {class cl = class.forname (name); class supercl = cl.getsuperclass (); // get super class Class object System.out.println (" Class " Name); / / Judgment whether the hyperclass is empty or Object if (Supercl! = Null && Supercl! = Object.class) System.out.Println ("Extends" Supercl.GetName ()); system.out.print ("/ n {/ n "); PrintConstructors (CL); System.out.Println (); PrintMethods (Cl); System.out.Println (); PrintField (Cl); System.out.println ("} ");} // Note when using a Class object, pay attention to capture the exception, an exception is the type of Catch (ClassNotFoundException E) {E.PrintStackTrace (); system.out.println ("Type Not Created");} System.exit (0);} / ** * This method prints the details of the constructor * / public static void PrintConstructors (Class Cl) {// Define constructor array constructor [] constructors = cl.GetDeclaredConstructors (); for INT i = 0; i

/ ** * Define a parameter type array This array is the Class object * getParameterTypes () Returns the array of constructor parameter type * Print Parameter Type Name * / class [] paramtypes = constr.getParameterTypes (); for (int J = 0 J 0) System.out.print (","); system.out.print (paramtypes [j] .getname ());} system.out.println ");");}} / ** * Details of this method printing method * / public static void printmeth 4 (Class Cl) {method [] methods = cl.getDeclaredMethods (); for (int i = 0; i < Methods.Length; i ) {method m = methods [i]; class rettype = m.getreturntype (); string name = m.getName (); system.out.print (MODIFIER.TOSTRINT (M.GETMODIFIERS ())) System.out.Print (" " " Name " ("); class [] paramtypes = m.getParameterTypes (); for (int J = 0; J 0) System.out.print (","); system.out.print (paramtypes [j] .getname ());} system.out.println (");") }} / ** * Details of the method print field * / public static void printfield (class cl) {Field [] fields = cl.getDeclaredfields (); for (int i = 0; i

System.out.print (f.getmodifiers ())); system.out.println (" type.getname () " name ";");}}} I often encountered Such a problem: Because if you don't have the command line parameters, I will not be very stupid. In fact, I can use a JOPANE.SHOWINPUTDIALOG to avoid such things, just add a judgment, we have a good style of our programming. Caring for how to write a steady code, not to catch an abnormality!

转载请注明原文地址:https://www.9cbs.com/read-16695.html

New Post(0)