• Java 注解


    public class Annote {
    
        @Retention(RetentionPolicy.RUNTIME)
        @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
        public @interface RequiredRoles{
            String name();
            int id() default 0;
        }
        
        @RequiredRoles(name="type") //类成员注解
        public class UserAnnotation {
            
            @RequiredRoles(name="param", id=1) //类成员注解
            private Integer age;
            
            @RequiredRoles (name="construct", id=2)//构造方法注解
            public UserAnnotation(){
            }
            
            @RequiredRoles(name="public method", id=3) //类方法注解
            public void a(){
                Map m = new HashMap(0);
            }
            
            @RequiredRoles(name="protected method", id=4) //类方法注解
            protected void b(){
                Map m = new HashMap(0);
            }
            
            @RequiredRoles(name="private method", id=5) //类方法注解
            private void c(){
                Map m = new HashMap(0);
            }
        }
        
        public static void parseTypeAnnotation() throws ClassNotFoundException {  
            Class clazz = Class.forName("Annote$UserAnnotation");        
            Annotation[] annotations = clazz.getAnnotations();
            
            for (Annotation annotation : annotations) {  
                RequiredRoles testA = (RequiredRoles)annotation;
                System.out.println("id= "+testA.id()+" name= "+testA.name());  
            }  
        } 
        
        public static void parseMethodAnnotation(){
            Method[] methods = UserAnnotation.class.getDeclaredMethods();  
            for (Method method : methods) {  
                
                boolean hasAnnotation = method.isAnnotationPresent(RequiredRoles.class);  
                if (hasAnnotation) {                  
                    RequiredRoles annotation = method.getAnnotation(RequiredRoles.class);  
                    System.out.println("method = " + method.getName()  + " ; id = " + annotation.id() + " ; description = "+ annotation.name());  
                }  
            }  
        }
        
        
        public static void main(String[] args) throws ClassNotFoundException{
            parseTypeAnnotation();
            
            parseMethodAnnotation();
        }
        
    }

    详细参考:http://blog.sina.com.cn/s/blog_93dc666c0101gzn5.html

    1、注解本质是就是对类、属性、方法的注释
    2、区别是,如果在声明注解的时候加上了 @Retention(RetentionPolicy.RUNTIME) 属性, 就可以在运行的时候通过代码读取到具体的注解内容,而注释在运行时是读取不到的

    http://baike.baidu.com/link?url=xToxlcR_PfOKUq4NOxxB1_LfwbFcAVYyGUci9iCgiiyLavSVRAqieOWQ9BJ9IWbk1thMYhAcrr78mr0sLCpgGK

  • 相关阅读:
    蔚来NIO Pilot 全球首试 终于摘掉半成品帽子
    这个机械手可精准灵活地接住一颗掉落的棉花糖并使其“安然无恙”
    谷歌已经领先 那么苹果的人工智能战略又在哪里
    你见过机器船吗?MIT新版ROBOAT可以自动组装
    谷歌、亚马逊、苹果三大语音助手发展现状 谁被甩在身后了
    Python中的十大图像处理工具
    测试用例设计理论
    Flask+Mysql搭建网站之数据库问题
    Flask+Mysql搭建网站之网页设计
    Flask+Mysql搭建网站之安装Mysql
  • 原文地址:https://www.cnblogs.com/lipeil/p/4807551.html
Copyright © 2020-2023  润新知