HomeAboutMeBlogGuest
© 2025 Sejin Cha. All rights reserved.
Built with Next.js, deployed on Vercel
🌚
[New] 우기팀
/
득윤
득윤
/
🕛
Java Annotations
🕛

Java Annotations

 
@) 참고
The Java™ Tutorials - Annotations
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.
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을 선언하고 사용해보자!

 
static class Generation3List { // Author: John Doe // Date: 3/17/2002 // Current revision: 6 // Last modified: 4/12/2004 // By: Jane Doe // Reviewers: Alice, Bill, Cindy // class code goes here }
전통적인 주석 방식의 MetaData 작성
 
annotation 방식을 도입해보자!
@interface ClassPreamble { String author(); String date(); int currentRevision() default 1; String lastModified() default "N/A"; String lastModifiedBy() default "N/A"; // Note use of array String[] reviewers(); }
@ClassPreamble 선언
@ClassPreamble ( author = "John Doe", date = "3/17/2002", currentRevision = 6, lastModified = "4/12/2004", lastModifiedBy = "Jane Doe", reviewers = {"Alice", "Bob", "Cindy"} ) static class Generation4List { // class code goes here }
어노테이션 추가

Meta-Annotations

Annotations that apply to other annotations are called meta-annotations. There are several meta-annotation types defined in java.lang.annotation.
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를 추가하면 된다.
    • // import this to use @Documented import java.lang.annotation.*; @Documented @interface ClassPreamble { // Annotation element definitions }
 
@Target
  • annotation이 삽입될 수 있는 장소를 제한한다.
  • ElementTime.
    • ANNOTATION_TYPE
    • CONSTRUCTOR
    • FIELD
    • LOCAL_VARIABLE
    • METHOD
    • PACKAGE
    • PARAMETER
    • TYPE
 
@Inherited
  • annotation의 정보가 상속된 클래스에 전달되는 여부를 설정한다.
  • 클래스의 선언에만 붙일 수 있다.
 
@Repeatable
  • 같은 annotation을 여러번 작성할 수 있는지 여부를 설정한다.
 

어노테이션을 선언하는 법

 
AnnotationInterfaceDeclaration: {InterfaceModifier} @interface TypeIdentifier AnnotationInterfaceBody
 
  • Annotation은 sealed 혹은 non-sealed 접근제어자를 가질 수 없다.
  • annotation interface는 top level 혹은 member interface로 선언 될 수 있지만 local interface로는 선언된 수 없다.
class C { @interface A1 {} /* Legal: an annotation interface can be a member interface */ void m() { @interface A2 {} /* Illegal: an annotation interface cannot be a local interface */ class D { @interface A3 {} /* Illegal: an annotation interface cannot be specified anywhere within the body of local class D */ class E { @interface A4 {} /* Illegal: an annotation interface cannot be specified anywhere within the body of local class D, even as a member of a class E nested in D */ } } } }
 
  • Annotation interface는 Generic이 될 수 없다.
  • Annotation interface의 direct superinterface는 java.lang.annotation.Annotation이다.
  • element가 하나도 없는 annotation interface를 marker annotation interface라고 한다.
  • single-element annotation의 경우 변수명을 value로 하는 것이 컨벤션이다.
    • @PostMapping(value = “/api/user”) → value = 생략 가능
  • annotation의 element에 default 값을 설정할 수 있다.
    • @interface RequestForEnhancement { int id(); // No default - must be specified in // each annotation String synopsis(); // No default - must be specified in // each annotation String engineer() default "[unassigned]"; String date() default "[unimplemented]"; }
 

Annotation Processor

에 대한 학습은 아래 포스팅을 읽는 것으로 대체하겠습니다.
Annotation Processing 101 (번역)
 
너무 어렵습니다.