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

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

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

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

语法定义:

  

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

反射使用注解

package com.lkj.Annotation;

import com.lkj.string.Persion;

import java.lang.annotation.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method; @Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
String [] value() default "unknown";
} class Person{
@MyAnnotation2//意味着empty()对应的MyAnnotation2的value值默认是unknown
@Deprecated//意味着empty()方法,不再被建议使用
public void empty(){
System.out.println("\nempty");
}
@MyAnnotation2(value = {"girl","boy"})//意味着MyAnnotation2的value值是{“girl”,“boy”}
public void someBody(String name,int age){
System.out.println("\nsomebody: "+name+", "+age);
}
} public class AnnotationUser {
public static void iteratorAnnotations(Method method){
//判断someBody()方法是否包含MyAnnotation2注解
if(method.isAnnotationPresent(MyAnnotation2.class)){
//获取该方法的MyAnnotation2注解实例
MyAnnotation2 myAnnotation2 = method.getAnnotation(MyAnnotation2.class);
//获取MyAnnotation2的值,并打印出来
String[] value = myAnnotation2.value();
for (int i=0;i<value.length;i++){
System.out.printf(value[i]);
}
System.out.println();
}
//获取方法上的所有注解,并打印出来
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation:annotations){
System.out.println(annotation);
}
}
public static void main(String []args) throws Exception {
//实例Persion对象
Person person = new Person();
//获取Persion的Class实例
Class<Person> c=Person.class;
//获取someBody()方法的method实例
Method method=c.getMethod("someBody", new Class[]{String.class,int.class});
//执行该方法
method.invoke(person,new Object[]{"lily",18});
iteratorAnnotations(method); //获取someBody()方法的Method实例
Method empty = c.getMethod("empty", new Class[]{});
empty.invoke(person,new Object[]{});
iteratorAnnotations(empty);
}
}

输出结果:

  

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. 代理实现aop以及代理工厂实现增强

    一.静态代理实现 1.接口(抽象主题) 2.接口的实现类(真实主题) 3.代理类(代理主题) 4.测试类: ApplicationContext context=new ClassPathXmlApp ...

  2. 进程管理与SELinux

      进程(process):   将程序与进程的总结:  程序 (program):通常为 binary program ,放置在储存媒体中 (如硬盘.光盘.软盘.磁带等), 为实体文 件的型态存在 ...

  3. C语言入门书籍知识点记录

    1. 数据在内存中的存储(二进制存储) 内存条:电路的电压有两种状态:0V或者5V,对应的一个元器件有2种状态:0 或者1. 一般情况下我们不一个一个的使用元器件,而是将8个元器件看做一个单位. 一个 ...

  4. ROS学习笔记INF-重要操作列表

    该笔记将重要操作的步骤进行列表,以便查询: 添加消息 在包中的msg文件夹中创建msg文件 确保package.xml中的如下代码段被启用: <build_depend>message_g ...

  5. HTTP和HTTPS的区别,SSL的握手过程

    超文本传输协议HTTP协议被用于在Web浏览器和网站服务器之间传递信息,HTTP协议以明文方式发送内容,不提供任何方式的数据加密,如果攻击者截取了Web浏览器和网站服务器之间的传输报文,就可以直接读懂 ...

  6. C 常用库函数memset,编译器宏定义assert

    一. 总览 1.1库函数 函数名 头文件 功能 原型 说明 syslog syslog.h 记录至系统记录(日志) void    syslog(int, const char *, ...) __p ...

  7. vue中使用矢量图

    1.打开矢量图库,将需要的图表添加至购物车 2.将购物车的图标添加到一个项目中(便于后期增加更新)并下载至本地 3.将这四个文件及iconfont.css添加至项目的assets中 4.打开iconf ...

  8. Rolling Update【转】

    滚动更新是一次只更新一小部分副本,成功后,再更新更多的副本,最终完成所有副本的更新.滚动更新的最大的好处是零停机,整个更新过程始终有副本在运行,从而保证了业务的连续性. 下面我们部署三副本应用,初始镜 ...

  9. 宏碁发布两款全A平台笔记本:良心价

    导读 8月3日消息,在全球数码互动娱乐盛会ChinaJoy上,宏碁推出全新两款全A平台笔记本——暗影骑士4 锐龙版酷冷游戏本和蜂鸟Swift3锐龙版金属轻薄本. 此次发布的宏碁暗影骑士4 锐龙版笔记本 ...

  10. 卸载重装ngin的问题解决方案

    1,卸载nginx不保留配置文件 $ sudo apt-get --purge remove nginx 2,卸载自动安装且不再需要的依赖包 $ sudo apt-get autoremove 3,卸 ...