Introduction to custom annotations based on Java annotations of annotations

  • 2020-04-01 01:43:46
  • OfStack

To learn more about annotations, we must be able to define our own annotations and use them. Before we can define our own annotations, we must understand the syntax of the meta-annotations and the associated definition annotations provided by Java.

--------------------------------------------------------------------------------

Yuan comments:

Meta-annotations are responsible for annotating other annotations. Java5.0 defines four standard meta-annotation types that are used to provide annotations for other annotation types. Meta annotations defined by Java5.0:
1. @ Target,
2. @ Retention,
3. @ Documented,
4. @ Inherited
These types and the classes they support can be found in the java.lang.annotation package. Let's take a look at what each meta-annotation does and the instructions for using the corresponding sub-parameters.

--------------------------------------------------------------------------------

@ Target:

@target illustrates the scope of the object decorated with an Annotation: annotations can be used for packages, types (classes, interfaces, enumerations, Annotation types), type members (methods, constructors, member variables, enumerated values), method parameters, and local variables (loop variables, catch parameters). The use of target in the declaration of the Annotation type gives more clarity to the target it modifies.

What it does: describes the scope of the annotations (i.e. where they can be used)

Value (ElementType) :

CONSTRUCTOR: describes a CONSTRUCTOR
2.FIELD: used to describe a FIELD
3.LOCAL_VARIABLE: describes local variables
4.METHOD: used to describe a METHOD
5.PACKAGE: describes a PACKAGE
6.PARAMETER: describes a PARAMETER
7.TYPE: describes a class, an interface (including an annotation TYPE), or an enum declaration

Usage examples:


@Target(ElementType.TYPE)
public @interface Table {
    
    public String tableName() default "className";
}
@Target(ElementType.FIELD)
public @interface NoDBColumn {
}

The annotation Table can be used for annotation classes, interfaces (including annotation types), or enum declarations, while the annotation NoDBColumn can only be used for member variables of the annotation class.

--------------------------------------------------------------------------------

@ Retention:

@retention defines how long an Annotation is retained: some annotations appear only in the source code and are dropped by the compiler; Others are compiled in a class file; The annotations compiled in the class file may be ignored by the virtual machine, while others will be read when the class is loaded (note that it does not affect the execution of the class because the annotations are separated from the class in use). Using this meta-annotation limits the "lifecycle" of annotations.

Effect: indicates at what level the annotation information needs to be stored, and describes the lifecycle of the annotation (i.e. what scope the described annotation is valid).

Value (RetentionPoicy) :

1.SOURCE: valid in the SOURCE file (that is, keep the SOURCE file)
2.CLASS: valid in the CLASS file
RUNTIME: valid at RUNTIME (that is, reserved at RUNTIME)

Retention meta - the annotation type has a unique value as a member of its values from Java. Lang. The annotation. RetentionPolicy enumeration values. Specific examples are as follows:


@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Column {
    public String name() default "fieldName";
    public String setFuncName() default "setField";
    public String getFuncName() default "getField"; 
    public boolean defaultDBValue() default false;
}

  The property value of the RetentionPolicy of the Column annotation is RUTIME, so the annotation handler can get the property value of the annotation through reflection, so as to do some logical processing at runtime

--------------------------------------------------------------------------------

@ Documented:

@documented, which describes other types of annotation, should be a public API for annotated program members, and therefore can be Documented by tools such as javadoc. Documented is a tagged annotation with no member.


@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Column {
    public String name() default "fieldName";
    public String setFuncName() default "setField";
    public String getFuncName() default "getField"; 
    public boolean defaultDBValue() default false;
}

@ Inherited:

Inherited meta-annotation is a markup annotation, and @inherited states that a annotated type is Inherited. If an annotation type modified with @inherited is used on a class, the annotation is used on a subclass of that class.

Note: the @inherited annotation type is Inherited from a subclass of the annotated class. A class does not inherit annotations from the interface it implements, and a method does not inherit annotations from the method it overloads.

The reflection API enhances this inheritance when the Retention of annotations annotated with the @inherited annotation type is retentionpolicy.runtime. If we use java.lang.reflect to query an annotation of type @inherited annotation, the reflection code check works by checking the class and its parent classes until the specified annotation type is found, or by reaching the top of the class inheritance structure.

Example code:



@Inherited
public @interface Greeting {
    public enum FontColor{ BULE,RED,GREEN};
    String name();
    FontColor fontColor() default FontColor.GREEN;
}


--------------------------------------------------------------------------------

Custom comments:

When using the @ interface custom annotations, automatic inherited Java. Lang. The annotation. The annotation interface, the compiler automatically other details. When defining annotations, you cannot inherit other annotations or interfaces. @interface is used to declare an annotation, where each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (return value types can only be base, Class, String, enum). Default values for parameters can be declared by default.

Define annotation format:
Public @interface annotation name {definition body}

Support data types for annotation parameters:

1. All basic data types (int, float, Boolean, byte, double, char, long, short)
2. Type String
3. The Class type
4. Enum type
5. The Annotation type
6. Arrays of all types above

How to set the parameters in the Annotation type:
First, you can only use public or default access, for example,String value(); The method is set to the defaul default type;
Second, members can only use basic types of parameters byte, short, char, int, long, float, double, Boolean eight basic data types, and String, Enum, Class, annotations and other data types, and that some types of arrays. For example, a String value (); The parameter member here is String;
Third, if there is only one parameter member, it is best to set the parameter name to "value" followed by parentheses.

Simple custom annotations and use annotation examples:


package annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitName {
    String value() default "";
}


package annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FruitColor {
    
    public enum Color{ BULE,RED,GREEN};

    
    Color fruitColor() default Color.GREEN;
}


package annotation;
import annotation.FruitColor.Color;
public class Apple {

    @FruitName("Apple")
    private String appleName;

    @FruitColor(fruitColor=Color.RED)
    private String appleColor;

    

    
    public void setAppleColor(String appleColor) {
        this.appleColor = appleColor;
    }
    public String getAppleColor() {
        return appleColor;
    }

    
    public void setAppleName(String appleName) {
        this.appleName = appleName;
    }
    public String getAppleName() {
        return appleName;
    }

    public void displayName(){
        System.out.println(" The name of the fruit is: apple ");
    }
}

Annotations are defined and annotated to relevant classes and class attributes when needed. If there is no corresponding annotation information processing process, annotations can be said to be of no practical value. How to make annotations really work is mainly in the annotation processing method. Next, we will implement a simple ORM through annotations to further study annotations and make use of annotations!


Related articles: