java自定义注释及其信息提取
转自: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.注解解析器:用来解析自定义注解。
java自定义注释及其信息提取的更多相关文章
- java自定义注释
一.什么是注释 说起注释,得先提一提什么是元数据(metadata).所谓元数据就是数据的数据.也就是说,元数据是描述数据的.就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义.而J2SE5 ...
- JAVA自定义注释(Target,Retention,Documented,Inherit)
java自定义注解 Java注解是附加在代码中的一些元信息,用于一些工具在编译.运行时进行解析和使用,起到说明.配置的功能.注解不会也不能影响代码的实际逻辑,仅仅起到辅助性的作用.包含在 java.l ...
- Java 自定义注释@interface的用法
最简单的待校验的注解定义 @Documented @Constraint(validatedBy = ExistBlankByListValidator.class) @Target({PARAMET ...
- java中自定义注释@interface的用法
一.什么是注释 说起注释,得先提一提什么是元数据(metadata).所谓元数据就是数据的数据.也就是说,元数据是描述数据的.就象数据表中的字段一样,每个字段描述了这个字段下的数据的含义.而J ...
- Java annotation 自定义注释@interface的用法
最近看到很多项目都是用了自定义注解,例如 1.什么是注解? 元数据(metadata),就是指数据的数据,元数据是描述数据的,就像数据库中的,表的字段,每一个 字段描述这个字段下面·的数据的含义,j2 ...
- java自定义注解类
一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...
- java自定义注解注解方法、类、属性等等【转】
http://anole1982.iteye.com/blog/1450421 http://www.open-open.com/doc/view/51fe76de67214563b20b385320 ...
- Android Studio自定义注释模板及生成JavaDoc
刚开始学习Android,使用了Android Studio IDE.为了将来生产JavaDoc,学习一下如何自定义注释模板. . 自定义注释模板 1. 通过 File –>Settings 或 ...
- java自定义注解知识实例及SSH框架下,拦截器中无法获得java注解属性值的问题
一.java自定义注解相关知识 注解这东西是java语言本身就带有的功能特点,于struts,hibernate,spring这三个框架无关.使用得当特别方便.基于注解的xml文件配置方式也受到人们的 ...
随机推荐
- MLflow系列1:MLflow入门教程(Python)
英文链接:https://mlflow.org/docs/latest/tutorial.html 本文链接:https://www.cnblogs.com/CheeseZH/p/11943280.h ...
- javascript prototype理解
如图比较好的阐述了prototype和__proto__ 简单的可以这么理解: 狗类A( function foo()),狗类A的模板描述:A.模板 (foo.prototype)是一个对象objec ...
- response.redirect 正在中止线程
问题描述:正在中止线程问题原因:Response.End 方法终止页的执行,并将此执行切换到应用程序的事件管线中的 Application_EndRequest 事件.不执行 Response.End ...
- C++中rapidxml用法
转载:https://www.cnblogs.com/rainbow70626/p/7586713.html 解析xml是第三方库很多,例如:tingxml,这次学习一下rapidxml,rapidx ...
- 运维笔记--Docker文件占用磁盘空间异常处理
场景描述: 1. 服务器运行一段时间后,发现系统盘磁盘空间在不断增加,一开始的时候,不会影响系统,随着时间的推移,磁盘空间在不断增加,直到有一天你会发现系统盘剩余空间即将使用完,值得庆幸的是,如果您使 ...
- UltraISO安装windows10时0x8007000D错误解决办法
进入安装界面之后提示错误:windows无法打开所需的文件 F:\Sources\install.wim.请确保安装所需的所有文件可用,并重新启动安装.错误代码:0x8007000D 原因: 肯定是i ...
- Sword 哈希表
哈希表 哈希表是一种典型的以空间换取时间的数据结构,在没有冲突的情况下,对任意元素的插入.索引.删除的时间复杂度都是O().这样优秀的时间复杂度是通过将元素的key值以hash方法f映射到哈希表中的某 ...
- 【454】ML-DL相关链接
GD(梯度下降)和SGD(随机梯度下降) 机器学习中的Bias和Variance 机器学习之判别式模型和生成式模型 笔记 | 什么是Cross Entropy
- Zabbix主动模式与被动模式的区别——最简单的解释
一直搞不清楚Zabbix的主动模式和被动模式的差别,网上看到别人博客里的解释都是云里雾里的,完全搞不清.知道偶然看到了以下这个解释.就基本上明白了. Zabbix的主动模式和被动模式都是相对agent ...
- spring boot 实现定时任务
定时任务或者说定时调度,是系统中比较普遍的一个功能,例如数据归档.清理,数据定时同步(非实时),定时收发等等都需要用到定时任务,常见的定时调度框架有Quartz.TBSchedule等. 如何在Spr ...