Target
Target
이 문서에서는 Target 설정 옵션의 종류와 특징에 대해 다룬다.
@interface Target
Library에서 Target 코드를 찾아보면 다음과 같다. 편의상 주석은 제거하였다.
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Documented
- 작성된 Annotation이 Javadoc에 문서화 됨을 표시@Retention
- 현재 Annotation이 RUNTIME 시점까지 유지되어야 함을 표시
enum ElementType
Target Annotation 내부에는 ElementType이 배열 형태로 설정될 수 있다. ElementType은 열거형(enum)으로 다음과 같이 구현되어 있다.
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
}
설정 가능한 ElementType 값은 다음과 같다.
기본
TYPE - 클래스, 인터페이스, Annotation, Enum을 대상으로 지정
FIELD - 열거형 상수를 포함한 필드를 대상으로 지정
METHOD - 메소드를 대상으로 지정
PARAMETER - 매개변수를 대상으로 지정
CONSTRUCTOR - 생성자를 대상으로 지정
LOCAL_VARIABLE - 지역변수를 대상으로 지정
ANNOTATION_TYPE - Annotation을 대상으로 지정
PACKAGE - Package를 대상으로 지정(package-info.java에서 사용 가능)
추가(JDK 1.8+)
TYPE_PARAMETER - 타입 파라미터(in 제네릭)를 대상으로 지정
TYPE_USE - 타입 파라미터를 포함한 모든 타입을 대상으로 지정
ElementType.TYPE
@Target(ElementType.TYPE)
public @interface TypeAnnotation {
}
ElementType.FIELD
@Target(ElementType.FIELD)
public @interface FieldAnnotation {
}
ElementType.METHOD
@Target(ElementType.METHOD)
public @interface MethodAnnotation {
}
ElementType.PARAMETER
@Target(ElementType.PARAMETER)
public @interface ParameterAnnotation {
}
ElementType.CONSTRUCTOR
@Target(ElementType.CONSTRUCTOR)
public @interface ConstructorAnnotation {
}
ElementType.LOCAL_VARIABLE
@Target(ElementType.LOCAL_VARIABLE)
public @interface LocalVariableAnnotation {
}
ElementType.ANNOTATION_TYPE
@Target(ElementType.ANNOTATION_TYPE)
public @interface AnnotationTypeAnnotation {
}
ElementType.PACKAGE
@Target(ElementType.PACKAGE)
public @interface PackageAnnotation {
}
ElementType.TYPE_PARAMETER
ElementType.TYPE_USE
@Target(ElementType.TYPE_USE)
public @interface TypeUseAnnotation {
}
예제 - Annotation 작성 가능 위치 확인
@TypeAnnotation
@AnnotationTypeAnnotation
@interface TestAnnotation {
}
@TypeAnnotation
@TypeUseAnnotation
public class TestClass {
@FieldAnnotation
@TypeUseAnnotation
private String fieldExample;
@MethodAnnotation
//public int methodExample(int a, int b)
public @TypeUseAnnotation int methodExample(@ParameterAnnotation int a, @TypeUseAnnotation int b) {
@LocalVariableAnnotation
int total = a + b;
return total;
}
@ConstructorAnnotation
//public Testclass(String arg0, String arg1)
public TestClass(@ParameterAnnotation String arg0, @TypeUseAnnotation String arg1) {
}
//public static <T>T genericMethodExample(T a)
public static <@TypeParameterAnnotation T> @TypeUseAnnotation T genericMethodExample(@TypeUseAnnotation T a){
System.out.println(a);
return a;
}
}
Last updated