[Annotation] @Target, @Retention

이주현·2021년 12월 30일
0

Annotation

목록 보기
1/5
post-thumbnail

Custom Annotation 생성하기

  • Custom Annotation을 생성하려면 @Target, @Retention Annotation을 사용합니다.

사용 예시

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) // SOURCE, CLASS 사용
public @interface MyAnnotation {
	//blah blah...
}

@Target, @Retention 알아보기

@Target

  • 자신이 만든 어노테이션이 사용되게될 자바 요소를 지정할 수 있습니다.
<!-- ElementType 종류 -->
ElementType.ANNOTATION_TYPE
ElementType.CONSTRUCTOR
ElementType.FIELD
ElementType.LOCAL_VARIABLE
ElementType.METHOD
ElementType.PACKAGE
ElementType.PARAMETER
ElementType.TYPE

@Retention

  • Annotation의 유지할 기간을 정합니다.
  • 유형 선언이 없는 경우 RetentionPolicy은 Default로 CLASS 입니다.


RetentionPolicy

enum

  • SOURCE : Source 까지 유지됩니다. (Compiler에 의해 폐기됩니다.)
  • CLASS : Class 까지 유지됩니다. (Compiler에 의해 class file에 기록되지만 Runtime에 VM에 의해 유지될 필요는 없습니다. )
  • RUNTIME : Runtime 까지 유지됩니다. (Compiler에 의해 class 파일에 기록되고 Runtime에 VM에 의해 유지되어 반사적으로 읽힐 수 있습니다. / Reflection API로 Annotation 조회가 가능합니다.)
profile
아직

0개의 댓글