어노테이션 타입 선언하기 Declaring an Annotation Type
많은 어노테이션들이 코드 안의 코멘트를 대체합니다.
소프트웨어 그룹이 전통적으로 모든 클래스의 몸체를 중요한 정보를 제공하는 코멘트로 시작한다고 가정합니다.
Many annotations replace comments in code.
Suppose that a software group traditionally starts the body of every class with comments providing important information:
public class Generation3List extends Generation2List { // 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 }
어노테이션을 가진 이러한 동일한 메타데이터를 추가하려면, 어노테이션 타입을 정의해야 합니다.
이를 수행하는 신택스는 아래와 같습니다:
To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is:
@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(); }
주석 타입 정의는 키워드 interface의 앞에 (@) (@ = AT, 주석 타입에서와 같이)가 오는 것과 유사합니다.
주석 타입은 나중 수업에서 다룰 인터페이스의 한 타입 입니다.
당분간, 인터페이스를 이해하지 않아도 됩니다.
이전의 주석 정의의 몸체에는, 메소드와 매우 비슷한 주석 타입 엘리먼트 선언이 포함되어 있습니다.
그들이 선택적 기본 값을 정의할 수 있음을 알아야 합니다.
this 처럼, 주석 타입이 정의된 후에, 채워진 값을 가진 해당 타입의 주석을 사용할 수 있습니다:
The annotation type definition looks similar to an interface definition where the keyword interface
is preceded by the at sign (@
) (@ = AT, as in annotation type). Annotation types are a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces.
The body of the previous annotation definition contains annotation type element declarations, which look a lot like methods. Note that they can define optional default values.
After the annotation type is defined, you can use annotations of that type, with the values filled in, like this:
@ClassPreamble (
author = "John Doe",
date = "3/17/2002",
currentRevision = 6,
lastModified = "4/12/2004",
lastModifiedBy = "Jane Doe",
// Note array notation
reviewers = {"Alice", "Bob", "Cindy"}
)
public class Generation3List extends Generation2List {
// class code goes here
}
Note: To make the information in
@ClassPreamble
appear in Javadoc-generated documentation, you must annotate the @ClassPreamble
definition with the @Documented
annotation:// import this to use @Documented
import java.lang.annotation.*;
@Documented
@interface ClassPreamble {
// Annotation element definitions
}
'Java 배우기' 카테고리의 다른 글
Type Annotations and Pluggable Type Systems (0) | 2016.01.05 |
---|---|
Predefined Annotation Types 미리 정의된 어노테이션 타입들 (0) | 2016.01.05 |
Annotations Basics (0) | 2016.01.05 |
Lesson: Annotations (0) | 2016.01.05 |
Questions and Exercises: Enum Types (0) | 2016.01.05 |
댓글