@) 참고
The Java® Language Specification - 9.6. Annotation Interfaces
The Java® Language Specification - 9.7. Annotations
Hannes Dorfmann/ Jason Kim - Annotation Processing 101 (번역)
Annotations
a form of metadata, provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate.
어노테이션의 대표적인 사용 이유는 아래와 같다.
- Information for the compiler
- e.g.)
@Override
@FunctionalInterface
,SuppressWarnings("unchecked")
- Compile-time and deployment-time processing
- Runtime processing
@Retention(RetentionPolicy.Runtime)
Annotation Type을 선언하고 사용해보자!
annotation 방식을 도입해보자!
Meta-Annotations
Annotations that apply to other annotations are called meta-annotations. There are several meta-annotation types defined in
java.lang.annotation
.@Retention
- marked annotation의 저장 장소를 설정함
RetentionPolicy.
SOURCE
: source level, compiler에게 무시됨(@Override
???)CLASS
- compiler에게는 전달, JVM은 무시RUNTIME
- JVM까지 전달. runtime 환경에서 사용가능
@Documented
- 기본적으로 annotation의 정보는 javadoc에 전달되지 않는다.
- Javadoc-generated documentation에
@ClassPreamble
의 내용을 추가시키고 싶으면@ClassPreamble
의 선언에@Documented
를 추가하면 된다.
@Target
- annotation이 삽입될 수 있는 장소를 제한한다.
ElementTime.
ANNOTATION_TYPE
CONSTRUCTOR
FIELD
LOCAL_VARIABLE
METHOD
PACKAGE
PARAMETER
TYPE
@Inherited
- annotation의 정보가 상속된 클래스에 전달되는 여부를 설정한다.
- 클래스의 선언에만 붙일 수 있다.
@Repeatable
- 같은 annotation을 여러번 작성할 수 있는지 여부를 설정한다.
어노테이션을 선언하는 법
- Annotation은
sealed
혹은non-sealed
접근제어자를 가질 수 없다.
- annotation interface는 top level 혹은 member interface로 선언 될 수 있지만 local interface로는 선언된 수 없다.
Annotation interface
는 Generic이 될 수 없다.
Annotation interface
의 direct superinterface는java.lang.annotation.Annotatio
n이다.
- element가 하나도 없는 annotation interface를 marker annotation interface라고 한다.
- single-element annotation의 경우 변수명을 value로 하는 것이 컨벤션이다.
@PostMapping(value = “/api/user”)
→value =
생략 가능
- annotation의 element에 default 값을 설정할 수 있다.
Annotation Processor
에 대한 학습은 아래 포스팅을 읽는 것으로 대체하겠습니다.
너무 어렵습니다.