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学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...
随机推荐
- 如何给LG gram写一个Linux下的驱动?
其实就是实现一下几个Fn键的功能,没有标题吹得那么牛. 不知道为啥,LG gram这本子意外的小众. 就因为这个,装Linux遇到的硬件问题就没法在网上直接搜到解决办法了. Fn + F9 实现阅读模 ...
- day34 Pyhton 网络编程
一今日内容 # 函数 # 面向对象 # 进阶 # 网络编程 4 # 并发编程 6-7 # 概念 # 网络基础 # 局域网的概念 # 交换机和路由器的工作流程 # ip地址 # mac地址 # 子网掩码 ...
- openresty上安装waf
一,查看openresty的当前版本 [root@localhost luacache]# /usr/local/openresty/bin/openresty -V nginx version: o ...
- sql中的Bulk 导入txt文本
通常,我们会对于一个文本文件数据导入到数据库中,不多说,上代码. 首先,表结构如下. 其次,在我当前D盘中有个文本文件名为2.txt的文件. 在数据库中,可以这样通过一句代码插入. Bulk in ...
- Cypress系列(67)- 环境变量设置指南
如果想从头学起Cypress,可以看下面的系列文章哦 https://www.cnblogs.com/poloyy/category/1768839.html 常见的环境变量设置方式 可参考这篇文章: ...
- <audio> 标签
<audio> 标签定义声音,比如音乐或其他音频流. 实例 一段简单的 HTML 5 音频:
- centos7源码编译安装LNMP+ZABBIX4.0LTS(1)——nginx
环境:192.168.117.132--zabbix server192.168.117.133--zabbix proxy 安装路径为/zabbix 安装nginx 1.安装包下载http://ng ...
- 解决Java连接Oracle 12c存在的问题
感谢作者 原文链接:https://blog.csdn.net/peng_wei_kang/article/details/80403486 1.发现项目报以下错误: Caused by: jav ...
- 虚拟机之kali2.0
kali2.0下载地址: 链接: https://pan.baidu.com/s/1CiQJNfvUPFw3aJR103XSbg 提取码: 57wp
- git 移除远程仓库关联
1 git remote rm origin // 移除本地关联 2 git remote add origin git@github.com/example.git // 添加线上仓库 3 git ...