接口:AnnotatedElement

  1. * Represents an annotated element of the program currently running in this
  2. * VM. This interface allows annotations to be read reflectively. All
  3. * annotations returned by methods in this interface are immutable and
  4. * serializable. The arrays returned by methods of this interface may be modified
  5. * by callers without affecting the arrays returned to other callers.
  6.   表示当前在此虚拟机中运行的程序的注释元素,该界面允许反射读取注释,通过该接口方法返回的所有注释
    都是不可变并且可序列化。通过此接口的方法返回的陈列可以由呼叫着,而不会影响其他调用者返回阵列进行修改。
    */
  7. public interface AnnotatedElement {
  8. /**
  9. * Returns true if an annotation for the specified type
  10. * is <em>present</em> on this element, else false. This method
  11. * is designed primarily for convenient access to marker annotations.
  12.     如果此元素上存在指定类型的注释,则返回true,否则返回false,该方法主要用于方便
        访问标记注释
    */
  13. default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
  14. return getAnnotation(annotationClass) != null;
  15. }
  16.  
  17. /**
  18. * Returns this element's annotation for the specified type if
  19. * such an annotation is <em>present</em>, else null.
  20.   如果这样的注解存在,就返回该注解,否则返回Null*/
  21. <T extends Annotation> T getAnnotation(Class<T> annotationClass);
  22.  
  23. /**
  24. * Returns annotations that are <em>present</em> on this element.
  25. *返回此元素上存在的注解
  26. */
  27. Annotation[] getAnnotations();
  28.  
  29. /**
  30. * Returns annotations that are <em>associated</em> with this element.
  31. 返回与此注解相关联的注解*/
  32. default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
  33.    //默认的调用getDeclaredAnnotationsByte传入annotationClass作为参数
  34. T[] result = getDeclaredAnnotationsByType(annotationClass);
  35.     //如果返回的数组长度大于零,则返回数组,
        //如果返回的数组是零长度而这个AnnotationElement是一个类,
        //并且参数类型是可继承的注解类型,并且该AnnotatedElement的AnnotatedElement是非空的
        //则返回结果是在父类上调用getAnnotationsByType的结果,具有annotationClass作为论证
        //否则返回零长度的数组
  36. if (result.length == 0 && // Neither directly nor indirectly present
  37. this instanceof Class && // the element is a class
  38. AnnotationType.getInstance(annotationClass).isInherited()) { // Inheritable
  39. Class<?> superClass = ((Class<?>) this).getSuperclass();
  40. if (superClass != null) {
  41. // Determine if the annotation is associated with the
  42. // superclass
  43. result = superClass.getAnnotationsByType(annotationClass);
  44. }
  45. }
  46.  
  47. return result;
  48. }
  49.  
  50. /**
  51. * Returns this element's annotation for the specified type if
  52. * such an annotation is <em>directly present</em>, else null.
  53.     如果这样的注解直接存在,则返回指定类型的元素注解,否则返回null,此方法忽略继承的注解*/
  54. default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
  55. Objects.requireNonNull(annotationClass);
  56. // Loop over all directly-present annotations looking for a matching one
  57. for (Annotation annotation : getDeclaredAnnotations()) {
  58. if (annotationClass.equals(annotation.annotationType())) {
  59. //更强大,在编译期间进行动态转换
  60. return annotationClass.cast(annotation);
  61. }
  62. }
  63. return null;
  64. }
  65.  
  66. default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
  67. Objects.requireNonNull(annotationClass);
  68. return AnnotationSupport.
  69. getDirectlyAndIndirectlyPresent(Arrays.stream(getDeclaredAnnotations()).
  70. collect(Collectors.toMap(Annotation::annotationType,
  71. Function.identity(),
  72. ((first,second) -> first),
  73. LinkedHashMap::new)),
  74. annotationClass);
  75. }
  76.  
  77. /**
  78. * Returns annotations that are <em>directly present</em> on this element.
  79. * This method ignores inherited annotations.
  80.    返回直接存在于此元素上的注释*/
  81. Annotation[] getDeclaredAnnotations();
  82. }

isAnnotationPresent方法的示例:

  1. @Retention(RetentionPolicy.RUNTIME)
  2. @interface Cus{
  3. public String name();
  4. public String value();
  5. }
  6.  
  7. @Cus(name = "SampleClass",value = "Sample Class Annotation")
  8. class SampClass{
  9. private String sampleFileld;
  10. @Cus(name = "sampleMethod",value = "sample Method Annotation")
  11. public String sampleMethod(){
  12. return "sample";
  13. }
  14. public String getSampleFileld(){
  15. return sampleFileld;
  16. }
  17. public void setSampleFileld(String sa){
  18. this.sampleFileld=sa;
  19. }
  20. }
  21.  
  22. public class AccessibleObjectDemo {
  23. public static void main(String [] args) throws NoSuchMethodException {
  24. AccessibleObject sampleMethod = SampClass.class.getMethod("sampleMethod");
  25. System.out.println("sampleMethod.isAnnotationPresent:"+sampleMethod.isAnnotationPresent(Cus.class));
  26. //输出结果:sampleMethod.isAnnotationPresent:true
  27. }
  28. }
  1. getAnnotations()示例:
  1. @Retention(RetentionPolicy.RUNTIME)
  2. @interface Tt{
  3. public String name();
  4. public String value();
  5. }
  6.  
  7. @Tt(name = "SampleClass",value = "Sample Class Annotation")
  8. class SampClass2{
  9. private String sampleFileld;
  10. @Tt(name = "sampleMethod",value = "sample Method Annotation")
  11. public String sampleMethod(){
  12. return "sample";
  13. }
  14. public String getSampleFileld(){
  15. return sampleFileld;
  16. }
  17. public void setSampleFileld(String sa){
  18. this.sampleFileld=sa;
  19. }
  20. }
  21.  
  22. public class AccessibleObjectDemo2 {
  23. public static void main(String [] args) throws NoSuchMethodException {
  24. AccessibleObject sampleMethod = SampClass2.class.getMethod("sampleMethod");
  25. Annotation[] annotations = sampleMethod.getAnnotations();
  26.  
  27. for(int i=0;i<annotations.length;i++){
  28. if(annotations[i] instanceof Tt){
  29. Tt cus2= (Tt) annotations[i];
  30. System.out.println(cus2.name());
  31. System.out.println(cus2.value());
  32. /*
  33. 输出结果:
  34. sampleMethod
  35. sample Method Annotation
  36. * */
  37. }
  38. }
  39. }
  40. }

