Chapter 16 Reflections and Annotations

Through the Java reflection mechanism, you can access the description of the Java object that has been loaded into the JVM in the program, and realize the function of accessing, detecting and modifying the information describing the Java object itself. The Java reflection mechanism is very powerful, and support for this function is provided in the Java.lang.reflect package.

As we all know, all Java classes inherit the Object class, and a getClass() method is defined in the Object class, which returns an object of type Class. For example the following code:

Using the object textFieldC of the Class class, you can access the description information of the textField object used to return the object. The main descriptive information that can be accessed is as follows:

1. Access constructor
When accessing the constructor through the following set of methods, an object or array of type Constructor is returned. Each Constructor object represents a constructor method, and the corresponding constructor method can be manipulated using the Constructor object:

getConstructors().

getConstructor(Class…parameterTypes).

getDeclaredConstructors().

getDeclaredConstructor(Class…parameterTypes).

If you are accessing a specified constructor, you need to access it based on the type of the entry parameter of the constructor. For example, accessing a constructor whose entry parameter types are String and int can be achieved through the following two methods:

objectClass.getDeclaredConstructor(String.class,int.class);
objectClass.getDeclaredConstructor(new Class[]{String.class,int.class});

The common methods provided in the Constructor class are as follows.

The modifier information represented by the return value of the getModifiers() method can be parsed through the Java.lang.reflect.Modifier class. This class provides a series of static methods for parsing, which can not only check whether the specified modifier is Modification, and all modifiers can be obtained in the form of strings. Commonly used static methods of the Modifier class are as follows.

For example, the typical code to determine whether the constructor represented by the object Constructor is privately modified and to obtain all modifiers of the constructor in the form of a string is as follows:

int modifiers = constructor.getModifiers();
boolean isEmbellishByPrivate = Modifier.isPrivate(modifiers);
String embellishment = Modifier.toString(modifiers);

Example: Reflecting all constructors of a class

Create a Demo2 class under the com.mr package, declare a String member variable and three int member variables in this class, and provide three constructors. The specific code is as follows:

operation result:

2. Access member variables
When accessing member variables through the following set of methods, an object or array of Field type will be returned. Each Field object represents each member variable, and the Field object can be used to manipulate the corresponding member variables:

getFields().

getField(String name).

getDeclaredFields().

getDeclaredField(String name).

If you are accessing a specified member variable, you can access it through the name of the member variable. For example, to access a member variable named birthday, the access method is as follows:

object.getDeclaredField(“birthday”);

Commonly used methods provided in the Field class are as follows:

3. Access member methods
When accessing a member method through the following set of methods, an object or array of Method type will be returned. Each Method object represents a method, and the corresponding method can be manipulated using the Method object:

getMethods().

getMethod(String name,Class…parameterTypes).

getDeclaredMethods().

getDeclaredMethods(String name,Class…parameterTypes).

If you want to access a specified method, you need to access it based on the name of the method and the type of the entry parameter. For example, accessing a method named print with entry parameter types of String and int can be achieved in the following two ways:

objectClass.getDeclaredMethod(“print”,String.class,int.class);
objectClass.getDeclaredMethod(“print”,new Class[]{String.class,int.class});
The common methods provided in the Method class are as follows:

Example: Reflecting all member methods of a class

Create a Demo4 class under the com.mr package and write four member methods. details as follows:

operation result:

2. Annotation annotation function
Annotation annotation function is provided in Java, which can be used in the declaration of classes, constructors, member variables, parameters, etc. This function does not affect the running of the program, but it will affect auxiliary tools such as compiler warnings.

1. Define the Annotation annotation function
When defining the Annotation type, you also need to use the interface keyword to define the interface, but you need to add an “@” symbol before the interface keyword. That is to say, the keyword defining the Annotation type is @interface. The implicit consciousness of this keyword is Inherits the java.lang.annotation.Annotation interface. For example, the following code defines an Annotation type:

public @interface NoMemberAnnotation{
}

The Annotation type @NoMemberAnnotation defined above does not contain any members. Such Annotation type is called marker annotation. The following code defines an Annotation type that contains only one member:

public @interface OneMemberAnnotation{
    String value();
}

String: member type. Available member types are String, Class, primitive, enumerated, and annotation, as well as arrays of the listed types.

value: member name. If the Annotation type defined by the definition contains only one member, the member name is usually named value.

The following code defines an Annotation type with multiple members:

public @interface MoreMemberAnnotation{
    String describe();
    Class type();
}

When defining members for an Annotation type, you can also set default values for the members. For example, the following code sets a default value for the member when defining the Annotation type:

public @interface DefaultValueAnnotation{
    String describe() degault"<default value>";
    Class type() default void.class;
}

2. Access Annotaiton information
If @Retention is set to RetentionPolicy.RUNTIME when defining the Annotaiton type, the relevant Annotaiton information can be obtained through reflection when running the program, such as obtaining the Annotaiton information of the constructor, field and method.

The Constructor class, Field class and Method class all inherit the AccessibleObject class, and three methods about Annotaiton are defined in the AccessibleObject class. Among them, the method isAnnotaitonPresent(ClassannotaitonClass) is used to check whether the specified type of Annotaiton has been added. If so, it returns true, otherwise it returns false; the method getAnnotaiton(ClassannotaitonClass) is used to obtain the specified type of Annotaiton. , if it exists, return the corresponding object, otherwise return null; the method getAnnotaiton() is used to obtain all Annotaiton, this method will return an Annotaiton array.

The method getParameterAnnotaitons() is also defined in the Constructor class and Method class, which is used to obtain the Annotaiton added for all parameters. It will be returned as a two-dimensional array of Annotaiton type, and the order in the array is the same as the order declared. If there are no parameters, an array of length 0 is returned; if there are parameters without annotaiton added, a nested array of length 0 will be used as the placeholder.