• 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)

  • 相关阅读:
    GridView分页用法
    鼠标移动 改变Datagrid行的背景颜色
    asp.net清空某一类控件或置某一状态
    解决XP系统下"HTTP 错误 403.9 禁止访问:连接的用户过多"的问题
    Asp.net项目路径获取方法
    误删资料恢复 技巧(转载)
    linux命令
    破解win2003“终端服务器授权”激活许可证! (转载)
    apache搭建网站更改默认语言为GB2312
    jquery实现图片广告轮换效果
  • 原文地址:https://www.cnblogs.com/zemliu/p/2924000.html
Copyright © 2020-2023  润新知