java元注解@Inherited的使用详解

作者:lele 时间:2023-09-15 04:58:48 

1.先看源码文档


/**
* 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 {
}

上述代码注释部分可以用谷歌翻译 * 大致意思是

表示注释类型自动继承。 如果在注释类型声明中存在继承的元注释,并且用户在类声明上查询注释类型,并且类声明没有此类型的注释,则该类的超类将自动查询注释类型。 将重复此过程,直到找到此类型的注释,或者达到类层次结构(Object)的顶部。 如果没有超类具有此类型的注释,则查询将指示所讨论的类没有这样的注释。

请注意,如果使用注释类型来注释除类之外的任何内容,则此元注释类型不起作用。 还要注意,这个元注释只会导致从超类继承注释; 已实现的接口上的注释无效。

通过上述描述可知,使用该注解的注解父类的子类可以继承父类的注解。

2.代码测试

2.1拥有@Inherited注解


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

String value();
}

@InheritedTest("拥有Inherited")
public class Person {

public void method(){
 }

public void method2(){
 }
}

public class Student extends Person {
}

测试:


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());
   }

}
}

输出:

java元注解@Inherited的使用详解

2.2未拥有@Inherited注解


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface IsNotInherited {
 String value();
}

@IsNotInherited("未拥有Inherited")
public class Person {

public void method(){
 }

public void method2(){
 }
}

public class Student extends Person {
}

测试:


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());
   }

}
}

java元注解@Inherited的使用详解

没有输出任何任容,由此可知未拥有@Inherited注解的注解的类的子类不会被继承该注解。

来源:https://segmentfault.com/a/1190000021123395

标签:java,元注解,@Inherited
0
投稿

猜你喜欢

  • Java使用Redis实现秒杀功能

    2023-04-11 11:26:54
  • Lucene实现索引和查询的实例讲解

    2022-09-06 16:35:30
  • Android11及以上文件读写权限申请详细介绍

    2022-04-14 01:02:42
  • SpringBoot使用AOP与注解实现请求参数自动填充流程详解

    2022-08-18 17:30:36
  • 如何实现自定义SpringBoot的Starter组件

    2023-06-28 14:49:04
  • SpringBoot框架RESTful接口设置跨域允许

    2021-12-31 13:40:19
  • Spring实战之Bean的作用域singleton和prototype用法分析

    2023-03-23 20:48:37
  • 浅谈Java中各种修饰符与访问修饰符的说明

    2022-10-07 00:49:52
  • SpringBoot构建ORM框架的方法步骤

    2023-10-31 20:41:21
  • C#从实体对象集合中导出Excel的代码

    2022-03-15 05:47:54
  • Java8 Stream流多字段求和、汇聚的实例

    2023-03-14 20:17:23
  • java 二分法算法的实例

    2023-04-25 05:04:05
  • C#网站生成静态页面的实例讲解

    2021-11-01 16:33:08
  • Java实例讲解注解的应用

    2021-11-02 01:43:14
  • C#面向对象编程中开闭原则的示例详解

    2022-12-07 11:15:28
  • 完美解决虚拟按键遮盖底部视图的问题

    2021-11-24 23:48:25
  • Spring Boot自定义Starter组件开发实现配置过程

    2022-05-31 16:07:12
  • 详解Android Activity的启动流程

    2023-07-29 08:06:19
  • fastjson转换对象实体@JsonProperty不生效问题及解决

    2023-10-07 00:13:51
  • Java Druid连接池与Apache的DBUtils使用教程

    2021-07-29 13:21:44
  • asp之家 软件编程 m.aspxhome.com