借鉴博客地址:https://www.cnblogs.com/skywang12345/p/3344137.html

  1. /**
  2. * The common interface extended by all annotation types.
  3. 所有注释类型扩展的公共接口*/
  4. public interface Annotation {
  5.  
  6. boolean equals(Object obj);
  7.  
  8. /**
  9. * Returns the hash code of this annotation, as defined below:
  10.     返回注解的哈希码值*/
  11. int hashCode();
  12.  
  13. /**
  14. * Returns a string representation of this annotation. The details
  15. * of the representation are implementation-dependent, but the following
  16. * may be regarded as typical:
        以字符串形式返回注解*/
  17. String toString();
  18.  
  19. /**
  20. * Returns the annotation type of this annotation.
  21.    返回注解的类型*/
  22. Class<? extends Annotation> annotationType();
  23. }
  1. ElementType.javaEnum枚举类型,它用来指定Annotation的类型
    /**
  2. * The constants of this enumerated type provide a simple classification of the
  3. * syntactic locations where annotations may appear in a Java program. These
  4. * constants are used in {@link Target java.lang.annotation.Target}
  5. * meta-annotations to specify where it is legal to write annotations of a
  6. * given type.
  7.   此枚举类型的常量提供了可能出现在java程序中注释的位置进行简单分类,这些类型是用于Targer(元注释)上
    *此枚举类型的常量提供了注释可能出现在Java程序中的语法位置的简单分类。 这些常量用于{@link Target java.lang.annotation.Target}元注释,以指定编写给定类型注释的合法位置。
    */
  8. public enum ElementType {
  9. /** Class, interface (including annotation type), or enum declaration */
      类,接口(包括注释类型)或枚举类型
  10. TYPE,
  11. /** Field declaration (includes enum constants)
      字段声明(包括枚举常量)
    */
  12. FIELD,
  13. /** Method declaration
      方法声明
    */
  14. METHOD,
  15. /** Formal parameter declaration
      参数声明
    */
  16. PARAMETER,
  17. /** Constructor declaration
      构造函数声明
    */
  18. CONSTRUCTOR,
  19. /** Local variable declaration
      局部变量声明
    */
  20. LOCAL_VARIABLE,
  21. /** Annotation type declaration
      注释声明类型
    */
  22. ANNOTATION_TYPE,
  23. /** Package declaration
      包声明
    */
  24. PACKAGE,
  25. /**
  26. * Type parameter declaration
  27. *输入参数类型
  28. * @since 1.8
  29. */
  30. TYPE_PARAMETER,
  31. /**
  32. * Use of a type
  33. *使用类型
  34. * @since 1.8
  35. */
  36. TYPE_USE
  37. }

RetentionPolicy.java,是Enum枚举类型,它用来指定Annotation的策略。通俗点说,就说不同的RetentionPolicy的注释作用域不同

  1. /**
  2. * Annotation retention policy.
  3. 注释保留策略*/
  4. public enum RetentionPolicy {
  5. /**
  6. * Annotations are to be discarded by the compiler.
        Annotation注释信息仅存在于编译器处理期间,编译器处理完成之后就没有改注释信息了
  7. */
  8. SOURCE,
  9.  
  10. /**
  11. * Annotations are to be recorded in the class file by the compiler
  12. * but need not be retained by the VM at run time. This is the default
  13. * behavior.
        编译器将Annotation注释存储于类对应的在.class文件中,默认行为
  14. */
  15. CLASS,
  16.  
  17. /**
  18. * Annotations are to be recorded in the class file by the compiler and
  19. * retained by the VM at run time, so they may be read reflectively.
  20.   编译器将Annotation存储于类对应的。class文件中,并且可以由JVM读入(反射)*/
  21. RUNTIME
  22. }

