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