接口:AnnotatedElement

 * Represents an annotated element of the program currently running in this
* VM. This interface allows annotations to be read reflectively. All
* annotations returned by methods in this interface are immutable and
* serializable. The arrays returned by methods of this interface may be modified
* by callers without affecting the arrays returned to other callers.
  表示当前在此虚拟机中运行的程序的注释元素,该界面允许反射读取注释,通过该接口方法返回的所有注释
都是不可变并且可序列化。通过此接口的方法返回的陈列可以由呼叫着,而不会影响其他调用者返回阵列进行修改。
*/
public interface AnnotatedElement {
/**
* Returns true if an annotation for the specified type
* is <em>present</em> on this element, else false. This method
* is designed primarily for convenient access to marker annotations.
    如果此元素上存在指定类型的注释,则返回true,否则返回false,该方法主要用于方便
    访问标记注释
*/
default boolean isAnnotationPresent(Class<? extends Annotation> annotationClass) {
return getAnnotation(annotationClass) != null;
} /**
* Returns this element's annotation for the specified type if
* such an annotation is <em>present</em>, else null.
  如果这样的注解存在,就返回该注解,否则返回Null*/
<T extends Annotation> T getAnnotation(Class<T> annotationClass); /**
* Returns annotations that are <em>present</em> on this element.
*返回此元素上存在的注解
*/
Annotation[] getAnnotations(); /**
* Returns annotations that are <em>associated</em> with this element.
返回与此注解相关联的注解*/
default <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
   //默认的调用getDeclaredAnnotationsByte传入annotationClass作为参数
T[] result = getDeclaredAnnotationsByType(annotationClass);
    //如果返回的数组长度大于零,则返回数组,
    //如果返回的数组是零长度而这个AnnotationElement是一个类,
    //并且参数类型是可继承的注解类型,并且该AnnotatedElement的AnnotatedElement是非空的
    //则返回结果是在父类上调用getAnnotationsByType的结果,具有annotationClass作为论证
    //否则返回零长度的数组
if (result.length == 0 && // Neither directly nor indirectly present
this instanceof Class && // the element is a class
AnnotationType.getInstance(annotationClass).isInherited()) { // Inheritable
Class<?> superClass = ((Class<?>) this).getSuperclass();
if (superClass != null) {
// Determine if the annotation is associated with the
// superclass
result = superClass.getAnnotationsByType(annotationClass);
}
} return result;
} /**
* Returns this element's annotation for the specified type if
* such an annotation is <em>directly present</em>, else null.
    如果这样的注解直接存在,则返回指定类型的元素注解,否则返回null,此方法忽略继承的注解*/
default <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
// Loop over all directly-present annotations looking for a matching one
for (Annotation annotation : getDeclaredAnnotations()) {
if (annotationClass.equals(annotation.annotationType())) {
//更强大,在编译期间进行动态转换
return annotationClass.cast(annotation);
}
}
return null;
} default <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass) {
Objects.requireNonNull(annotationClass);
return AnnotationSupport.
getDirectlyAndIndirectlyPresent(Arrays.stream(getDeclaredAnnotations()).
collect(Collectors.toMap(Annotation::annotationType,
Function.identity(),
((first,second) -> first),
LinkedHashMap::new)),
annotationClass);
} /**
* Returns annotations that are <em>directly present</em> on this element.
* This method ignores inherited annotations.
   返回直接存在于此元素上的注释*/
Annotation[] getDeclaredAnnotations();
}

isAnnotationPresent方法的示例:

