轉自: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.注解解析器:用來解析自定義注解。