java自定义注释及其信息提取

转自:https://xuwenjin666.iteye.com/blog/1637247

1. 自定义注解

package cn.veji.hibernate.po;  
  
import java.lang.annotation.ElementType;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
import java.lang.annotation.Target;  
  
@Target(ElementType.METHOD)  
@Retention(RetentionPolicy.RUNTIME)  
public @interface Privilege {  
    String[] value();  
  
}  

2. 使用自定义注解

package cn.veji.hibernate.po;  
  
package cn.veji.hibernate.po;  
  
public class TestPrivilege {  
  
    @Privilege( { "a" })  
    public void a() {  
    }  
  
    @Privilege( { "b" })  
    public void b() {  
    }  
  
    @Privilege( { "c" })  
    public void c() {  
    }  
  
}  

3. 信息提取

package cn.veji.hibernate.po;  
  
package cn.veji.hibernate.test;  
  
import java.lang.annotation.Annotation;  
import java.lang.reflect.Method;  
import java.util.HashMap;  
import java.util.Map;  
  
import cn.veji.hibernate.po.Privilege;  
import cn.veji.hibernate.po.TestPrivilege;  
  
public class AnnotationUtil {  
  
    public static AnnotationUtil anno = null;  
  
    public static AnnotationUtil getInstance() {  
        if (anno == null) {  
            anno = new AnnotationUtil();  
        }  
        return anno;  
    }  
  
    /** 
     * 读取注解值 
     *  
     * @param annotationClasss 处理Annotation类名称 
     * @param annotationField 处理Annotation类属性名称 
     * @param className 处理Annotation的使用类名称 
     * @return 
     * @throws Exception 
     */  
    @SuppressWarnings("all")  
    public Map<String, String> loadVlaue(Class annotationClasss,  
            String annotationField, String className) throws Exception {  
  
        System.out.println("处理Annotation类名称  === "+annotationClasss.getName());  
        System.out.println("处理Annotation类属性名称  === "+annotationField);  
        System.out.println("处理Annotation的调用类名称  === "+className);  
        Map<String, String> map = new HashMap<String, String>();  
        Method[] methods = Class.forName(className).getDeclaredMethods();  
        for (Method method : methods) {  
            if (method.isAnnotationPresent(annotationClasss)) {  
                Annotation p = method.getAnnotation(annotationClasss);  
                Method m = p.getClass()  
                        .getDeclaredMethod(annotationField, null);  
                String[] values = (String[]) m.invoke(p, null);  
                for (String key : values) {  
                    System.out.println("注解值 === " + key);  
                    map.put(key, key);  
                }  
            }  
        }  
        System.out.println("map数量  === " + map.size());  
        return map;  
    }  
  
    public static void main(String[] args) throws Exception {  
  
        AnnotationUtil.getInstance().loadVlaue(Privilege.class, "value",  
                TestPrivilege.class.getName());  
    }  
  
}  

4. 执行结果

处理Annotation类名称  === cn.veji.hibernate.po.Privilege  
处理Annotation类属性名称  === value  
处理Annotation的调用类名称  === cn.veji.hibernate.po.TestPrivilege  
注解值 === c  
注解值 === a  
注解值 === b  
map数量  === 3 

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

猜你喜欢

转载自www.cnblogs.com/sunada2005/p/10865320.html