The use of Java meta annotation @ inherited

Time:2020-11-10

1. Look at the source document first

/**
 * 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 code comments above can be translated by Google

Indicates that annotation types are automatically inherited. 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 this type of annotation, the superclass of the class will query the annotation type automatically. This process is repeated until the annotation of this type is found or until the top of the class hierarchy (object) is reached. 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 an annotation type to annotate anything except a class, this meta annotation type has no effect. Also note that this meta annotation only causes annotations to be inherited from the superclass; annotations on implemented interfaces are invalid.

From the above description, it can be seen that the child class of the parent class can inherit the annotation of the parent class by using the annotation of the annotation.

2. Code testing

2.1 have @ inherited annotation

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

    String value();
}

@Inherited test ("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(InheritedTest.class)){
            System.out.println(studentClass.getAnnotation(InheritedTest.class).value());
        }


    }
}

Output:
The use of Java meta annotation @ inherited

2.2 does not have @ inherited annotation

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotInherited {
    String value();
}
@Isnothinherited ("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());
        }


    }
}

The use of Java meta annotation @ inherited

No content is output, so it can be seen that subclasses of a class that does not have an annotation with the @ inherited annotation will not inherit the annotation.

Recommended Today

The course of using Chinese software of poedit Pro

Poedit pro (formerly known as poedit) is a free (professional version charge), open source and cross platform gettext class (. Po format, gettext is used in the application program for program internationalization) International Translation editor. It is also one of the most widely used software of the same type. At present, it is available in […]