java @Inherited注解的作用

看到很多注解都被@Inherited进行了修饰,但是这个@Inherited有什么作用呢?

查看@Inherited代码描述:

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.
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.

翻译后:

指示批注类型是自动继承的。如果在注释类型声明中存在继承的元注释,并且用户在类声明上查询注释类型,并且类声明对该类没有注释,那么该类的超类将自动被查询到注释类型。这个过程将被重复,直到找到这个类型的注释,或者到达类层次结构(对象)的顶端。如果没有超类具有此类的注释,那么查询将表明该类没有此类注释

根据描述进行实例测试:

定义两个注解:@IsInheritedAnnotation 、@NoInherritedAnnotation,其中@IsInheritedAnnotation加了注解@Inherited

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

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface NoInherritedAnnotation {
}

测试类继承关系中@Inherited的作用

@NoInherritedAnnotation
@IsInheritedAnnotation
public class InheritedBase {
}

public class MyInheritedClass extends InheritedBase  {
}

测试接口继承关系中@Inherited的作用

@NoInherritedAnnotation
@IsInheritedAnnotation
public interface IInheritedInterface {
}

public interface IInheritedInterfaceChild extends IInheritedInterface {
}

测试类实现接口关系中@Inherited的作用

public class MyInheritedClassUseInterface implements IInheritedInterface {
}

 

junit测试代码

public class MyInheritedClassTest {

    @Test
    public void testInherited(){
        {
            Annotation[] annotations = MyInheritedClass.class.getAnnotations();
            assertTrue("", Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
            assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
        }
        {
            Annotation[] annotations = MyInheritedClassUseInterface.class.getAnnotations();
            assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
            assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
        }
        {
            Annotation[] annotations = IInheritedInterface.class.getAnnotations();
            assertTrue("", Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
            assertTrue("", Arrays.stream(annotations).anyMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
        }
        {
            Annotation[] annotations = IInheritedInterfaceChild.class.getAnnotations();
            assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(IsInheritedAnnotation.class)));
            assertTrue("", Arrays.stream(annotations).noneMatch(l -> l.annotationType().equals(NoInherritedAnnotation.class)));
        }
    }

}

测试结果:

java @Inherited注解的作用_第1张图片

得到以下结论:

类继承关系中@Inherited的作用

类继承关系中,子类会继承父类使用的注解中被@Inherited修饰的注解

接口继承关系中@Inherited的作用

接口继承关系中,子接口不会继承父接口中的任何注解,不管父接口中使用的注解有没有被@Inherited修饰

类实现接口关系中@Inherited的作用

类实现接口时不会继承任何接口中定义的注解

你可能感兴趣的:(java)