转自:https://xuwenjin666.iteye.com/blog/1637247

1. 自定义注解

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.Documented; @Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Haha {
String author();
String desc();
String date();
String[] checkPoint();
}

2. 使用自定义注解

public class MyAnnotation {
@Haha(author = "hahaAuthor1",
desc = "hahaDesc",
date="2019-03-01",
checkPoint = {"1","2"}
)
public void useMyAnnotation1(){
System.out.println("MyAnnotation.useMyAnnotation1");
} @Haha(author = "hahaAuthor2",
desc = "hahaDesc",
date="2019-03-01",
checkPoint = {"1","2"}
)
public void useMyAnnotation2(){
System.out.println("MyAnnotation.useMyAnnotation2");
} public void notUseAnnotation(){
System.out.println("MyAnnotation.notUseMyAnnotation"); }
}

3. 信息提取

import java.lang.reflect.Method;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson; public class GetMyAnnotatioinInfo {
public static GetMyAnnotatioinInfo info = null; public static GetMyAnnotatioinInfo getInstance(){
if(info == null) info = new GetMyAnnotatioinInfo();
return info;
} public Map<String, String> getAnnotationInfo(Class annotationClass, String annotationField, String className) throws Exception{
Method[] methods = Class.forName(className).getDeclaredMethods(); System.out.println("类中所有方法名");
for(Method m : methods) System.out.println(m.getName()); System.out.println("目标注解名:" + annotationClass.getName() + " 类中使用了目标注解的方法有:");
Map<String, String> map = new HashMap<>();
Gson gson = new Gson();
for (Method m : methods){
if(m.isAnnotationPresent(annotationClass)){
System.out.println(m.getName());
Annotation an = m.getAnnotation(annotationClass);
Method anMethod = an.getClass().getDeclaredMethod(annotationField, null);
Object object = anMethod.invoke(an, null);
map.put(m.getName() + "." + annotationField, gson.toJson(object));
}
}
return map;
} public static void main(String[] args) throws Exception{
GetMyAnnotatioinInfo anInfo = GetMyAnnotatioinInfo.getInstance(); Map<String, String> res = anInfo.getAnnotationInfo(Haha.class, "author", MyAnnotation.class.getName());
res.putAll(anInfo.getAnnotationInfo(Haha.class, "checkPoint", MyAnnotation.class.getName())); System.out.println("以上注释名及内容如下:");
for(Map.Entry<String, String> entry : res.entrySet()){
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}

4. 执行结果

类中所有方法名
useMyAnnotation2
notUseAnnotation
useMyAnnotation1
目标注解名:Haha 类中使用了目标注解的方法有:
useMyAnnotation2
useMyAnnotation1
类中所有方法名
useMyAnnotation2
notUseAnnotation
useMyAnnotation1
目标注解名:Haha 类中使用了目标注解的方法有:
useMyAnnotation2
useMyAnnotation1
以上注释名及内容如下:
useMyAnnotation1.author = "hahaAuthor1"
useMyAnnotation2.author = "hahaAuthor2"
useMyAnnotation2.checkPoint = ["1","2"]
useMyAnnotation1.checkPoint = ["1","2"]

5. 补充

5.1.注解的定义:Java文件叫做Annotation,用@interface表示。

5.2.元注解:@interface上面按需要注解上一些东西,包括@Retention、@Target、@Document、@Inherited四种。

5.3.注解的保留策略:

  •   @Retention(RetentionPolicy.SOURCE)   // 注解仅存在于源码中,在class字节码文件中不包含
  •   @Retention(RetentionPolicy.CLASS)     // 默认的保留策略,注解会在class字节码文件中存在,但运行时无法获得
  •   @Retention(RetentionPolicy.RUNTIME)  // 注解会在class字节码文件中存在,在运行时可以通过反射获取到

5.4.注解的作用目标:

  •   @Target(ElementType.TYPE)                      // 接口、类、枚举、注解
  •   @Target(ElementType.FIELD)                     // 字段、枚举的常量
  •   @Target(ElementType.METHOD)                 // 方法
  •   @Target(ElementType.PARAMETER)            // 方法参数
  •   @Target(ElementType.CONSTRUCTOR)       // 构造函数
  •   @Target(ElementType.LOCAL_VARIABLE)   // 局部变量
  •   @Target(ElementType.ANNOTATION_TYPE) // 注解
  •   @Target(ElementType.PACKAGE)               // 包

5.5.注解包含在javadoc中:

  •   @Documented

5.6.注解可以被继承:

  •   @Inherited

5.7.注解解析器:用来解析自定义注解。

最新文章

  1. 第一篇(C#中?与??)
  2. js for in对象key排序
  3. SGU 148.B-Station
  4. Tea加密算法和XxTea加密算法
  5. VC++中的类的内存分布(上)(通过强制转换,观察地址,以及地址里的值来判断)
  6. IoC容器Autofac正篇之类型注册(五)
  7. JavaScript 扫描枪使用(一)
  8. GMF常见问题
  9. Win10命令大全通用(Win8,Win7)
  10. 尚硅谷springboot学习35-启动原理
  11. [HAOI2012]道路(最短路DAG上计数)
  12. Eclipse纯净版安装web插件
  13. openstack安装-计算节点-nova计算服务安装
  14. Always an integer UVALive - 4119
  15. 2018.09.01 09:08 Genesis
  16. Elasticsearch - 理解字段分析过程(_analyze与_explain)
  17. Apache启用GZIP压缩网页传输
  18. 标准的sql执行顺序
  19. Linux中逻辑卷(LVM)管理基本操作
  20. nefu 628 Garden visiting

热门文章

  1. (原创)C++ 同步队列
  2. allure与junit结合生成漂亮的demo
  3. 图片转化base64格式
  4. 【python库模块】Python subprocess模块功能与常见用法实例详解
  5. 工具分享:excel2json,将Excel表格转换为JSON
  6. Hadoop入门——初识Hadoop
  7. 【ARM-Linux开发】【CUDA开发】NVIDIA TEGRA X1:LINUX驱动程序包多媒体用户指南
  8. oracle登录信息对应
  9. Retrofit 二次封装实践
  10. fineui 实现下拉框模糊查询