This article describes the Java viewing information of a class through reflection. Share it for your reference, as follows:
A finishing touch
1 Get Class object through reflection
Use the forName() static method of the Class class. This method requires passing in a string parameter, and the value of the string parameter is the fully qualified class name of a certain class (the full package name must be added).
Call the class attribute of a certain class to get the corresponding Class object of the class. For example, the Class object corresponding to the Person class will be returned.
Calls the getClass() method of an object. This method is a method in the class, so all Java objects can call this method, and the method will return the Class object corresponding to the class to which the object belongs.
2 Get information from Class
Get the constructor
Access the methods contained in the corresponding class of Class
Access the properties (Field) contained in the corresponding class of Class
Access the comments contained on the class corresponding to Class.
Access the inner class contained in the corresponding class of the Class object.
Access the external class where the corresponding class of the Class object is located.
Access the parent class inherited by the corresponding class to the Class object, the implemented interface, etc.
2. Practical battle
1 Code
import .*; import .*; import .*; // Definition repeatable annotations@Repeatable() @interface Anno { } @Retention(value = ) @interface Annos { Anno[] value(); } // Use 4 annotations to modify this class@SuppressWarnings(value = "unchecked") @Deprecated // Use repeated annotations to modify this class@Anno @Anno public class ClassTest { // Define a private constructor for this class private ClassTest() { } // Define a constructor with parameters public ClassTest( String name ) { ("Execute a constructor with parameters"); } // Define an info method without parameters public void info() { ("Execute info method without parameters"); } // Define an info method with parameters public void info( String str ) { ("Execute info method with parameters" + ", its str parameter value:" + str); } // Define an internal class for testing class Inner { } public static void main( String[] args ) throws Exception { // The following code can get the ClassTest corresponding to the ClassTest Class<ClassTest> clazz = ; // Get all constructors of the corresponding class to the Class object Constructor[] ctors = (); ("The entire constructor of ClassTest is as follows:"); for (Constructor c : ctors) { (c); } // Get all public constructors for the corresponding class to the Class object Constructor[] publicCtors = (); ("The entire public constructor of ClassTest is as follows:"); for (Constructor c : publicCtors) { (c); } // Get all public methods of the corresponding class to the Class object Method[] mtds = (); ("The entire public method of ClassTest is as follows:"); for (Method md : mtds) { (md); } // Get the specified method of the corresponding class to the Class object ("ClassTestWith a string parameterinfo()The method is:" + ("info", )); // Get all the annotations on the corresponding class to the Class object Annotation[] anns = (); ("The entire Annotation of ClassTest is as follows:"); for (Annotation an : anns) { (an); } ("The @SuppressWarnings annotation on this Class element is:" + (())); ("The @Anno annotation on this Class element is:" + (())); // Get all internal classes of the corresponding class to the Class object Class<?>[] inners = (); ("The entire inner classes of ClassTest are as follows:"); for (Class c : inners) { (c); } // Use method to load the Inner inner class of ClassTest Class inClazz = ("ClassTest$Inner"); // Access the external class where the class is located through getDeclaringClass() ("The outer class of the corresponding class inClazz is:" + ()); ("ClassTest package is:" + ()); ("The parent class of ClassTest is:" + ()); } }
2 Run
All constructors of ClassTest are as follows:
private ClassTest()
public ClassTest()
All public constructors of ClassTest are as follows:
public ClassTest()
All public methods of ClassTest are as follows:
public static void ([]) throws
public void ()
public void ()
public final void () throws
public final void (long,int) throws
public final native void (long) throws
public boolean ()
public ()
public native int ()
public final native ()
public final native void ()
public final native void ()
The info() method with a string parameter in ClassTest is: public void ()
All Annotations of ClassTest are as follows:
@()
@Annos(value=[@Anno(), @Anno()])
@SuppressWarnings annotation on this Class element is: []
@Anno annotation on this Class element is: [@Anno(), @Anno()]
All internal classes of ClassTest are as follows:
class ClassTest$Inner
The outer class of the corresponding class inClazz is: class ClassTest
The package of ClassTest is: null
The parent class of ClassTest is: class
For more Java-related content, please view the topic of this site:Introduction and Advanced Tutorial on Object-Oriented Programming in Java》、《Java Data Structure and Algorithm Tutorial》、《Summary of Java operating DOM node skills》、《Summary of Java files and directory operation skills"and"Summary of Java cache operation skills》
I hope this article will be helpful to everyone's Java programming.