Custom Repeat Annotation

Notes Basic Summary:

  1. Identifier is @interface
  2. Two required @target and @Retention are used to indicate in which scope the comment is added (class or interface TYPE, property FIELD, method METHOD, constructor CONSTRUCTOR, method parameter PARAMETER), and retention is used to indicate in which cycle (compiled SOURCE such as Override and SuppressWarnings, CLASS only, runtime RUNTIME), most of which are at runtime.Not labeling retention defaults to CLASS only.
  3. @Documented: The Javadoc tool will include the annotation information for this annotation markup element in javadoc. (Default annotation information will not be included in Javadoc)
  4. @Target, @Retention, and @Repeatable, known as meta annotation, can modify other annotations.
  5. In the properties of annotation classes, the value property is a common property, which can be directly constants in parentheses, omitting value = ".
  6. Configuration parameters must be constants, including all base types, strings, classes, and enumerations (including corresponding arrays)
# Sample javadoc tools
javadoc -d /Catalog org.xxx.Package Name -encoding utf-8 -charset utf-8

What are duplicate notes

Allows the same comment to be used more than once on the same declaration type (class, property, or method).This annotation was added in jdk 1.8.

For example (custom notes):

@FieldMap(channel = "classroom1", value = "name")
@FieldMap(channel = "classroom2", value = "peopleName")
private String studentName;

This allows you to decide which value to take based on channel s after getting two notes.

step

  1. First, create a general annotation FieldMap.class.
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldMap {
    String channel() default "";
    String value(); // This option is required if default default is not written
}
  1. Add a line @Repeatable(FieldMaps.class).
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(FieldMaps.class)
@Documented
public @interface FieldMap {
    String channel();
    String value();
}
  1. Create a new corresponding package annotation class FieldMaps, Target and Retention to be the same as the package annotation.Also, the property is the wrapped annotation array FieldMap[].Because there is only one array property, the default value() is chosen for the property value, and no default value is added.
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface FieldMaps {
    FieldMap[] value();
}

Use

  1. getAnnotation and getAnnotationsByType, one for a single annotation and one for an array of annotations of the same type;
  2. The @FieldMap annotation is used to determine whether an attribute contains this annotation, but it is actually compiled and wrapped in @FieldMaps;
  • Common Note Usage
if (field.isAnnotationPresent(FieldMap.class)) {
	FieldMap annotation =  field.getAnnotation(FieldMap.class);
    String value = annotation.value();
}
  • Repeat Annotation Use
if (field.isAnnotationPresent(FieldMaps.class)) { // Get FieldMaps here
    FieldMap[] annotationsByType = field.getAnnotationsByType(FieldMap.class);  // Here is the getAnnotationsByType() method, which returns an array
    for (FieldMap annotation : annotationsByType) {
        if ("classroom1".equals(annotation.channel())) { // Get the first one by annotation.channel()
            String value = annotation.value();
            break;   // To prevent identical attribute values, abort the loop directly
        }
    }
}

summary

  1. Annotations are added in jdk 1.5, but they are duplicated and need to be packaged as containers.So the duplicate note introduced by jdk 1.8: @Repeatable is actually a grammatical sugar.
// jdk 1.5
@FieldMaps({@FieldMap("name"), @FieldMap("peopleName")})
private String studentName;
// jdk 1.8
@FieldMap("name")
@FieldMap("peopleName")
private String studentName;

The annotation class FieldMap is annotated by @Repeatable (FieldMaps.class).FieldMaps are containers that hold FieldMaps, and the compiler hides their existence.So the studentName property can be annotated twice by FieldMap.

The reflection API provides a new method: getAnnotationsByType() to return the type of duplicate comment.

Filterable.class.getAnnotation(FieldMaps.class) returns the FieldMaps instance injected by the compiler.

Tags: Java Spring

Posted on Sat, 04 Sep 2021 12:26:42 -0400 by zero-one