android custom annotation

 
 
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface A{
    String value();
}

@Target:

  Scope of use of annotations

  The values ​​(ElementType) are:

             1.CONSTRUCTOR: used to describe the constructor
    2. ElementType. FIELD : used to describe the domain 
    3.LOCAL_VARIABLE: used to describe the local variable 
    4. ElementType. METHOD : used to describe the method 
    5.PACKAGE: used to describe the package 
    6.PARAMETER: Used to describe parameters 
    7. ElementType. TYPE : used to describe classes, interfaces (including annotation types) or enum declarations

@Retention:

  @Retention defines how long the Annotation is retained: some Annotations only appear in the source code and are discarded by the compiler; others are compiled in the class file; Annotation compiled in the class file may be virtualized The machine ignores, and others will be read when the class is loaded (please note that it does not affect the execution of the class, because Annotation and class are used separately). Use this meta-Annotation to limit the "lifetime" of Annotation. 
  Function: Indicates at what level the annotation information needs to be saved, used to describe the life cycle of the annotation (ie: within what range is the described annotation valid). The 
  values ​​(RetentionPoicy) are: 
    1. SOURCE: Valid in the source file (ie Source file retention) 
    2. CLASS: Valid in class files (ie class retention) 

    3.RUNTIME: Valid at runtime (that is, reserved at runtime)

1. Custom annotations

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();  

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

From the above code, we can see the scope corresponding to Target (Target can accept multiple parameters, separated by commas)

E.g:
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestIn {
    String author() default "kaelthas.wang";
    String address() default "Qingdao, Shandong";

}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2. Get class annotation attributes

/**
     * read annotation value
     *  
     * @param annotationClasss handles Annotation class names
     * @param annotationField handles the Annotation class attribute name
     * @param className handles the class name used for Annotation
     * @return
     * @throws Exception
     */  
    @SuppressWarnings("all")  
    public Map<String, String> loadVlaue(Class annotationClasss,  
            String annotationField, String className) throws Exception {  

        System.out.println("Processing Annotation class name === "+annotationClasss.getName());  
        System.out.println("Processing Annotation class attribute name === "+annotationField);  
        System.out.println("Processing Annotation's calling class name === "+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);  
                //Here is forced type conversion according to the attribute parameter type        
                String[] values = (String[]) m.invoke(p, null);  
                for (String key : values) {  
                    System.out.println("Annotation value === " + key);  
                    map.put(key, key);  
                }  
            }  
        }  
        System.out.println("map数量  === " + map.size());  
        return map;  
    } 

 
 
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

3.获取方法注解属性值

这里的属性值为int 强制类型转换时候使用Integer

 
 4.执行结果: 
  
@Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestIn {
    String author() default "kaelthas.wang";
    String address() default "山东青岛";

}

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();  

}

@SuppressWarnings("all")
    public int loadVlaue(Class annotationClasss,
                         String annotationField, String className) {

        System.out.println("Processing Annotation class name === " + annotationClasss.getName());
        System.out.println("Processing Annotation class attribute name === " + annotationField);
        System.out.println("Processing Annotation's calling class name === " + className);
        Map<String, String> map = new HashMap<String, String>();

        try {
            Method[] methods = Class.forName(className).getDeclaredMethods();

            Class test = Class.forName(className);
            if (test.isAnnotationPresent(annotationClasss)) {
                Annotation p = test.getAnnotation(annotationClasss);
                Method m = p.getClass()
                        .getDeclaredMethod(annotationField, null);
                return (Integer) m.invoke(p, null);

            }
        } catch (Exception e) {
            return -1;
        }

        return -1;

    }


Handling Annotation class name === cn.veji.hibernate.po.Privilege  
Handling Annotation class property name === value  
The name of the calling class that handles Annotation === cn.veji.hibernate.po.TestPrivilege  
Annotation value === c  
Annotation value === a  
Annotation value === b  
Number of maps === 3  

Reference: http://xuwenjin666.iteye.com/blog/1637247

      http://blog.csdn.net/hai_qing_xu_kong/article/details/51779695


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324811933&siteId=291194637