- 浏览: 229486 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (102)
- 开源软件 (1)
- 并发 (14)
- WEB (1)
- NIO (4)
- Socket (5)
- 应用服务器 (4)
- 集群 (0)
- 数据库 (1)
- JAVA基础 (17)
- 开源框架 (2)
- 业务知识 (1)
- JVM (9)
- Windows (1)
- LINUX (0)
- Jquery (0)
- JMS (0)
- Cache (0)
- Oracle (5)
- XML (0)
- EJB (0)
- WebService (0)
- Struts2 (1)
- Hibernate (1)
- Spring (0)
- 设计模式 (4)
- UML (0)
- JS (12)
- 网络爬虫 (0)
- 数据结构与算法 (1)
- EXT (1)
- DIV+CSS (2)
- 安全 (3)
- Android (9)
- LDAP (1)
- Mybatis (1)
最新评论
-
Dom_4j:
...
理解注解中的@Inherited -
s469799470:
demo少个ID
iframe父子页面交互问题 -
errorerror0:
...
iframe父子页面交互问题 -
errorerror0:
iframe父子页面交互问题 -
johnawm:
2012-12-18 wangshibei 写道CountD ...
CountDownLatch的使用
@Inherited:
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。
如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
注意:@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,
方法并不从它所重载的方法继承annotation。
当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。
如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:
检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。
看下面的例子:
分析下这段代码,这里定义了两个annotion,其中ATable使用了@Inherited, BTable没有使用
@Inherited,类Super和类Sub分别使用了ATable和BTable这两个注解,并且Sub类 继承Super类。
这段程序的运行结果如下:
getFields()获得某个类的所有的公共(public)的字段,包括父类。
getDeclaredFields()获得某个类的所有申明的字段,即包括public、private和proteced,
但是不包括父类的申明字段。 同样类似的还有getConstructors()和getDeclaredConstructors(),
getMethods()和getDeclaredMethods()。
因此:Field的打印好理解,因为sub是super类的子类,会继承super的类
同样method和constructor的打印也是如此。
clazz.getAnnotations()可以打印出当前类的注解和父类的注解
clazz.getDeclaredAnnotations()只会打印出当前类的注解
如果注解ATable把@Inherit去掉。那么后面四行的输出结果为:
无法获取到@ATable的注解,
也就是说注解和普通类的区别是如果一个子类想获取到父类上的注解信息,
那么必须在父类上使用的注解上面 加上@Inherit关键字
@Inherited 元注解是一个标记注解,@Inherited阐述了某个被标注的类型是被继承的。
如果一个使用了@Inherited修饰的annotation类型被用于一个class,则这个annotation将被用于该class的子类。
注意:@Inherited annotation类型是被标注过的class的子类所继承。类并不从它所实现的接口继承annotation,
方法并不从它所重载的方法继承annotation。
当@Inherited annotation类型标注的annotation的Retention是RetentionPolicy.RUNTIME,则反射API增强了这种继承性。
如果我们使用java.lang.reflect去查询一个@Inherited annotation类型的annotation时,反射代码检查将展开工作:
检查class和其父类,直到发现指定的annotation类型被发现,或者到达类继承结构的顶层。
看下面的例子:
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Inherited public @interface ATable { public String name() default ""; } @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface BTable { public String name() default ""; } @ATable public class Super { private int superx; public int supery; public Super() { } private int superX(){ return 0; } public int superY(){ return 0; } } @BTable public class Sub extends Super{ private int subx; public int suby; private Sub() { } public Sub(int i){ } private int subX(){ return 0; } public int subY(){ return 0; } } public class TestMain { public static void main(String[] args) { Class<Sub> clazz = Sub.class; System.out.println("============================Field==========================="); System.out.println(Arrays.toString(clazz.getFields())); System.out.println(Arrays.toString(clazz.getDeclaredFields())); //all + 自身 System.out.println("============================Method==========================="); System.out.println(Arrays.toString(clazz.getMethods())); //public + 继承 //all + 自身 System.out.println(Arrays.toString(clazz.getDeclaredMethods())); System.out.println("============================Constructor==========================="); System.out.println(Arrays.toString(clazz.getConstructors())); System.out.println(Arrays.toString(clazz.getDeclaredConstructors())); System.out.println("============================AnnotatedElement==========================="); //注解DBTable2是否存在于元素上 System.out.println(clazz.isAnnotationPresent(BTable.class)); //如果存在该元素的指定类型的注释DBTable2,则返回这些注释,否则返回 null。 System.out.println(clazz.getAnnotation(BTable.class)); //继承 System.out.println(Arrays.toString(clazz.getAnnotations())); System.out.println(Arrays.toString(clazz.getDeclaredAnnotations())); ////自身 } }
分析下这段代码,这里定义了两个annotion,其中ATable使用了@Inherited, BTable没有使用
@Inherited,类Super和类Sub分别使用了ATable和BTable这两个注解,并且Sub类 继承Super类。
这段程序的运行结果如下:
============================Field=========================== [public int annotion.inherit.Sub.suby, public int annotion.inherit.Super.supery] [private int annotion.inherit.Sub.subx, public int annotion.inherit.Sub.suby] ============================Method=========================== [public int annotion.inherit.Sub.subY(), public int annotion.inherit.Super.superY(), public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll()] [private int annotion.inherit.Sub.subX(), public int annotion.inherit.Sub.subY()] ============================Constructor=========================== [public annotion.inherit.Sub(int)] [private annotion.inherit.Sub(), public annotion.inherit.Sub(int)] ============================AnnotatedElement=========================== true @annotion.inherit.BTable(name=) [@annotion.inherit.ATable(name=), @annotion.inherit.BTable(name=)] [@annotion.inherit.BTable(name=)]
getFields()获得某个类的所有的公共(public)的字段,包括父类。
getDeclaredFields()获得某个类的所有申明的字段,即包括public、private和proteced,
但是不包括父类的申明字段。 同样类似的还有getConstructors()和getDeclaredConstructors(),
getMethods()和getDeclaredMethods()。
因此:Field的打印好理解,因为sub是super类的子类,会继承super的类
同样method和constructor的打印也是如此。
clazz.getAnnotations()可以打印出当前类的注解和父类的注解
clazz.getDeclaredAnnotations()只会打印出当前类的注解
如果注解ATable把@Inherit去掉。那么后面四行的输出结果为:
true @annotion.inherit.BTable(name=) [@annotion.inherit.BTable(name=)] [@annotion.inherit.BTable(name=)]
无法获取到@ATable的注解,
也就是说注解和普通类的区别是如果一个子类想获取到父类上的注解信息,
那么必须在父类上使用的注解上面 加上@Inherit关键字
发表评论
-
深入学习EnumSet
2018-03-25 00:18 499Set接口的实现类HashSet/TreeSet,它们内部都是 ... -
枚举中valueOf用法
2018-01-14 11:21 4477Enum的特征如下: 1.它不能有public的构造函数,这样 ... -
mybatis源码学习总结-class.getResource方法与claasloader.getResource方法的区别
2018-01-14 10:49 1007Class.getResources(String path) ... -
使用自定义注解搭建简单框架
2017-05-01 00:54 508本文主要介绍如何使用Java运行时级别的注解配合反射来搭建框架 ... -
java注解处理器
2017-04-30 17:43 644注解处理器: Java SE5扩 ... -
JAVA注解总结
2017-04-30 12:59 544元注解: 元注解 ... -
java泛型理解2
2017-01-07 22:54 453泛型类型注意细节: 1.泛型类型变量不能是基本数据类型 比如, ... -
JAVA泛型理解
2017-01-07 22:33 465泛型类型的擦除: ArrayList< ... -
逻辑运算与移位运算
2012-11-27 14:56 1257源码:正数的补码与原码相同例+7 源码:00000111 补码 ... -
关于数字签名基础知识
2012-10-08 17:40 13111.消息摘要 public class MessageDige ... -
JJd
2012-05-10 20:02 0//Access-Request报文 创建message ... -
HashMap原理
2012-04-29 17:27 1788概述: HashMap是基于哈希表的Map接口的非同步实现。此 ... -
使用内部类有什么好处
2012-03-17 12:41 1285使用内部类在java编程高级设计中是必须的,它能使你的代码更加 ... -
关于a& 0xff的运算
2011-11-21 11:23 1404byte是一个有符号数可以表示-128~+127,但是作为一个 ... -
java调用Windows命令行
2011-11-20 21:32 1684java来调用windows的命令,一般情况下下面两行代码即可 ... -
parseInt(String s, int radix)用法介绍
2011-11-19 22:13 6275parseInt(String s, int radix) , ... -
深入理解String.getBytes()中编码问题
2011-11-04 15:25 2519查看jdk的源码得知,String.getBytes()的源码 ...
相关推荐
主要介绍了java元注解@Inherited的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
注解的理解 16 基本的 Annotation 介绍 16 @Override 注解 16 @Override 使用说明 17 @Deprecated 注解 17 @SuppressWarnings 注解 17 元注解 19 @Retention 注解 19 @Target 注解 19 @Documented注解 20 @Inherited...
注解(Annotation)JDK5.0引入 内置注解: @Override:重写方法 @Deprecated:废弃 ...@Inherited:子类可以继承父类的注解 自定义注解 @interface 注解名{} 属性为注解的参数:可以使用默认值 反射(框架设计的
注解可以包含与其绑定的元注解,元注解为注解提供信息,有四种元注解类型:包括@Retention @Target @Document @Inherited2、@t
4.@Inherited 这些类型和它们所⽀持的类在java.lang.annotation包中可以找到。下⾯我们看⼀下每个元注解的作⽤和相应分参数的使⽤说明。 @Target: @Target说明了Annotation所修饰的对象范围:Annotation可...
@Inherited @Target 用于描述注解的使用范围,可能的ElementType参数如下: CONSTRUCTOR:用于描述构造器 FIELD:用于描述域 LOCAL_VARIABLE:用于描述局部变量 METHOD:用于描述方法 PACKAGE:用于描述包 PARAMETER:用于...
注解的目标,使用ElementType枚举,目标可以有多个,用{}表示@Retention :注解保留到什么时候,使用RetentionPolicy ,更改RetentionPolicy.CLASS @Documented :表示注解信息包含到生成的文档中@Inherited :...
带有Inherited注解的service文件,包含java源文件和编译后的class文件,可用于dubbo+zookeeper平台的事务处理,下载后直接将原来的server.class文件替换即可
元注解(JDK的元Annotation)4.1.@Target4.2.@Retention4.3.@Documented4.4.@Inherited4.5.@Result四、在程序中使用(解析)注解五、案例——简单的测试框架 一、注解是什么? 从JDK5开始,Java增加对元数据的支持...
3.4 @Inherited 3.5 @Repeatable 四 Java 预置的注解 4.1 @Deprecated 4.2 @Override 4.3 @SuppressWarnings 4.4 @SafeVarargs 4.5 @FunctionalInterface 五 注解的属性 5.1 属性的定义 5.2 注解的提取 六 注解的...
@ComponentScan @Configuration @Conditional @Retention @Documented @Target @Inherited @Bean @Import等注解含义的总结和个人理解: 包扫描@ComponentScan+组件标注注解(@Controller/@Service/@Repository/@...
主要介绍了Java注解之Retention、Documented、Inherited注解介绍,本文内容和相关文章是系列文章,需要的朋友可以参考下
标识这个注解怎么保存,是只在代码中,还是编入class文件中,或者是在运行时可以通过反射访问。 @Documented - 标记这些注解是否包含在用户文档中。 @Target - 标记这个注解应该是哪种 Java 成员。 @Inherited - ...
@Inherited @Documented @Target({ElementType.FIELD, ElementType.TYPE, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface AccessLimit { //限流唯一标识 String key() default ""; ...
@Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType....
由于无论在Java后台或者Android开发中我们经常遇到注解这个功能机制,例如常用的框架Java后台开发中,Spring、MyBatis等,Android的Dagger2,butterknife等,都是注解框架。今天我们就了解java是如何进行设置注解的...
主要介绍了JDK 5 提供的注解:Target、Inherited和Documented的区别,需要的朋友可以参考下
Java程序设计中关联与继承的比较Java programming associated with inherited comparison
inherited在消息中的作用(编译器根据inherited所在的函数,直接转换成对祖先类同名动态函数的调用,或者转换成对DefaultHandler的调用).mht`