zoukankan      html  css  js  c++  java
  • 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)

  • 相关阅读:
    SenCha Touch AJAX跨域
    MS SQL 索引分析
    Tomcat性能优化(二) 启动参数设置
    PLSQL 连接不上64位ORACLE数据库解决办法
    PLSQL 配置连接ORACLE数据库
    Mybatis Batch 批量操作
    [No000014]听说不背单词,考英语会是这种下场-我们为什么必须背单词?
    [No000000]常用软件测试编译环境声明
    [No000013]在Office中关闭自动拼写检查和自动语法检查
    [No000012]编程中浮点数之什么是科学计数法
  • 原文地址:https://www.cnblogs.com/zemliu/p/2924000.html
Copyright © 2011-2022 走看看