zoukankan      html  css  js  c++  java
  • java-注解

    http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html

    三步走:

    1.注解定义。

    /**
     * 注解定义
     * Created by pengliang.zhan on 2016/10/24.
     */
    @Target({ElementType.TYPE,ElementType.METHOD,ElementType.FIELD,ElementType.CONSTRUCTOR})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface TestA {
        String name() ;
        int id() default 0;
        Class<Long> gid();
    }

    2.注解使用。

    /**
     * 注解使用
     * Created by pengliang.zhan on 2016/10/24.
     */
    @TestA(name="type",gid=Long.class)
    public class UserAnnotation {
        @TestA(name="param",id=1,gid=Long.class) // 使用了类成员注解
        private Integer age;
    
        @TestA(name="construct",id=2,gid=Long.class)// 使用了构造方法注解
        public UserAnnotation() {
        }
    
        @TestA(name="public method", id=3, gid=Long.class)// 使用了 public 方法注解
        public void a() {
        }
    
        @TestA(name="protected method", id=4, gid=Long.class)//protected 方法注解
        protected void b() {
        }
    
        @TestA(name="private method " , id = 5, gid=Long.class) // private 方法注解
        private void c(){
    
        }
    }

    3.注解处理器。

    /**
     * 注解解析
     * Created by pengliang.zhan on 2016/10/24.
     */
    public class ParseAnnotation {
        /**
         * 简单打印出UserAnnotation 类中所使用到的类注解
         * 该方法只打印了 Type 类型的注解
         * @throws ClassNotFoundException
         */
        public static void parseTypeAnnotation() throws ClassNotFoundException{
            Class clazz = Class.forName("test.annotation.UserAnnotation");
            Annotation[] annotations = clazz.getAnnotations();
            for (Annotation annotation : annotations) {
                TestA testA = (TestA) annotation;
                System.out.println("type name = "+clazz.getName() + "  |  id = " + testA.id() + "  |  name = " + testA.name() + "  |                      gid = " + testA.gid());
            }
        }
    
        /**
         * 简单打印出UserAnnotation 类中所使用到的方法注解
         * 该方法只打印了 Method 类型的注解
         * @throws ClassNotFoundException
         */
        public static void parseMethodAnnotation() throws ClassNotFoundException{
            Method[] methods = UserAnnotation.class.getDeclaredMethods();
            for (Method method : methods) {
                 /*
                 * 判断方法中是否有指定注解类型的注解
                 */
                boolean hasAnnotation = method.isAnnotationPresent(TestA.class);
                if(hasAnnotation){
                    TestA annotation = method.getAnnotation(TestA.class);
                    System.out.println("method name = " + method.getName() + "  |  id = " +
                            annotation.id() + "  |  description = " + annotation.name() + "  |  gid = " + annotation.gid());
                }
            }
        }
    
        /**
         * 简单打印出UserAnnotation 类中所使用到的构造方法注解
         * 该方法只打印了 构造方法 类型的注解
         * @throws ClassNotFoundException
         */
        public static void parseConstructAnnotation()  throws ClassNotFoundException{
            Constructor[] constructors = UserAnnotation.class.getConstructors();
            for (Constructor constructor : constructors) {
                /*
                 * 判断构造方法中是否有指定注解类型的注解
                 */
                boolean hasAnnotation = constructor.isAnnotationPresent(TestA.class);
                if(hasAnnotation){
                     /*
                     * 根据注解类型返回方法的指定类型注解
                     */
                    TestA annotation = (TestA) constructor.getAnnotation(TestA.class);
                    System.out.println("constructor = " + constructor.getName()
                            + "   |   id = " + annotation.id() + "  |  description = "
                            + annotation.name() + "  |   gid= "+annotation.gid());
                }
            }
        }
    
        /**
         * 简单打印出UserAnnotation 类中所使用到的字段注解
         * 该方法只打印了 Method 类型的注解
         * @throws ClassNotFoundException
         */
        public static void parseFieldAnnotation() throws ClassNotFoundException{
            Field[] fields = UserAnnotation.class.getDeclaredFields();
            for (Field field : fields) {
                boolean hasAnnotation = field.isAnnotationPresent(TestA.class);
                if(hasAnnotation){
                    TestA annotation = field.getAnnotation(TestA.class);
                    System.out.println("Field = " + field.getName()
                            + "  |  id = " + annotation.id() + "  |  description = "
                            + annotation.name() + "  |  gid= "+annotation.gid());
                }
            }
        }
    
        public static void main(String[] args) throws ClassNotFoundException {
            System.out.println("------------------------------解析Type注解----------------------------------------------------------");
            parseTypeAnnotation();
            System.out.println("------------------------------解析Method注解-------------------------------------------------------");
            parseMethodAnnotation();
            System.out.println("------------------------------解析构造方法(Construct)注解------------------------------------------");
            parseConstructAnnotation();
            System.out.println("------------------------------解析字段(Field)注解-----------------------------------------------------");
            parseFieldAnnotation();
        }
    }

    4.注解定义时使用的元注解:@Target,@Retention,@Documented,@Inherited

    @Target : 表示该注解用于什么地方,可能的值在枚举类 ElemenetType 中,包括: 
              ElemenetType.CONSTRUCTOR        构造器声明 
              ElemenetType.FIELD               域声明(包括 enum 实例) 
              ElemenetType.LOCAL_VARIABLE     局部变量声明 
              ElemenetType.METHOD              方法声明 
              ElemenetType.PACKAGE             包声明 
              ElemenetType.PARAMETER             参数声明 
              ElemenetType.TYPE                类,接口(包括注解类型)或enum声明

    @Retention : 表示在什么级别保存该注解信息。可选的参数值在枚举类型 RetentionPolicy 中,包括: 
              RetentionPolicy.SOURCE        注解将被编译器丢弃 
              RetentionPolicy.CLASS       注解在class文件中可用,但会被VM丢弃 
              RetentionPolicy.RUNTIME     VM将在运行期也保留注释,因此可以通过反射机制读取注解的信息。

    @Documented : 将此注解包含在 javadoc 中 ,它代表着此注解会被javadoc工具提取成文档。在doc文档中的内容会因为此注解的信息内容不同而不同。相当与@see,@param 等。

    @Inherited : 在您定义注解后并使用于程序代码上时,默认父类别中的注解并不会被继承至子类别中。您可以在定义注解时加上java.lang.annotation.Inherited 限定的Annotation,这让您定义的Annotation型别被继承下来。注意注解继承只针对class 级别注解有效

    5.注解处理器。

    一般情况下,我们开发使用的是运行时注解。  可以通过反射机制,解析。

  • 相关阅读:
    文本比较算法Ⅴ——回顾贴,对前面几篇文章的回顾与质疑
    键盘监控的实现Ⅱ——容易产生误解的CallNextHookEx函数
    利用WPF建立自适应窗口大小布局的WinForm窗口
    计算机中的颜色XIII——颜色转换的快速计算公式
    键盘监控的实现Ⅲ——按键消息的修改(映射)
    计算机中的颜色XI——从色相值到纯色的快速计算(新的公式)
    Dot Net中InputLanguage对象的使用限制
    计算机中的颜色XII——快速计算纯色的色相值(新的公式)
    关于房产中介网的设计随想
    java笔记:熟练掌握线程技术基础篇之解决资源共享的问题(中)前篇
  • 原文地址:https://www.cnblogs.com/DengGao/p/5994407.html
Copyright © 2011-2022 走看看