Home Java의 Annotation 알아보기 - @Target
Post
Cancel

Java의 Annotation 알아보기 - @Target

출처: Package org.springframework.core.annotation / Annotation Interface Target


@Target

An annotation of type java.lang.annotation.Target is used on the declaration of an annotation interface A to specify the contexts in which A is applicable. java.lang.annotation.Target has a single element, value, of type java.lang.annotation.ElementType[], to specify contexts.

    java.lang.annotation.Target 타입인 애너테이션은
애너테이션 인터페이스 A 선언이
적용가능하다는 문맥을
명시하는 데에 사용된다.
java.lang.annotation.Target
java.lang.annotation.ElementType[]
단일 요소, value를 가지며
이를 통해 문맥을 명시한다.


1
2
3
4
5
6
7
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {

    ElementType[] value();
}

    이 애너테이션은,
해당 애너테이션 인터페이스가
적용 가능applicable한 문맥에 있음을 알려준다.
애너테이션 인터페이스가 적용가능한
선언 문맥과 타입 문맥이란 것은
JSL 9.6.4.1에 명시되어 있다.

@Taget 메타 애너테이션이
T라는 애너테이션 인터페이스에
붙어있지 않다면,
T 타입의 애너테이션은
어느 선언에서든
제어자modifier로 작성될 것이다.

반대로, T에 붙어있다면,
컴파일러는 열거형 상수 중에서
ElementType을 이용하여
사용 제한 설정을 강제할 것이다.
이는 JLS 9.7.4에 나와있다.

예를 들어,
아래의 @Taget 메타 애너테이션은
선언된 인터페이스 스스로가
메타 애너테이션 인터페이스임을 나타낸다.
이는 오직
애너테이션 인터페이스 선언에만 쓰인다.

1
2
3
4
@Target(ElementType.ANNOTATION_TYPE)
public @interface MetaAnnotationType {
    ...
}
Contents