Java implementation of custom annotation development

First, let's take a look at the standard example of custom annotation. The annotation class is decorated with the @ interface keyword, and the annotation related information is declared above the annotation class, including the following four types of information

@Documented – will annotations be included in JavaDoc
@Retention – when to use this annotation
@Target – where are annotations used
@Inherited – whether subclasses are allowed to inherit the annotation

1.) @ Retention – defines the life cycle of the annotation

● RetentionPolicy.SOURCE: discarded at compile time. These annotations no longer have any meaning after compilation, so they are not written to bytecode@ Override and @ suppresswarnings belong to this type of annotation.
● RetentionPolicy.CLASS: discarded when the class is loaded. It is useful in the processing of bytecode files. Annotations use this method by default
● RetentionPolicy.RUNTIME: the annotation will never be discarded, and the annotation will be retained during the runtime. Therefore, the information of the annotation can be read using the reflection mechanism. Our custom annotations usually use this method.

2.) Target – indicates where the annotation is used. The default value is any element, indicating where the annotation is used. Available ElementType parameters 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 method
● ElementType.PACKAGE: used to describe the package
● ElementType.PARAMETER: used to describe parameters
● ElementType.TYPE: used to describe class, interface (including annotation type) or enum declaration

3.)@Documented – a simple Annotations tag annotation that indicates whether annotation information is added to the java document.
4.)@Inherited – defines the relationship between the annotation and the subclass
The @ Inherited meta annotation is a tag annotation, @ Inherited describes that a marked type is Inherited.
If an annotation type decorated with @ Inherited is used in a class, the annotation will be used in the subclass of the class.
Use of custom annotation classes

@Target(value={ElementType.TYPE,ElementType.METHOD,ElementType.FIELD}) is used to declare that the scope of the current annotation class is the class method attribute
@When Retention(value = RetentionPolicy.RUNTIME) is running, the annotation is reserved, and the annotation information can be read through reflection

package com.gaunyi.batteryonline.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 * Created by S0111 on 2019/8/20.
 * Custom annotation class declaration
@Target(value= {ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
public @interface MyAnnotationDefinition {
  /*Define the parameter information in the annotation*/
    String name();
    String value();
    String path();

Read annotation information (test annotation class)
Here, the annotation information is read through reflection, and the annotation content corresponds to the corresponding class, method and attribute.

package com.gaunyi.batteryonline.annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
 * Created by S0111 on 2019/8/20.
 * Custom annotation class test
public class MyAnnotationTest {
    public static void  main(String[] args) throws Exception{
        Class clazz = Class.forName("com.gaunyi.batteryonline.annotation.MyAnnotationUse");
        //Get class annotation information
        MyAnnotationDefinition classAnno =(MyAnnotationDefinition) clazz.getAnnotation(MyAnnotationDefinition.class);
        //Get all method annotation information ps: here, you need to use isAnnotationPresent to judge whether annotations are used on the method
        Method[] allMethods = clazz.getDeclaredMethods();
        for(int i=0;i<allMethods.length;i++){
            if(allMethods[i].isAnnotationPresent(MyAnnotationDefinition.class)) {
                MyAnnotationDefinition methodAnno = allMethods[i].getAnnotation(MyAnnotationDefinition.class);
                System.out.println("ergodic:The current method name is:"+allMethods[i].getName()+" Annotation information for:---" + "---" + methodAnno.value() + "---" + methodAnno.path());
        //Gets the annotation information of the specified method
       Method methodTest = clazz.getDeclaredMethod("testAnno");
        MyAnnotationDefinition methodAnnotest =  methodTest.getAnnotation(MyAnnotationDefinition.class);
        //Get attribute annotation information
        Field nameField =  clazz.getDeclaredField("name");
        MyAnnotationDefinition attrAnno = nameField.getAnnotation(MyAnnotationDefinition.class);

Custom annotation usage
Use annotation information on classes, methods and attributes respectively

package com.gaunyi.batteryonline.annotation;
 * Created by S0111 on 2019/8/20.
 * Custom annotation class usage
@MyAnnotationDefinition(name="Class name",value="Class value",path="Classpath")
public class MyAnnotationUse {
    @MyAnnotationDefinition(name="Attribute name",value="Attribute value",path="Attribute path")
    private String name;
    private String age;
    @MyAnnotationDefinition(name="Method name",value="Method value",path="Method access path")
    public String testAnno(){
        return "successs!!!";
    @MyAnnotationDefinition(name="Method name 1",value="Method value 1",path="Method access path 1")
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public String getAge() {
        return age;
    public void setAge(String age) {
        this.age = age;

ok! function

Tags: Java reflection

Posted by rcity on Mon, 23 May 2022 18:30:40 +0300