Java development custom annotation-power node

Syntax rules for custom annotations

1. Use the @interface keyword to define annotations, pay attention to the location of the keywords

When using @interface custom annotations, the java.lang.annotation.Annotation interface is automatically inherited, and other details are automatically completed by the compiler. When defining annotations, you cannot inherit other annotations or interfaces.

2. The members are declared with no parameters and no exceptions. Pay attention to the difference between the declaration of general class member variables

Each of these methods actually declares a configuration parameter. The name of the method is the name of the parameter

3. You can use default to specify a default value for the member, as shown above

4. Member types are restricted. Legal types include primitive types and String, Class, Annotation, Enumeration (there are 8 basic data types in JAVA: byte (byte), short (short integer), int (integer type) ), long (long integer), float (single-precision floating-point number type), double (double-precision floating-point number type), char (character type), boolean (boolean type)

5. Annotation classes can have no members, and annotations without members are called identification annotations, such as @Override and @Deprecation in JDK annotations

6. If the annotation has only one member, and the member is named value(), the member name and assignment sign "=" can be omitted when using it, such as @SuppviseWarnings in the JDK annotation; if the member name is not value, it is required when using Specify the member name and assignment number "=",
Insert picture description here
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here

Insert picture description here

Meta annotation

What is a meta-annotation? It is an annotation of an annotation, that is, to add an annotation to an annotation that you define yourself. You define an annotation yourself, but what kind of function do you want your annotation to have, then you need to use the meta-annotation for your Annotations are explained. Java5.0 defines 4 standard meta-annotation types, which are used to provide instructions for other annotation types.

Insert picture description here

1、@Target

@Target explains the scope of the object modified by Annotation: the scope of the annotation, which is used to explain the scope of the annotation (that is, where the annotation can be used, such as class annotations, method annotations, member variable annotations, etc.)

Note: If the Target meta-annotation does not appear, the defined annotation can be applied to any element of the program.

The value is specified in the enumeration java.lang.annotation.ElementType:

● CONSTRUCTOR: used to describe the constructor

● FIELD: used to describe the field

● LOCAL_VARIABLE: used to describe local variables

● METHOD: used to describe the method

● PACKAGE: used to describe the package

● PARAMETER: used to describe parameters

● TYPE: used to describe class, interface (including annotation type) or enum statement

Example code
Insert picture description here

2、@Retention

@Retention defines the length of time the Annotation is retained:

(1) Some Annotations only appear in the source code and are discarded by the compiler;

(2) Others are compiled in the class file; Annotations compiled in the class file may be ignored by the virtual machine,

(3) Others will be read when the class is loaded (please note that it does not affect the execution of the class, because Annotation and class are separated in use).

Using this meta-Annotation can limit the "life cycle" of Annotation.

The value of @Retention is specified in the enumeration RetentionPoicy

● SOURCE: valid in the source file (that is, the source file is retained)

● CLASS: valid in the class file (that is, class retention)

● RUNTIME: valid at runtime (that is, reserved at runtime)

The example code is as follows:

Insert picture description here

Note: The attribute value of the annotated RetentionPolicy is RUTIME, so that the annotation processor can obtain the attribute value of the annotation through reflection, so as to do some logic processing at runtime.

3、@Documented

@Documented is used to describe that other types of annotations should be used as the public API of the annotated program members, so they can be documented by tools such as javadoc. Documented is a mark annotation and has no members.

4、@Inherited

The @Inherited meta-annotation is a mark-up annotation. @Inherited states that an annotated type is inherited. If an annotation type modified with @Inherited is used in a class, this annotation will be used in a subclass of the class.

Note: The @Inherited annotation type is inherited by subclasses of the annotated class. The class does not inherit the annotation from the interface it implements, and the method does not inherit the annotation from the method it overloads.

When the Retention of the annotation marked by the @Inherited annotation is RetentionPolicy.RUNTIME, the reflection API enhances this inheritance. If we use java.lang.reflect to query an annotation of the @Inherited annotation type, the reflection code inspection will start to work: check the class and its parent class until the specified annotation type is found, or reach the top level of the class inheritance structure.

● Definition note 1

Insert picture description here

● Definition Note 2

Insert picture description here

● Define a base class

Insert picture description here

● Define a subclass

Insert picture description here

● Get all the annotations used in the subclass through reflection

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_49543720/article/details/111871095