어노테이션은 기본적으로 메타 어노테이션으로 구성되어 있다.
메타 어노테이션
- @Retention
- 자바 컴파일러가 어노테이션을 다루는 방법을 기술하며, 특정 시점까지 영향을 미치는지를 결정
종류
- RetentionPolicy.SOURCE - 컴파일 전까지만 유효하고 컴파일 하면 사라진다.
- RetentionPolicy.CLASS - 컴파일러가 클래스를 참조할때 까지만 유효하다. (class파일 에서도 존재는 하지만, invisible옵션을 통해 런타임 메모리에서는 제거됨)
- RetentionPolicy.RUNTIME - 런타임에도 어노테이션이 유효하므로 reflection사용 가능
- @Target
- 어노테이션이 적용될 수 있는 위치를 선택하는 메타 어노테이션
종류
- ElementType.PACKAGE
- ElementType.TYPE
- ElementType.ANNOTATION_TYPE
- ElementType.CONSTRUCTOR
- ElementType.FIELD
- ElementType.LOCAL_VARIABLE
- ElementType.METHOD
- ElementType.PARAMETER
- ElementType.TYPE_PARAMETER
- ElementType.TYPE_USE
- @Documented
- 어노테이션을 javadoc에 포함시킨다
- @Inherited
- 어노테이션을 상속 가능하도록 한다.
- @Repeatable
- 어노테이션을 적용시 반복 사용할 수 있게 한다.
후에 어노테이션을 활용할땐 아래와 같이 활용한다.
Class targetClass = 생성할클래스.class;
obj = targetClass.newInstance();
Field[] fields = targetClass.getDeclaredFields();
for (Field field : fields) {
InsertIntData annotation = field.getAnnotation(InsertIntData.class);
if (annotation == null) continue;
field.setAccessible(true);
field.set(obj, annotation.data());
}
'JAVA' 카테고리의 다른 글
java CompletableFuture (0) | 2023.07.07 |
---|---|
JPA @ElementCollection (0) | 2021.05.27 |
Jackson 어노테이션 정리 (0) | 2021.04.30 |
JPA 캐시 (0) | 2021.04.28 |
JPA 락 처리 (0) | 2021.04.28 |