java 注解 inherited_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)));

}

}

}

测试结果:

得到以下结论:

类继承关系中@Inherited的作用

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

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

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

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值