语法定义:

  

  1. /*
  2. * @interface:
  3. * 意味着它实现了java.lang.annotation.Annotation接口,即该注解就是一个Annotation
  4. * 定义Annotation时,@interface是必须的
  5. * 注意:
  6. * 它和我们通常的implemented实现接口的方法不同,Annotation接口的实现细节都由编译器
  7. * 完成。通过@interface定义注解后,改注解不能继承其他的注解或接口
  8. * @Documented:
  9. * 类和方法的Annotation在缺省情况下是不出现在javadoc中的,如果使用@Documented修饰该注解
  10. * 则表示它可以出现在Javadoc中。定义注解时,@Documented可有可无,若没有定义,则注解不会
  11. * 出现在javadoc中
  12. * @Target(ElementType.TYPE):
  13. * ElementType是Annotation(注解)的类型属性,而@Target的作用,就是来指定Annotation(注解)
  14. * 类型属性。@Target(ElementType.TYPE)指定该Annotation(注解)的类型是ElementType.TYPE,
  15. * 这意味着MyAnnotation是用来修饰类,接口(包括注释类型)或枚举声明的注释。定义Annotation(注解)
  16. * 时,@Target可有可无,若有@Target,则该Annotation(注解)可以用于任何地方
  17. *@Retention(RetentionPolicy.RUNTIME):
  18. * RetentionPolicy是Annotation(注解)的策略属性,而@Retention的作用,就是指定Annotation(注解)
  19. * 的策略是RetentionPolicy.RUNTIME,意味着,编译器会将该Annotation信息保存在对应类的.class文件中
  20. * 并且能被jvm读取。定义Annotation(注解)时,@Retention可有可无,若没有@Retention,则默认是
  21. * RetentionPolicy.CLASS(默认行为)
  22. * */
  23. @Documented
  24. @Target(ElementType.TYPE)
  25. @Retention(RetentionPolicy.RUNTIME)
  26. public @interface MyAnnotation {
  27.  
  28. }

反射使用注解

  1. package com.lkj.Annotation;
  2.  
  3. import com.lkj.string.Persion;
  4.  
  5. import java.lang.annotation.*;
  6. import java.lang.annotation.Annotation;
  7. import java.lang.reflect.Method;
  8.  
  9. @Retention(RetentionPolicy.RUNTIME)
  10. @interface MyAnnotation2{
  11. String [] value() default "unknown";
  12. }
  13.  
  14. class Person{
  15. @MyAnnotation2//意味着empty()对应的MyAnnotation2的value值默认是unknown
  16. @Deprecated//意味着empty()方法,不再被建议使用
  17. public void empty(){
  18. System.out.println("\nempty");
  19. }
  20. @MyAnnotation2(value = {"girl","boy"})//意味着MyAnnotation2的value值是{“girl”,“boy”}
  21. public void someBody(String name,int age){
  22. System.out.println("\nsomebody: "+name+", "+age);
  23. }
  24. }
  25.  
  26. public class AnnotationUser {
  27. public static void iteratorAnnotations(Method method){
  28. //判断someBody()方法是否包含MyAnnotation2注解
  29. if(method.isAnnotationPresent(MyAnnotation2.class)){
  30. //获取该方法的MyAnnotation2注解实例
  31. MyAnnotation2 myAnnotation2 = method.getAnnotation(MyAnnotation2.class);
  32. //获取MyAnnotation2的值,并打印出来
  33. String[] value = myAnnotation2.value();
  34. for (int i=0;i<value.length;i++){
  35. System.out.printf(value[i]);
  36. }
  37. System.out.println();
  38. }
  39. //获取方法上的所有注解,并打印出来
  40. Annotation[] annotations = method.getAnnotations();
  41. for (Annotation annotation:annotations){
  42. System.out.println(annotation);
  43. }
  44. }
  45. public static void main(String []args) throws Exception {
  46. //实例Persion对象
  47. Person person = new Person();
  48. //获取Persion的Class实例
  49. Class<Person> c=Person.class;
  50. //获取someBody()方法的method实例
  51. Method method=c.getMethod("someBody", new Class[]{String.class,int.class});
  52. //执行该方法
  53. method.invoke(person,new Object[]{"lily",18});
  54. iteratorAnnotations(method);
  55.  
  56. //获取someBody()方法的Method实例
  57. Method empty = c.getMethod("empty", new Class[]{});
  58. empty.invoke(person,new Object[]{});
  59. iteratorAnnotations(empty);
  60. }
  61. }