@Retention(RetentionPolicy.RUNTIME)
@interface Cus{
public String name();
public String value();
} @Cus(name = "SampleClass",value = "Sample Class Annotation")
class SampClass{
private String sampleFileld;
@Cus(name = "sampleMethod",value = "sample Method Annotation")
public String sampleMethod(){
return "sample";
}
public String getSampleFileld(){
return sampleFileld;
}
public void setSampleFileld(String sa){
this.sampleFileld=sa;
}
} public class AccessibleObjectDemo {
public static void main(String [] args) throws NoSuchMethodException {
AccessibleObject sampleMethod = SampClass.class.getMethod("sampleMethod");
System.out.println("sampleMethod.isAnnotationPresent:"+sampleMethod.isAnnotationPresent(Cus.class));
//输出结果:sampleMethod.isAnnotationPresent:true
}
}
getAnnotations()示例:
@Retention(RetentionPolicy.RUNTIME)
@interface Tt{
public String name();
public String value();
} @Tt(name = "SampleClass",value = "Sample Class Annotation")
class SampClass2{
private String sampleFileld;
@Tt(name = "sampleMethod",value = "sample Method Annotation")
public String sampleMethod(){
return "sample";
}
public String getSampleFileld(){
return sampleFileld;
}
public void setSampleFileld(String sa){
this.sampleFileld=sa;
}
} public class AccessibleObjectDemo2 {
public static void main(String [] args) throws NoSuchMethodException {
AccessibleObject sampleMethod = SampClass2.class.getMethod("sampleMethod");
Annotation[] annotations = sampleMethod.getAnnotations(); for(int i=0;i<annotations.length;i++){
if(annotations[i] instanceof Tt){
Tt cus2= (Tt) annotations[i];
System.out.println(cus2.name());
System.out.println(cus2.value());
/*
输出结果:
sampleMethod
sample Method Annotation
* */
}
}
}
}

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. 吴裕雄--天生自然HADOOP操作实验学习笔记:hadoop框架认识以及基本操作

    实验目的 了解Hadoop的概念和原理 学习HDFS架构原理 熟悉mapreduce框架 熟悉mapred和yarn命令 实验原理 1.hadoop和hadoop生态系统 hadoop的思想来源是Go ...

  2. 如何更改placeholder属性中文字颜色

    如何更改placeholder属性中文字颜色 placeholder这个属性是HTML5中新增的属性,该属性的作用是规定可描述输入字段预期值的简短的提示信息,该提示会在用户输入之前显示在输入字段中,会 ...

  3. jdbc学习一半的代码

    用java连接MySQL的准备工作 1.下载MySQL(了解MySQL的基本语法) 2.下载java的和MySQL的连接 3.在程序中加入2中下载的jar包 写java程序连接数据库的基本步骤: 1. ...

  4. Java服务端对Cookie的简单操作

    Java服务端对Cookie的简单操作 时间 2016-04-07 10:39:44 极客头条 原文  http://www.cuiyongzhi.com/index.php/post/15.html ...

  5. 校园服务APP使用体验

    校园服务APP软件使用体验 一.概况: 校园服务这款软件是针对大学生这一群体量身打造,具有简洁的界面,能让使用者快速上手,不存在第一次使用发懵,发怵等情况,是一款非常实用的功能软件. 二.功能特性: ...

  6. Mysql使用存储过程创建测试数据

    一.概述 存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集.其存储在数据库中,经过第一次编译后调用不需要再次编译,用户通过指定存储过程的名字并给出 ...

  7. 【转】深入分析JAVA IO(BIO、NIO、AIO)

    IO的基本常识 1.同步 用户进程触发IO操作并等待或者轮询的去查看IO操作是否完成 2.异步 用户触发IO操作以后,可以干别的事,IO操作完成以后再通知当前线程继续处理 3.阻塞 当一个线程调用 r ...

  8. 阿里云服务器 :Linux环境下搭建Apache+php+mysql

    以前我用的是Windows2012 的服务器,那时候只是抱着玩一玩的心态,所有用的是Windows,但是后来被导师给DISS了,于是决定改服务器的操作系统: (一)下载安装php+mysql+apac ...

  9. Mybatis注解开发单表CRUD

    Mybatis注解开发单表CRUD mybatis注解开发和xml开发不可兼容,要么全部使用注解,要么全部使用xml,个人建议注解,简单. 当实体类属性名称和数据库表属性名称一致时:无需配置Resul ...

  10. idea中跑mapreduce报错, PATH设置错误

    问题如题,报错: [root@node01 servers]# hadoop jar loginVisit.jar cn.itcast.loginVisit.step1.Step1Main19/07/ ...