Definition of annotations

A simple example

public @interface Ano01 {
    String name(a) default "xiaoming";
    int age(a)default 18;
    String[] schools() default {"primary school"."high school"};
}
Copy the code

It’s just like defining an interface, but it’s at sign interface, one more at sign than interface

The parameter definition is similar to the function definition, followed by default to set the default value

Special attribute Value

  1. When there is only one attribute in the annotation and the name is value, assigning a value to the value attribute when using the annotation can directly assign the value to the attribute, whether the value is a single-valued element or an array type.

    // Define the annotation Book
    public @interface Book {
        / / title
        String value(a);
    }
    
    // Use the annotation Book
    public class BookShelf {
        @book (journey to the West)
        public void showBook(a){}} orpublic class BookShelf {
        @book (value=" journey to the West ")
        public void showBook(a){}}Copy the code
  2. If there are other attributes in the annotation besides the value attribute, and at least one attribute does not have a default value, the value attribute name cannot be omitted when assigning a value to the attribute using the annotation.

    // Define the annotation Book
    public @interface Book {
        / / title
        String value(a);
        / / price
        double price(a) default 100;
        // Multiple authors
        String[] authors();
    }
    // Use the Book annotation: the correct way
    @book (value=" authors ",authors =" caoxueqin ")
    public class BookShelf {
      // Use the Book annotation: the correct way
        @book (value=" rotator ",authors = {" rotator "," rotator "})
        public void showBook(a){}}// Use the Book annotation: wrong way
    public class BookShelf {
        @book (" Journey to the West ",authors = {" wu C 'en "," Bethune "})
        public void showBook(a){}}// The value attribute name cannot be omitted.
    Copy the code

Annotation attribute types can have the types listed below

  1. Basic data types
  2. String
  3. Enumerated type
  4. Annotation type
  5. The Class type
  6. A one-dimensional array type of the above type

Yuan notes

Meta-annotations are the annotations that are responsible for annotating annotations

@target and @Retention are here to modify @overide

@Target

Used to describe where annotations can be annotated, functions, classes, parameters, and so on

The optional parameter values in the enumeration class ElemenetType include:

  • TYPE: used on classes, interfaces
  • FIELD: Used on member variables
  • METHOD: Used in methods
  • PARAMETER: used in parameters
  • CONSTRUCTOR: used in constructing methods
  • LOCAL_VARIABLE: used for local variables

@Retention

It indicates whether the annotation existence phase is reserved for source code (compile time), bytecode (classload), or runtime (running in the JVM)

Optional parameter values are included in the enumeration type RetentionPolicy

  • SOURCE: Annotations exist only in the Java SOURCE code, not in the bytecode files generated by compilation.
  • CLASS: Annotations exist in Java source code, compiled bytecode files, not in memory at runtime, default.
  • RUNTIME: Annotations exist in Java source code, in compiled bytecode files, and in RUNTIME memory, and can be retrieved by programs through reflection.

@Document

Related to the Javadoc

@Inherited

A @inherited annotation modifs a parent class. If a child class is not modified by another annotation, its child class inherits the parent’s annotations.

@Repeatable

Note that this annotation can be used multiple times in one place

Built-in annotations

annotations role Matters needing attention
@Override It is used to describe the current method as an overridden method, which is checked at compile time In JDK1.5 it can only describe overrides in inheritance. In JDK1.6 it can describe overrides in interface implementations as well as classes
@Deprecated It is used to describe the current method as an outdated method There is no
@SuppressWarnings Remove warnings from programs. There is no

Get annotation information

public class Test06 {
    public static void main(String[] args) throws Exception {
        Class cPerson = Person.class;
        Annotation[] annotations = cPerson.getAnnotations();
        for (Annotation annotation : annotations){
            System.out.println("Loop:"+annotation);
        }
        Annotation myTable = cPerson.getAnnotation(MyTable.class);
        System.out.println(myTable);
        System.out.println(((MyTable)myTable).value());

        Field name = cPerson.getDeclaredField("name"); MyField nameAnnotation = name.getAnnotation(MyField.class); System.out.println(nameAnnotation); System.out.println(nameAnnotation.column()); System.out.println(nameAnnotation.type()); System.out.println(nameAnnotation.length()); }}@MyTable("person_table")
class Person{
    @MyField(column = "col_name",type = "char",length = 20)
    private String name;
    @MyField(column = "col_age",type = "int",length = 20)
    private int age;

    public Person(a) {}public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName(a) {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge(a) {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString(a) {
        return "Person{" +
                "name='" + name + '\' ' +
                ", age=" + age +
                '} '; }}@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface MyTable{
    String value(a);
}

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@interface MyField{
    String column(a);
    String type(a);
    int length(a);
}
Copy the code

Cycle: @com.kehao.MyTable(value=person_table) @com.kehao.MyTable(value=person_table) person_table @com.kehao.MyField(column=col_name, type=char, length=20) col_name char 20