The concept of reflection and related classes in Java language

Keep creating and accelerate growth! This is the 21st day that I participated in the "Nuggets Daily New Plan·October Update Challenge", click to view event details

concept

Any Java source files written by Java developers need to be compiled to generate bytecode files (.class files). For the Java virtual machine, the bytecode file loaded into it is an object of type Class. (Not the keyword class!) - Reflection is to map various components in ordinary Java classes into corresponding Java classes. For example, a general Java class includes methods, constructors, attribute fields, and modifier information. In the Java language, Method, Constructor, Field, and Modeifier correspond to these components one-to-one.

308fa8566167291e7280c31714b6fce.jpg- In the Java language, most classes and interfaces related to reflection are defined in the software package java.lang.reflect. The layer structure between classes and interfaces in the software package java.lang.reflect is as shown in the figure: (The wording is a bit ugly)

7104fc747b7e92e00c99a1cb67b31ae.jpg

Classes related to reflection

Class class

When a Java program is running, the system always performs Time Type Identification (RTTI) on all Java objects. This information records the class to which each object belongs. The Java virtual machine automatically selects the correct method to run based on the runtime type identification. The Class class is the class used to save these type information. The Class class encapsulates the runtime state of an object and interface. When a class is loaded, an object of Class type is automatically created.

Here are some examples of the main methods given in the Class class: - T cast(Object obj): Force an object into the class and interface represented by this Class object. - static Class<?>forName(String className):Returns the Class object associated with the class or interface with the given string name. - Field getFild(String name): Returns a Field object that reflects the specified public member fields of the class or interface represented by this Class object - ······

Modifier classModifier

Properties, constructors, and general methods in general classes should be modified by modifiers such as public, private, protected, and static. The Modifier class provides static methods and constants to decode the member access modifiers of the class. Modifier sets are represented as integers, with different positions representing different modifiers.

In order to determine whether it is modified by certain modifiers, Modifier provides some methods: - static boolean isXy(int mod):If the integer parameter includes the xy modifier, it returns true, otherwise it returns false. where xy can be abstract、final、interface、native、private、protected、public、static、strictfp、syncchronized、transient、volatile·······. Xy is the same as xy, just capitalize the first letter. - The static method code of the Redlect class defining the modifier string:

```java public static String getModifierStr(){ public static boolean isPublic(int mod) { //被public修饰 return (mod & PUBLIC) != 0; }

public static boolean isPrivate(int mod) { //被private修饰 return (mod & PRIVATE) != 0; } public static boolean isProtected(int mod) { //被protected修饰 return (mod & PROTECTED) != 0; }

public static boolean isStatic(int mod) { //Modified by static return (mod & STATIC) != 0; } //. . . . You should be able to find the code rules, so I won’t show them one by one} ```

Other related categories

There are many classes related to reflection, so I won’t show them one by one. For example: AccessibleObject class, Field class, Constructor class, Methood class, Reflect class, etc. . . . , if you are interested, you can check it out for yourself

Guess you like

Origin blog.csdn.net/y943711797/article/details/132978089