简单使用,利用反射获取注解值

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/** @Target标识该注解使用范围,Method表示用于方法声明处 */
@Target(ElementType.METHOD)
/** @Retention标志该注解的使用时机,RUNTIME表示VM运行时保留注解 */
@Retention(RetentionPolicy.RUNTIME)
public @interface UserCase { 
    public int id();
    public String description() default "no description";
}

package annotation;

import java.lang.reflect.Method;

public class Tracker {
    @UserCase(id = 47, description =
            "Password can't be empty")
    public boolean validatePwd(String pwd) {
        return pwd.matches(".+");
    }
    
    @UserCase(id = 48, description =
            "Username can't be empty")
    public boolean validateUsername(String username) {
        return username.matches(".+");
    }
    
    public void trackUserCases(Class<?> cls) {
        for (Method m : cls.getDeclaredMethods()) {
            UserCase uc = m.getAnnotation(UserCase.class);
            if (uc != null)
                System.out.println("Found usercase id : " + uc.id()
                        + " description : " + uc.description());
        }
    }
    
    public static void main(String[] args) {
        Tracker t = new Tracker();
        t.trackUserCases(Tracker.class);
    }
}

输出

Found usercase id : 47 description : Password can't be empty
Found usercase id : 48 description : Username can't be empty

 

注解的嵌套使用

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Constraints {
    boolean primaryKey() default false;
    boolean allowNull() default true;
    boolean unique() default false;
}

package annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SQLInteger {
    String name() default "";
    Constraints constrains() default @Constraints;
}

package annotation;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;

public class Test {
    @SQLInteger(name="id") int pk;
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> cls = Class.forName("annotation.Test");
        for (Field f : cls.getDeclaredFields()) {
            Annotation[] anns = f.getDeclaredAnnotations();
            for (Annotation a : anns) {
                if (a instanceof SQLInteger) {
                    SQLInteger sInt = (SQLInteger) a;
                    System.out.println(f.getName());
                    System.out.println(sInt.name());
                    System.out.println(sInt.constrains());
                }
            }
        }
    }
}

输出

pk
id
@annotation.Constraints(unique=false, primaryKey=false, allowNull=true)

posted on 2013-02-24 10:31  ZimZz  阅读(274)  评论(0编辑  收藏  举报