java基础源码 (4)--reflect包-AnnotatedElement接口的更多相关文章

  1. java基础源码 (5)--reflect包-AccessibleObject类

    学习参考博客:https://blog.csdn.net/benjaminzhang666/article/details/9664585AccessibleObject类基本作用 1.将反射的对象标 ...

  2. java基础源码 (6)--ArrayListt类

    原作出处:https://www.cnblogs.com/leesf456/p/5308358.html 简介: 1.ArrayList是一个数组队列,相当于动态数组.与java中的数组相比,它的容量 ...

  3. java基础源码 (1)--String类

    这个是String类上面的注释,我用谷歌翻译翻译的,虽然有点语法上的问题,但是大概都可以翻译出来 /** * The {@code String} class represents character ...

  4. java基础源码 (3)--Annotation(注解)

    借鉴博客地址:https://www.cnblogs.com/skywang12345/p/3344137.html /** * The common interface extended by al ...

  5. java基础源码 (2)--StringBuilder类

    Serializable(接口): 是一个IO的序列化接口,实现了这个接口,就代表这个类可以序列化或者反序列化,该接口没有方法或者字段,仅用于标识可串行话的语义. Appendable(接口): /* ...

  6. 自学Java HashMap源码

    自学Java HashMap源码 参考:http://zhangshixi.iteye.com/blog/672697 HashMap概述 HashMap是基于哈希表的Map接口的非同步实现.此实现提 ...

  7. java容器源码分析及常见面试题笔记

      概览 容器主要包括 Collection 和 Map 两种,Collection 存储着对象的集合,而 Map 存储着键值对(两个对象)的映射表. List Arraylist: Object数组 ...

  8. Java集合源码分析(三)LinkedList

    LinkedList简介 LinkedList是基于双向循环链表(从源码中可以很容易看出)实现的,除了可以当做链表来操作外,它还可以当做栈.队列和双端队列来使用. LinkedList同样是非线程安全 ...

  9. Java集合源码学习(一)集合框架概览

    >>集合框架 Java集合框架包含了大部分Java开发中用到的数据结构,主要包括List列表.Set集合.Map映射.迭代器(Iterator.Enumeration).工具类(Array ...

随机推荐

  1. CF1209C Paint the Digits

    CF1209C Paint the Digits 题意:给定T组数据,每组数据第一行输入数字串长度,第二行输入数字串,用数字1和2对数字串进行涂色,被1涂色的数字子串和被2涂色的数字子串拼接成新的数字 ...

  2. hdu 2838 Cow Sorting 树状数组求所有比x小的数的个数

    Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. 学习笔记:中国剩余定理(CRT)

    引入 常想起在空间里见过的一些智力题,这个题你见过吗: 一堆苹果,\(3\)个\(3\)个地取剩\(1\)个,\(5\)个\(5\)个地取剩\(1\)个,\(7\)个\(7\)个地取剩\(2\)个,苹 ...

  4. 「译」forEach循环中你不知道的3件事

    前言 本文925字,阅读大约需要7分钟. 总括: forEach循环中你不知道的3件事. 原文地址:3 things you didn't know about the forEach loop in ...

  5. 「SDOI2015」寻宝游戏

    传送门 Luogu 解题思路 发现一个性质: 对于所有的宝藏点 \({a_1,a_2...a_k}\) ,按照dfs序递增排列,答案就是: \(dis(a_1, a_2) + dis(a_2, a_3 ...

  6. PHP 附录 : 用户注册与登录完整代码

    login.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...

  7. C# 创建Windows Service(Windows服务)程序

    本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...

  8. 图片转换到指定大小PDF

    1.首先转换为eps jpeg2ps compile to exec file ./jpeg2ps  -p a4  a.jpg -o x.eps2.从eps转换到pdf ps2pdf -dDownsa ...

  9. SciPy 安装

    章节 SciPy 介绍 SciPy 安装 SciPy 基础功能 SciPy 特殊函数 SciPy k均值聚类 SciPy 常量 SciPy fftpack(傅里叶变换) SciPy 积分 SciPy ...

  10. java理解抽象类 2.19

    // Telphone.java public abstract class Telphone{ public abstract void call(); public abstract void m ...