Java notes: reflection, annotation

1、 Reflection

1. Reflection mechanism

The related classes of reflection mechanism except a Java Lang. class, the rest are in Java Lang.reflect package. The reflection mechanism is used to read class bytecode files. It should be noted that only one copy of bytecode will be saved when the JVM loads it into memory. When reading class files many times, you don't have to worry about loading them many times. Common classes related to reflection mechanism:

2. Reflection bytecode (class / type)

There are three ways to obtain the bytecode of a class (java.lang.class class):

Common methods in class:

3. Reflection attribute bytecode field

To obtain a field, you need to obtain the corresponding class bytecode class before you can obtain the corresponding field (Java. Lang.reflect. Field) from the class. Common field methods:

4. Reflection bytecode method

To get the method, you need to get the corresponding class bytecode class before you can get the corresponding method (Java. Lang. reflect. Method) from the class.

Common methods in method:

5. Reflection construction method (constructor)

To obtain the constructor, you need to obtain the corresponding class bytecode class before you can obtain the corresponding method (Java. Lang.reflect. Constructor) from the class.

Common methods in constructor:

2、 Annotation

1. Definition annotation

Annotation, or annotation, is also a reference data type. After compilation, a class file will be generated. See the example for specific usage:

Example of annotation definition:

Examples of annotation usage:

Annotation attribute type: when defining an annotation attribute, the attribute type can be byte, short, int, long, float, double, Boolean, char, string, class, enumeration type, and the array form of these types. It cannot be other types. There is a trick. If the attribute is an array and there is only one array element passed in, the braces defining the array can not be written.

2. Java built-in annotation

Built in annotations in Java Lang package, commonly used are:

3. Meta annotation

The annotation used to mark "annotation type", that is, the annotation of annotation, is called meta annotation, which is in Java Lang.annotation package. Common meta annotations are:

4. Reflection annotation

Take the class annotation as an example. After obtaining the bytecode object of the class, use the method of class object for reflection. The common methods are:

Attribute acquisition: after getting the annotation object through reflection, you can obtain the attribute value by calling a method (actually an attribute). Because the annotation defines an attribute with braces, it looks like calling a method, such as "string name = annotationobj. Name();" Note: the annotation acquisition of methods and attributes is the same as the above methods, and most of the called methods are the same.

5. Role of annotations

Annotation usually checks whether the annotated class and method meet the requirements through the reflection mechanism. For example, @ override checks whether the annotated method is a method that overrides the parent class. If not, it will compile and report an error.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>