This is the 25th day of my participation in the August Challenge

1. Meta-annotations

Java.lang. Annotation provides four types of meta-annotations for other annotations (you need to use meta-annotations for custom annotations) :

  • @documented – Whether an annotation will be included in JavaDoc
  • @Retention — When is this annotation used
  • @target – Where are annotations used
  • @Inherited – Whether subclasses are allowed to inherit this annotation

(1). @Retention: Defines the life cycle of the annotation

  • Retentionpolicy. SOURCE: discarded at compile time. These annotations are no longer meaningful after compilation, so they are not written to bytecode. @Override and @SuppressWarnings fall into this category.
  • Retentionpolicy. CLASS: Discarded when the CLASS is loaded. Useful in the processing of bytecode files. This is the default for annotations
  • Retentionpolicy.runtime: The annotation is never discarded and is retained at RUNTIME, so it can be read using reflection. Our custom annotations usually use this approach.

(2).target: indicates where the annotation is used. The default value is any element, indicating where the annotation is used. The ElementType parameters available include

  • Elementtype. CONSTRUCTOR: Used to describe the CONSTRUCTOR
  • Elementtype. FIELD: Member variables, objects, attributes (including enum instances)
  • Elementtype. LOCAL_VARIABLE: Used to describe local variables
  • Elementtype. METHOD: Used to describe methods
  • Elementtype. PACKAGE: Used to describe packages
  • Elementtype. PARAMETER: Used to describe parameters
  • Elementtype. TYPE: Used to describe classes, interfaces (including annotation types), or enum declarations

(3).@Documented: A simple Notation indicating whether annotation information is added to a Java document.

@Inherited is a markup annotation. @Inherited explains that an annotated type is Inherited. If an annotation type with the @Inherited annotation is applied to a class, the annotation will be applied to a subclass of that class.

2, custom annotation class written some rules

  1. Annotation type is defined as @interface. All annotations automatically inherit java.lang.Annotation and cannot inherit from other classes or interfaces.
  2. Parameter members can only be qualified with public or default access rights
  3. Parameter members can only use the basic data types byte, short, char, int, long, float, double, and Boolean, and the data types String, Enum, Class, and Annotations, as well as arrays of these types.
  4. To get Annotation information for class methods and fields, you must use Java’s reflection technology to get the Annotation object, because you have no other way to get the Annotation object
  5. Annotations can also not define members, but then annotations are useless

PS: Custom annotations need to use meta-annotations

3, the instance,

AnimalName.java

package com.xiaojian.basics.annotation;
/** * Animal species */
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalName {
    String value(a) default "";
}
Copy the code

AnimalColor.java

package com.xiaojian.basics.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/** * animal color */
@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalColor {
    /** ** color enumeration */
    public enum Color{black, white, gray, motley};Color animalColor(a) defaultWhite Color.;
}
Copy the code

AnimalMaster.java

package com.xiaojian.basics.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/** * Animal owner */

@Target(FIELD)
@Retention(RUNTIME)
@Documented
public @interface AnimalMaster {

    int id(a) default- 1;

    String name(a) default "";

    String address(a) default "";
}
Copy the code

AnimalInfoUtil.java

package com.xiaojian.basics.annotation;

import java.lang.reflect.Field;

/** * returns animal info */
public class AnimalInfoUtil {

    public static void getInfo(Class
        clazz){

        StringBuilder strAnimalName = new StringBuilder( "Animal type:");
        StringBuilder strAnimalColor = new StringBuilder( "Animal color:");
        StringBuilder strAnimalProvider = new StringBuilder( "Owner information:");

        Field[] fields = clazz.getDeclaredFields();

        for(Field field : fields){
            if(field.isAnnotationPresent(AnimalName.class)){
                AnimalName animalAnimal = (AnimalName)field.getAnnotation(AnimalName.class);
                strAnimalName.append(animalAnimal.value());
                System.out.println(strAnimalName);
            } else if(field.isAnnotationPresent(AnimalColor.class)){
                AnimalColor animalColor = (AnimalColor)field.getAnnotation(AnimalColor.class);
                strAnimalColor.append(animalColor.animalColor().toString());
                System.out.println(strAnimalColor);
            } else if(field.isAnnotationPresent(AnimalMaster.class)){
                AnimalMaster animalMaster = (AnimalMaster)field.getAnnotation(AnimalMaster.class);
                strAnimalProvider.append("[Ref. :" + animalMaster.id() + Owner's name: + animalMaster.name() + Address:" + animalMaster.address() + "]"); System.out.println(strAnimalProvider); }}}}Copy the code

Testing:

package com.xiaojian.basics.annotation;

/** * Test class */
public class Test {
    public static void main(String[] args) { AnimalInfoUtil.getInfo(Animal.class); }}Copy the code

Console display:

Animal type: Dog Animal Color: Black Owner info:1, owner name: Xiaoming, address: Hangzhou XXXXXXXX, Zhejiang Province]Copy the code