• 注解


    注解的分类

      1.标准注解

      2.元注解

        @Retention :注解的生命周期

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        /**
         * Returns the retention policy.
         * @return the retention policy
         */
        RetentionPolicy value();
    }
    public enum RetentionPolicy {
        /**
         * Annotations are to be discarded by the compiler.
         */
        SOURCE,
    
        /**
         * Annotations are to be recorded in the class file by the compiler
         * but need not be retained by the VM at run time.  This is the default
         * behavior.
         */
        CLASS,
    
        /**
         * Annotations are to be recorded in the class file by the compiler and
         * retained by the VM at run time, so they may be read reflectively.
         *
         * @see java.lang.reflect.AnnotatedElement
         */
        RUNTIME
    }

    SOURCE:只作用在java类中,.class里没有这个注解

    .CLASS:在.class里也会有这个注解

    .RUNTIME:在运行时这个注解也会起到作用.

        @Target :注解的作用目标

          

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        /**
         * Returns an array of the kinds of elements an annotation type
         * can be applied to.
         * @return an array of the kinds of elements an annotation type
         * can be applied to
         */
        ElementType[] value();
    }
    public enum ElementType {
        /** Class, interface (including annotation type), or enum declaration */
        TYPE,
    
        /** Field declaration (includes enum constants) */
        FIELD,
    
        /** Method declaration */
        METHOD,
    
        /** Formal parameter declaration */
        PARAMETER,
    
        /** Constructor declaration */
        CONSTRUCTOR,
    
        /** Local variable declaration */
        LOCAL_VARIABLE,
    
        /** Annotation type declaration */
        ANNOTATION_TYPE,
    
        /** Package declaration */
        PACKAGE,
    
        /**
         * Type parameter declaration
         *
         * @since 1.8
         */
        TYPE_PARAMETER,
    
        /**
         * Use of a type
         *
         * @since 1.8
         */
        TYPE_USE
    }

     TYPE:作用在类上

     Field:作用在属性上

     Method:作用在方法上

        @Inherited :是否允许之类继承该注解

        @Documented

    自定义注解的实现

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface PersonAnnotation {
        String name();
        int age();
        String gender() default  "";
        String[] language();
    }

    获取注解的信息

    public static void main(String[] args) throws Exception {
            Class clazz = Class.forName("Annotation.Person");
            PersonAnnotation annotation = (PersonAnnotation) clazz.getAnnotation(PersonAnnotation.class);
            System.out.println(annotation.age()+"
    "+annotation.gender()+"
    "+annotation.name());
            for(String str : annotation.language()){
                System.out.println(str+" ");
            }
        }

    通过反射可以获取到注解的信息,但前提是注解的生命周期必须是Runntime.

  • 相关阅读:
    处理不同方向的文本1.0
    CSS盒模型
    费德曼学习法
    [转]Photoshop中的高斯模糊、高反差保留和Halcon中的rft频域分析研究
    [转]仿射变换及其变换矩阵的理解
    [转]Scintilla开源库使用指南(一
    [转]Scintilla开源库使用指南(二
    [转]C#中WinForm窗体事件的执行次序
    [转]透过IL看C#:switch语句(转)
    [转]程序员必读书单(转)
  • 原文地址:https://www.cnblogs.com/lzh66/p/13526420.html
Copyright © 2020-2023  润新知