Java Reflection
Java.lang.Class
Object-oriented principles generally believe that everything is the object
A. Class name.
B. If the object exists, the object can be direct. GetClass
C.class.forname ("Full name of the class");
Example:
Package Corelesson;
Import Java.lang.reflect.Constructor;
Import Java.lang.reflect.Field;
Import Java.lang.reflect.Method;
Import Javax.swing.JButton;
public class ClassDemo3 {
public static void Main (string[] args) {
ClassDemo3.class.getClassLoader () Gets the class loader object
System.out.println (ClassDemo3.class.getClassLoader (). GetClass (). GetName ());
Printclassmessage (New JButton ());
}
public static void Printclassmessage (Object obj) {
To get the class information, first get the class type, 3 ways to choose according to the existing conditions
Class C = Obj.getclass ();
Get class Name
System.out.println (C.getname ());
Gets the class name that does not contain the package name
System.out.println (C.getsimplename ());
Gets the method of the class--the object of the Java.lang.reflect.Method class
/*getmethod gets all the public's methods
* The parent class inherits the same
* Getdeclaredmethods () is a method of declaring itself, without asking access rights
* The inheritance cannot be reached.
* */
Method[] ms = C.getmethods ();
for (int i = 0;i < ms.length;i++) {
Gets the class type of the method's return value type
Class returntype = Ms[i].getreturntype ();
System.out.print (Returntype.getname () + "");
System.out.print (Ms[i].getname () + "(");
Gets the parameter list class type for the method
class[] Paramstype = Ms[i].getparametertypes ();
for (int j = 0;j <paramstype.length;j++) {
System.out.print (paramstype[j]+ ",");
}
System.out.println (")");
}
System.out.println ("+++++++++++++++++++++++++++++++++");
/* Member variables are also object Java.lang.reflect.Field;
* Getdeclatredfields gets all the member variables of its own declaration
* GetFields method gets the member variable for all public
*
*
* */
field[] fs = C.getdeclaredfields ();
for (Field Field:fs) {
Gets the class type of the member variable
Class FieldType = Field.gettype ();
Gets the type and name of the member variable
System.out.println (Fieldtype.getname () + "" +field.getname ());
}
System.out.println ("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
/* All constructors are objects of the Java.lang.reflect.Constructor class
* Getdeclaredconstructors (Get All)
* GetConstructor Get the public
*
* */
Constructor[] cs = c.getdeclaredconstructors ();
for (Constructor Constructor:cs) {
System.out.print (Constructor.getname () + "(");
Gets the class type of the argument list of the constructor
class[] Paramstype = Constructor.getparametertypes ();
for (Class Class1:paramstype) {
System.out.print (Class1.getname () + ",");
}
System.out.println (")");
}
}
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The reflection mechanism of Java "introductory version (3)"