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 </span>= "hahaAuthor2"<span style="color: rgba(0, 0, 0, 1)">, desc </span>= "hahaDesc"<span style="color: rgba(0, 0, 0, 1)">, date</span>="2019-03-01"<span style="color: rgba(0, 0, 0, 1)">, checkPoint </span>= {"1","2"<span style="color: rgba(0, 0, 0, 1)">} ) </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> useMyAnnotation2(){ System.out.println(</span>"MyAnnotation.useMyAnnotation2"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> notUseAnnotation(){ System.out.println(</span>"MyAnnotation.notUseMyAnnotation"<span style="color: rgba(0, 0, 0, 1)">); }
}
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;</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span><span style="color: rgba(0, 0, 0, 1)"> GetMyAnnotatioinInfo getInstance(){ </span><span style="color: rgba(0, 0, 255, 1)">if</span>(info == <span style="color: rgba(0, 0, 255, 1)">null</span>) info = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> GetMyAnnotatioinInfo(); </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> info; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> Map<String, String> getAnnotationInfo(Class annotationClass, String annotationField, String className) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception{ Method[] methods </span>=<span style="color: rgba(0, 0, 0, 1)"> Class.forName(className).getDeclaredMethods(); System.out.println(</span>"类中所有方法名"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)">(Method m : methods) System.out.println(m.getName()); System.out.println(</span>"目标注解名:" + annotationClass.getName() + " 类中使用了目标注解的方法有:"<span style="color: rgba(0, 0, 0, 1)">); Map</span><String, String> map = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap<><span style="color: rgba(0, 0, 0, 1)">(); Gson gson </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Gson(); </span><span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> (Method m : methods){ </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)">(m.isAnnotationPresent(annotationClass)){ System.out.println(m.getName()); Annotation an </span>=<span style="color: rgba(0, 0, 0, 1)"> m.getAnnotation(annotationClass); Method anMethod </span>= an.getClass().getDeclaredMethod(annotationField, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); Object object </span>= anMethod.invoke(an, <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">); map.put(m.getName() </span>+ "." +<span style="color: rgba(0, 0, 0, 1)"> annotationField, gson.toJson(object)); } } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> map; } </span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> main(String[] args) <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception{ GetMyAnnotatioinInfo anInfo </span>=<span style="color: rgba(0, 0, 0, 1)"> GetMyAnnotatioinInfo.getInstance(); Map</span><String, String> res = anInfo.getAnnotationInfo(Haha.<span style="color: rgba(0, 0, 255, 1)">class</span>, "author", MyAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">.getName()); res.putAll(anInfo.getAnnotationInfo(Haha.</span><span style="color: rgba(0, 0, 255, 1)">class</span>, "checkPoint", MyAnnotation.<span style="color: rgba(0, 0, 255, 1)">class</span><span style="color: rgba(0, 0, 0, 1)">.getName())); System.out.println(</span>"以上注释名及内容如下:"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">for</span>(Map.Entry<String, String><span style="color: rgba(0, 0, 0, 1)"> entry : res.entrySet()){ System.out.println(entry.getKey() </span>+ " = " +<span style="color: rgba(0, 0, 0, 1)"> 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. 注解解析器:用来解析自定义注解。