• 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.注解解析器:用来解析自定义注解。

  • 相关阅读:
    第三章 运算符与表达式
    python 第二章 对象与类型
    线段树区间染色+注意事项
    ACM-ICPC北京赛区2018重现赛 A题
    删除元组
    修改元组
    访问元组
    Python 元组
    Python List sort()方法
    hdu4501——小明系列故事——买年货(多维背包)
  • 原文地址:https://www.cnblogs.com/sunada2005/p/10865320.html
Copyright © 2020-2023  润新知