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学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...
随机推荐
- 不要以为Bug写的好就是好程序员,其实这只占不到15%!
最近和一位从事多年架构工作的技术哥们见面,聊到了近期面试程序员的一些经历,谈到了"如何判断程序员水平高低"这个话题,颇有些感触,觉得有价值,因此花了些时间整理.分享给大家. 正 ...
- 【原创】xenomai内核解析--xenomai与普通linux进程之间通讯XDDP(二)--实时与非实时关联(bind流程)
版权声明:本文为本文为博主原创文章,转载请注明出处.如有问题,欢迎指正.博客地址:https://www.cnblogs.com/wsg1100/ 1.概述 上篇文章介绍了实时端socket创建和配置 ...
- CentOS8平台php日志的定时切分
一,编写bash脚本: [root@yjweb crontab]# vi split_php_logs.sh 代码: #!/bin/bash # 备份php/php-fpm的日志 # 昨天的日期 fi ...
- git 撤销push到远程仓库的无用commit
一 回退代码 git reset <版本号> --soft // 软回退 - 所有的commit修改都被撤销了,且修改的代码统一撤回到暂存区 git reset <版本号> - ...
- JavaSE学习笔记03流程控制
Java流程控制 1.顺序结构 java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句往下执行. 顺序结构是最简单的算法结构,它是任何一个算法都离不开的一种基本算法结构. 2. 选择结构 ...
- 19. [链表][双指针]删除链表的倒数第N个节点
19. 删除链表的倒数第N个节点 方法一:哨兵节点+快慢指针 在本题中,快慢指针的用法为:让快指针先走几步,步数由 \(n\) 决定. 使用哨兵节点的理由是为了避免删除节点为头结点引发的空指针异常. ...
- HashMap 中的哈希值计算问题
date: 2020-08-21 16:48:00 updated: 2020-08-21 16:52:00 HashMap 中的哈希值计算问题 1. hash 计算 JDK1.8 HashMap源码 ...
- 论文解读《Learning Deep CNN Denoiser Prior for Image Restoration》
CVPR2017的一篇论文 Learning Deep CNN Denoiser Prior for Image Restoration: 一般的,image restoration(IR)任务旨在从 ...
- Linux部署常用命令
1../startup.sh 开启tomcat服务 2.rm -f file1 删除文件 3.rmdir dir xxx 删除目录 4.Tab按钮 自动补全文件名称功能 5.gzip file1 ...
- VMware Workstation Pro 虚拟机安装CentOS-7
一.下载CentOS-7镜像 我是通过阿里开源镜像站下载的, 下载url:https://mirrors.aliyun.com/centos/7/isos/x86_64/ 下载CentOS-7-x86 ...