The use of java meta annotation @ Inherited

Posted by eneyas on Tue, 26 Nov 2019 19:14:09 +0100

1. First look at the source document

/**
 * Indicates that an annotation type is automatically inherited.  If
 * an Inherited meta-annotation is present on an annotation type
 * declaration, and the user queries the annotation type on a class
 * declaration, and the class declaration has no annotation for this type,
 * then the class's superclass will automatically be queried for the
 * annotation type.  This process will be repeated until an annotation for this
 * type is found, or the top of the class hierarchy (Object)
 * is reached.  If no superclass has an annotation for this type, then
 * the query will indicate that the class in question has no such annotation.
 *
 * <p>Note that this meta-annotation type has no effect if the annotated
 * type is used to annotate anything other than a class.  Note also
 * that this meta-annotation only causes annotations to be inherited
 * from superclasses; annotations on implemented interfaces have no
 * effect.
 *
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.3.3 @Inherited
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Inherited {
}

The above code comments can be translated by Google

 Indicates that the annotation type is inherited automatically If there is an inherited meta annotation in the annotation type declaration, and the user queries the annotation type on the class declaration, and the class declaration does not have a annotation of this type, the superclass of the class will automatically query the annotation type This process is repeated until a comment of this type is found, or at the top of the class hierarchy (Object) If no superclass has a comment of this type, the query indicates that the class in question does not have such a comment.

Note that if you use annotation types to annotate anything other than classes, this meta annotation type has no effect Also note that this metacomment only causes comments to be inherited from the superclass; comments on implemented interfaces are not valid.

It can be seen from the above description that the subclass of the annotation parent class using the annotation can inherit the annotation of the parent class.

2. Code test

2.1 have @ Inherited annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface InheritedTest {

    String value();
}

@InheritedTest("Have Inherited")
public class Person {


    public void method(){
    }


    public void method2(){
    }
}
public class Student extends Person {
}

Test:

public class TestInherited {


    public static void main(String[] args) {
        Class<Student> studentClass = Student.class;
        if (studentClass.isAnnotationPresent(InheritedTest.class)){
            System.out.println(studentClass.getAnnotation(InheritedTest.class).value());
        }


    }
}

Output:

2.2 does not have @ Inherited annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotInherited {
    String value();
}
@IsNotInherited("Not owned Inherited")
public class Person {


    public void method(){
    }


    public void method2(){
    }
}
public class Student extends Person {
}

Test:

public class TestInherited {


    public static void main(String[] args) {
        Class<Student> studentClass = Student.class;
        if (studentClass.isAnnotationPresent(IsNotInherited.class)){
            System.out.println(studentClass.getAnnotation(IsNotInherited.class).value());
        }


    }
}

No content is output, so the subclass of a class that does not have an annotation of @ Inherited will not inherit the annotation.

Topics: Java Google