JAVA注解的继承性
摘要
本文从三个方面介绍java注解的**“继承性”**:
- 基于元注解@Inherited,类上注解的继承性
- 基于类的继承,方法/属性上注解的继承性
- 基于接口的继承/实现,方法/属性上注解的继承性
一、基于@Inherited
首先元注解@Inherited
作为一个元注解,只能修饰其他注解类型(由@Target(ElementType.ANNOTATION_TYPE)
决定)。
所谓的基于@Inherited的继承性,指的是@Inherited修饰的其他注解修饰类时,这个类的子类是否可以继承到父类的注解;主角是@Inherited修饰的其他注解,而不是@Inherited本身。
JDK中@Inherited的说明文档很清楚的阐述了继承性:
当用户在一个程序元素类上,使用AnnotatedElement的相关注解查询方法,查询元注解Inherited修饰的其他注解类型A时,如果这个类本身并没有被注解A修饰,那么会自动查询这个类的父类是否被注解A修饰。查询过程会沿着类继承链一直向上查找,直到注解A被找到,或者到达继承链顶层(Object)。
如果元注解Inherited修饰的其他注解,修饰了除类之外的其他程序元素,那么继承性将会失效。
下面的以demo说明:
public class ClassInheritedTest {
@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
@Inherited // 声明注解具有继承性
@interface AInherited {
String value() default "";
}
@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
@Inherited // 声明注解具有继承性
@interface BInherited {
String value() default "";
}
@Target(value = ElementType.TYPE)
@Retention(value = RetentionPolicy.RUNTIME)
// 未声明注解具有继承性
@interface CInherited {
String value() default "";
}
@AInherited("父类的AInherited")
@BInherited("父类的BInherited")
@CInherited("父类的CInherited")
class SuperClass {
}
@BInherited("子类的BInherited")
class ChildClass extends SuperClass {
}
public static void main(String[] args) {
Annotation[] annotations = ChildClass.class.getAnnotations();
System.out.println(Arrays.toString(annotations));
// output: [@annotations.InheritedTest1$AInherited(value=父类的AInherited), @annotations.InheritedTest1$BInherited(value=子类的BInherited)]
}
}
说明:
- 自定义注解
@CInherited
没有被@Inherited 修饰,不具备继承性,子类ChildClass
获取类上的注解时,没有该注解; - 自定义注解
@BInherited
,具备继承性,但是子类ChildClass在类上自行指定了与父类相同类型的注解@BInherited,那么子类获取其类注解时,@BInherited为子类自己声明的; - 自定义注解
@AInherited
,具备继承性,子类上未指定相同注解,子类获取注解时,成功获取到父类上的@AInherited注解。
二、基于类继承
属性和方法注解的继承,与类注解的继承完全不同,与元注解Inherited毫无关系,忠实于方法/属性本身的继承。
以下示例说明属性/方法注解的继承:
public class InheritedTest {
@Target(value = {ElementType.METHOD, ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
@interface DESC {
String value() default "";
}
class SuperClass {
@DESC("父类方法foo")
public void foo() {}
@DESC("父类方法bar")
public void bar(){}
@DESC("父类的属性")
public String field;
}
class ChildClass extends SuperClass {
@Override
public void foo() {
super.foo();
}
}
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
Method foo = ChildClass.class.getMethod("foo");
System.out.println(Arrays.toString(foo.getAnnotations()));
// output: []
// 子类ChildClass重写了父类方法foo,并且@Override注解只在源码阶段保留,所以没有任何注解
Method bar = ChildClass.class.getMethod("bar");
System.out.println(Arrays.toString(bar.getAnnotations()));
// output: [@annotations.InheritedTest$DESC(value=父类方法bar)]
// bar方法未被子类重写,从父类继承到了原本注解
Field field = ChildClass.class.getField("field");
System.out.println(Arrays.toString(field.getAnnotations()));
}
// output: [@annotations.InheritedTest$DESC(value=父类的属性)]
// 解释同上
三、基于接口继承/实现
基于接口的继承/实现中,属性和方法注解的继承大体与类相似。jdk7以前接口的方法都需要实现,所以子类中的方法永远也无法获得父接口方法的注解,但是jdk8以后的默认方法打开了这种限制。
以下以demo说明:
public class IterInheritedTest {
@Target(value = {ElementType.METHOD, ElementType.FIELD})
@Retention(value = RetentionPolicy.RUNTIME)
@interface DESC {
String value() default "";
}
interface SuperInterface {
@DESC("父接口的属性")
String field = "field";
@DESC("父接口方法foo")
public void foo();
@DESC("父接口方法bar")
default public void bar() {
}
}
interface ChildInterface extends SuperInterface {
@DESC("子接口方法foo")
@Override
void foo();
}
class ChildClass implements SuperInterface {
@DESC("子类的属性")
public String field = "field";
@Override
public void foo() {
}
}
public static void main(String[] args) throws NoSuchMethodException, NoSuchFieldException {
Method iFoo = ChildInterface.class.getMethod("foo");
System.out.println(Arrays.toString(iFoo.getAnnotations()));
// output: [@annotations.IterInheritedTest$DESC(value=子接口方法foo)]
Method iBar = ChildInterface.class.getMethod("bar");
System.out.println(Arrays.toString(iBar.getAnnotations()));
// output: [@annotations.IterInheritedTest$DESC(value=父接口方法bar)]
Field iField = ChildInterface.class.getField("field");
System.out.println(Arrays.toString(iField.getAnnotations()));
// output: [@annotations.IterInheritedTest$DESC(value=父接口的属性)]
Method foo = ChildClass.class.getMethod("foo");
System.out.println(Arrays.toString(foo.getAnnotations()));
// output: []; 被子类覆盖
Method bar = ChildClass.class.getMethod("bar");
System.out.println(Arrays.toString(bar.getAnnotations()));
// output: [@annotations.IterInheritedTest$DESC(value=父接口方法bar)]
Field field = ChildClass.class.getField("field");
System.out.println(Arrays.toString(field.getAnnotations()));
// output: [@annotations.IterInheritedTest$DESC(value=子类的属性)]
// 是子类作用域下的属性`field`
}
}
总结
@Inherited 修饰的注解,继承性只体现在对类的修饰上;
方法和属性上注解的继承,忠实于方法/属性继承本身,客观反映方法/属性上的注解。
JAVA注解的继承性的更多相关文章
- Java注解实践
Java注解实践 标签 : Java基础 注解对代码的语意没有直接影响, 他们只负责提供信息给相关的程序使用. 注解永远不会改变被注解代码的含义, 但可以通过工具对被注解的代码进行特殊处理. JDK ...
- Java - 注解 (Annotation)
Java - 注解 (Annotation) 一.基本的 Annotation > 使用 Annotation 时要在其前面增加 @符号,并把该 Annotation 当成一个修饰符 ...
- 认识下java注解的实现原理
1,什么是注解 注解也叫元数据,例如常见的@Override和@Deprecated,注解是JDK1.5版本开始引入的一个特性,用于对代码进行说明,可以对包.类.接口.字段.方法参数.局部变量等进行注 ...
- Java注解Annotation学习笔记
一.自定义注解 1. 使用关键字 @interface 2. 默认情况下,注解可以修饰 类.方法.接口等. 3. 如下为一个基本的注解例子: //注解中的成员变量非常像定义接口 public @int ...
- Java 注解概要
转载自:https://www.cnblogs.com/peida/archive/2013/04/24/3036689.html(Java注解就跟C#的特性是一样的) 要深入学习注解,我们就必须能定 ...
- java注解使用总结
2005年,sun公司推出了jdk1.5,同时推出的注解功能吸引了很多人的目光,使用注解编写代码,能够减轻java程序员繁琐配置的痛苦. 使用注解可以编写出更加易于维护,bug更少的代码. 注解是什么 ...
- [转帖]java注解核心知识总结
java注解核心知识总结 2019-11-01 20:39:50 从事Java 阅读数 2 收藏 文章标签: java注解总结程序员 更多 分类专栏: java 注解 版权声明:本文为博主原创文 ...
- 深入JAVA注解-Annotation(学习过程)
JAVA注解-Annotation学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...
- 深入学习JAVA注解-Annotation(学习过程)
JAVA注解-Annotation学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...
随机推荐
- RLP序列化算法
RLP RLP(Recursive Length Prefix)递归长度前缀编码,是由以太坊提出的序列化/反序列化标准,相比json格式体积更小,相比protobuf对多语言的支持更强. RLP将数据 ...
- 判断ip地址是属于国内还是国外
一,如何判断一个ip地址是否属于国内? 我们以前使用淘宝提供的一个api地址进行判断,但经常出现打不开的报错, 因为只需要判断是国内或国外,于是考虑自己搞一个简单的. 分配给国内的ip地址在apnic ...
- springboot入门系列(四):SpringBoot和Mybatis配置多数据源连接多个数据库
SpringBoot和Mybatis配置多数据源连接多个数据库 目前业界操作数据库的框架一般是 Mybatis,但在很多业务场景下,我们需要在一个工程里配置多个数据源来实现业务逻辑.在SpringBo ...
- MongoDB添加认证
创建用户管理员 在admin数据库中,添加具有该userAdminAnyDatabase角色的用户 .根据需要为此用户添加其他角色. 注意:创建用户的数据库(在此示例中为 admin)是用户的身份验证 ...
- Linux配置和管理设备映射多路径multipath
(一)多路径管理软件的由来 在企业中,服务器与存储通常是分开放置的,服务器上的硬盘通常用来安装操作系统和应用软件,业务数据则是存储在单独的存储设备上,那么,服务器与存储是如何连接的呢?根据存储协议,经 ...
- 【总结】spring基础
一.spring 1.spring体系结构 (1)核心容器(core container):由spring-core,spring-beans,spring-context和spring-expres ...
- printk 流程分析
1. 概述 printk 用于在终端上打印内核想要输出的信息,平常我们较多使用的打印函数是 printf,两者名字虽然只有最后一个字母不同,且都是为了在终端上显示信息,但是它们的应用场景并不相同.pr ...
- OpenCascade拓扑对象之:拓扑对象的层次结构
@font-face { font-family: "Times New Roman" } @font-face { font-family: "宋体" } @ ...
- python数据类型之tuple(元组)
tuple元组 关注公众号"轻松学编程"了解更多. 1.概述 本质上是一种有序的集合,和列表非常的相似,列表使用[]表示,元组使用**()**表示. 特点:一旦初始化,就不能发生改 ...
- [Luogu P2257] YY的GCD (莫比乌斯函数)
题面 传送门:洛咕 Solution 推到自闭,我好菜啊 显然,这题让我们求: \(\large \sum_{i=1}^{n}\sum_{j=1}^{m}[gcd(i,j)\in prime]\) 根 ...