输出结果:

  

java基础源码 (3)--Annotation(注解)的更多相关文章

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

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

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

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

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

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

  4. java基础源码 (4)--reflect包-AnnotatedElement接口

    接口:AnnotatedElement * Represents an annotated element of the program currently running in this * VM. ...

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

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

  6. Java基础13:反射与注解详解

    Java基础13:反射与注解详解 什么是反射? 反射(Reflection)是Java 程序开发语言的特征之一,它允许运行中的 Java 程序获取自身的信息,并且可以操作类或对象的内部属性. Orac ...

  7. 自学Java HashMap源码

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

  8. SpringMVC源码解读 - RequestMapping注解实现解读 - RequestCondition体系

    一般我们开发时,使用最多的还是@RequestMapping注解方式. @RequestMapping(value = "/", param = "role=guest& ...

  9. Spring IoC 源码分析 (基于注解) 之 包扫描

    在上篇文章Spring IoC 源码分析 (基于注解) 一我们分析到,我们通过AnnotationConfigApplicationContext类传入一个包路径启动Spring之后,会首先初始化包扫 ...

随机推荐

  1. 吴裕雄--天生自然PYTHON爬虫:安装配置MongoDBy和爬取天气数据并清洗保存到MongoDB中

    1.下载MongoDB 官网下载:https://www.mongodb.com/download-center#community 上面这张图选择第二个按钮 上面这张图直接Next 把bin路径添加 ...

  2. 第2节 storm实时看板案例:10、redis的安装使用回顾

    2.redis的持久化机制: redis支持两种持久化机制:RDB  AOF RDB:多少秒之内,有多少个key放生变化,将redis当中的数据dump到磁盘保存,保存成一个文件,下次再恢复的时候,首 ...

  3. 第1节 kafka消息队列:3、4、kafka的安装以及命令行的管理使用

    6.kafka的安装 5.1三台机器安装zookeeper 注意:安装zookeeper之前一定要确保三台机器时钟同步 */1 * * * * /usr/sbin/ntpdate us.pool.nt ...

  4. redis学习笔记-04:redis五大数据结构类型

    redis的命令大全网站:http://redisdoc.com/ 一.redis五大数据类型 1.String(字符串).Hash(哈希,类似Java里的Map).List(列表).Set(集合)和 ...

  5. jmeter断言之响应code:200

    因为Jmeter只要检测到网页的响应,就认为是Pass而并不管当前网页内容的正确性.在进行压力测试时,为了检查Web服务器返回的网页是否正确,我们可以设置断言,这些断言是验证网页上是否存在指定的Tex ...

  6. vim的几种模式&快捷键

    vim的几种模式&快捷键 2017年01月01日 14:05:24 阅读数:3060 一.vim的模式 基本上vim可以分为三种模式:命令模式,插入模式和底行模式,其实vim有多种模式,我们只 ...

  7. Mozilla Firefox 68 正式发布下载:对刚Chrome

    Mozilla Firefox 68开源和跨平台Web浏览器现在正式发布,可以下载适用于GNU/Linux,Mac和Windows平台的Firefox 68了. Firefox 68网络浏览器现在可以 ...

  8. 变相降价的iPhone,能挽救苹果在中国的命运吗?

    人无千日好,花无百样红.当年iPhone的横空出世不仅开辟了智能手机时代,还间接导致了诺基亚.黑莓等手机品牌的没落.十余年来,苹果凭借iPhone活得风光无限,并成为全球首个市值超万亿美元的公司.但进 ...

  9. ETC系列产品非接触式读卡器方案:SI522

    随着科技的不断发展,出行上高速这是非常寻常的事.但是在很多节假日高峰时期,在高速路口塞车缴费给很多车主造成很大的烦心.为了解决这一系列的问题,科技发明了ETC这种便捷式缴费技术,让车主们顺畅通过高速路 ...

  10. 【capstone/ropgadget】环境配置

    具体环境配置可参考 https://github.com/JonathanSalwan/ROPgadget/tree/master 作者给出的安装方式 但具体配置中出现了问题,如引用时出现如下错误: ...