Java 注解

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

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值