ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] Annotation
    Study/Effective Java 2023. 8. 25. 07:22

    @Documented

    : javadoc을 만들 때 annotation 정보가 포함된다.

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Documented {
    }

    Document Annotation 코드를 보면 런타임시 참조할 수 있고 Annotation Type에만 선언이 가능하다.

     예제

    @Documented
    public @interface MyAnnotation {
    
    }

     

    Annotation에 사용 가능하다.

     

    @Retention

    : annotation 정보를 언제까지 참조할 수 있는지 설정한다.

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Retention {
        /**
         * Returns the retention policy.
         * @return the retention policy
         */
        RetentionPolicy value();
    }

    ▼ 예제

    @Retention(RetentionPolicy.SOURCE)
    public @interface MyAnnotation {
    
    }

    RetensionPolicy에는 SOURCE, CLASS, RUNTIME 세 종류가 있다.

    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
    }

     

    RetentionPolicy.SOURCE // 컴파일시에만 사용.

    RetentionPolicy.RUNTIME // default. RUNTIME 중 참조 가능.

    RetentionPolicy.CLASS // 컴파일하면 생기는 class 파일에서까지 참조.

    @MyAnnotation
    public class MyClass {
        public static void main(String[] args) {
            Arrays.stream(MyClass.class.getAnnotations()).forEach(System.out::println);
            // @Retention(RetentionPolicy.SOURCE), @Retention(RetentionPolicy.CLASS)
            // 아무것도 출력X
            // @Retention(RetentionPolicy.RUNTIME) // default. 런타임에도 참조 가능.
            // 출력 : @me.whiteship.chapter05.item27.annotation.MyAnnotation()
        }
    }

     

    @Target

    : 이 annotation 을 선언할 수 있는 위치를 설정한다.

    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.ANNOTATION_TYPE)
    public @interface Target {
        /**
         * Returns an array of the kinds of elements an annotation interface
         * can be applied to.
         * @return an array of the kinds of elements an annotation interface
         * can be applied to
         */
        ElementType[] value();
    }

    ▼ 예제

    @Target({ElementType.TYPE, ElementType.METHOD})
    public @interface MyAnnotation {
    
    }

    ElementType 종류를 한 번 보고 넘어가자.

     

    public enum ElementType {
        /** Class, interface (including annotation interface), enum, or record
         * declaration */
        TYPE,
    
        /** Field declaration (includes enum constants) */
        FIELD,
    
        /** Method declaration */
        METHOD,
    
        /** Formal parameter declaration */
        PARAMETER,
    
        /** Constructor declaration */
        CONSTRUCTOR,
    
        /** Local variable declaration */
        LOCAL_VARIABLE,
    
        /** Annotation interface declaration (Formerly known as an annotation type.) */
        ANNOTATION_TYPE,
    
        /** Package declaration */
        PACKAGE,
    
        /**
         * Type parameter declaration
         *
         * @since 1.8
         */
        TYPE_PARAMETER,
    
        /**
         * Use of a type
         *
         * @since 1.8
         */
        TYPE_USE,
    
        /**
         * Module declaration.
         *
         * @since 9
         */
        MODULE,
    
        /**
         * Record component
         *
         * @jls 8.10.3 Record Members
         * @jls 9.7.4 Where Annotations May Appear
         *
         * @since 16
         */
        RECORD_COMPONENT;
    }
    @MyAnnotation // ElementType.TYPE 선언시 위치 가능
    public class MyClass {
    
        @MyAnnotation // ElementType.METHOD 선언시 위치 가능
        public static void main(String[] args) {
            Arrays.stream(MyClass.class.getAnnotations()).forEach(System.out::println);
        }
    }

     

    댓글

Designed by Tistory.