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学习 本文目的:项目开发过程中遇到自定义注解,想要弄清楚其原理,但是自己的基础知识不足以支撑自己去探索此问题,所以先记录问题,然后补充基础知识,然后解决其问题.记录此 ...
随机推荐
- MeteoInfoLab脚本示例:Maskout图形
Maskout通常有两种类型:Maskout图形和Maskout数据.这里是Maskout图形的示例.需要用shaperead读取地图数据形成图层作为Maskout图层(这里是中国的行政区域china ...
- linux centos 05
centos7安装mariadb 1.yum 得配置yum源,配置阿里云的 两个 yum源 ,阿里云的yum源中,会有 mariadb的软件包 阿里云的yum仓库中,mariadb版本如下 mar ...
- centOS7永久关闭防火墙(防火墙的基本使用(转)
查看防火墙状态: systemctl status firewalld.service 如图 绿的running表示防火墙开启 执行关闭命令: systemctl stop firewalld.ser ...
- JavaSE学习笔记05面向对象编程01
面向对象编程01 java的核心思想就是OOP 面向过程&面向对象 面向过程思想: 步骤清晰简单,第一步做什么,第二步做什么...... 面向过程适合处理一些较为简单的问题 面向对象思想: 物 ...
- 总结2020最新50道Python面试题集锦(附答案)
Python是目前编程领域最受欢迎的语言.在本文中,我将总结Python面试中最常见的50个问题.每道题都提供参考答案,希望能够帮助你在2019年求职面试中脱颖而出,找到一份高薪工作.这些面试题涉及P ...
- java 封装多态继承
java 面向对象思想 封装多态继承 面向过程与面向对象 编程分为面向对象编程和面向对象编程,汇编,C语言是面向过程写代码,C++/Java是面向对象 其实面向过程和面向对象在本质都是一样写代码,然后 ...
- Ignite、Vertx
Ignite IpFinder 默认采用multicast的ip发现方式 优点: 集群较小时,配置方便 缺点 集群较大100s-1000s时,广播非常耗时,此时建议使用ZooKeeper发现机制(Zo ...
- Go之NSQ简介,原理和使用
NSQ简介 NSQ是Go语言编写的一个开源的实时分布式内存消息队列,其性能十分优异. NSQ 是实时的分布式消息处理平台,其设计的目的是用来大规模地处理每天数以十亿计级别的消息.它具有分布式和去中心化 ...
- templatedcodegenerator 简单模板化代码生成器
传送门:https://github.com/uniqss/templatedcodegenerator templatedcodegenerator code generator with spec ...
- 学会这些CSS,再也不用切图!!!
三角形 利用border-color支持transparent这一特性,隐藏三条边框,实现三角形. <style> .triangle { width: 0; height: 0; bor ...