1. 自定义注解
importjava.lang.annotation.Retention;importjava.lang.annotation.RetentionPolicy;importjava.lang.annotation.Target;importjava.lang.annotation.ElementType;importjava.lang.annotation.Documented;
@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)public @interfaceHaha {
String author();
String desc();
String date();
String[] checkPoint();
}
2. 使用自定义注解
public classMyAnnotation {
@Haha(author= "hahaAuthor1",
desc= "hahaDesc",
date="2019-03-01",
checkPoint= {"1","2"}
)public voiduseMyAnnotation1(){
System.out.println("MyAnnotation.useMyAnnotation1");
}
@Haha(author= "hahaAuthor2",
desc= "hahaDesc",
date="2019-03-01",
checkPoint= {"1","2"}
)public voiduseMyAnnotation2(){
System.out.println("MyAnnotation.useMyAnnotation2");
}public voidnotUseAnnotation(){
System.out.println("MyAnnotation.notUseMyAnnotation");
}
}
3. 信息提取
importjava.lang.reflect.Method;importjava.lang.annotation.Annotation;importjava.util.ArrayList;importjava.util.HashMap;importjava.util.Map;importcom.google.gson.Gson;public classGetMyAnnotatioinInfo {public static GetMyAnnotatioinInfo info = null;public staticGetMyAnnotatioinInfo getInstance(){if(info == null) info = newGetMyAnnotatioinInfo();returninfo;
}public Map getAnnotationInfo(Class annotationClass, String annotationField, String className) throwsException{
Method[] methods=Class.forName(className).getDeclaredMethods();
System.out.println("类中所有方法名");for(Method m : methods) System.out.println(m.getName());
System.out.println("目标注解名:" + annotationClass.getName() + " 类中使用了目标注解的方法有:");
Map map = new HashMap<>();
Gson gson= newGson();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));
}
}returnmap;
}public static void main(String[] args) throwsException{
GetMyAnnotatioinInfo anInfo=GetMyAnnotatioinInfo.getInstance();
Map res = anInfo.getAnnotationInfo(Haha.class, "author", MyAnnotation.class.getName());
res.putAll(anInfo.getAnnotationInfo(Haha.class, "checkPoint", MyAnnotation.class.getName()));
System.out.println("以上注释名及内容如下:");for(Map.Entryentry : 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.注解解析器:用来解析自定义注解。