Week12(Annotation)
์ ๋
ธํ
์ด์
์ด๋
Annotation์ ์ฃผ์ ์ด๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ํ์ง๋ง /๋ /*์ ๊ฐ์ ์ฃผ์๊ณผ๋ ํ์คํ ๋ค๋ฅด๋ค
์ปดํ์ผ ๊ณผ์ ์์ ์ฝ๋๋ฅผ ์ด๋ป๊ฒ ์ปดํ์ผ ํ ๊ฒ์ธ์ง, ์คํ ๊ณผ์ ์์ ์ฝ๋๋ฅผ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊ฒ์ธ์ง๋ฅผ ์๋ ค์ฃผ๋ ์ ๋ณด
annotation์ด ๋ถ์ ์ฝ๋๋ annotation์ ๊ดํ ์ ๋ณด๊ฐ ๊ตฌํ๋ ๊ฒ์ ๋ฐ๋ผ์ ์ฐ๊ฒฐ๋๋ ๋ฐฉํฅ์ด ๊ฒฐ์ ๋๋ค
์ ์ฒด ์์ค์ฝ๋์์ ๋น์ง๋์ค ๋ก์ง์๋ ์ํฅ์ ์ฃผ์ง๋ ์์ง๋ง ํด๋น ํ๊ฒ์ ์ฐ๊ฒฐ ๋ฐฉ๋ฒ์ด๋ ์์ค์ฝ๋์ ๊ตฌ์กฐ๋ฅผ ๋ณ๊ฒฝํ ์ ์๋ค.
= ์ด ์์ฑ์ ์ด๋ค ์ฉ๋๋ก ์ฌ์ฉํ ๊ป์ง, ์ด ํด๋์ค์๊ฒ ์ด๋ค ์ญํ ์ ์ค๊ป์ง๋ฅผ ๊ฒฐ์ ํด์ ๋ถ์ฌ์ฃผ๋ ๊ฒ
annotation์ ์์ค์ฝ๋์ ๋ฉํ๋ฐ์ดํฐ๋ฅผ ์ฝ์ ํ๋ ๊ฒ์ด๊ธฐ ๋๋ฌธ์ ์ ์ด์ฉํ๋ค๋ฉด ๊ตฌ๋ ์ฑ ๋ฟ๋ง ์๋๋ผ ์ฒด๊ณ์ ์ธ ์์ค์ฝ๋๋ฅผ ๊ตฌ์ฑํ๋๋ฐ ๋์์ ์ค๋ค
๋ฉํ ๋ฐ์ดํฐ๋ -> ๋ฐ์ดํฐ๋ฅผ ์ํ ๋ฐ์ดํฐ๋ฅผ ์๋ฏธํ๋ฉฐ, ๋ฐ์ดํฐ์ ๋ํ ์ค๋ช ์ ์๋ฏธํ๋ ๋ฐ์ดํฐ(์์ ์ ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ ๋ฐ์ดํฐ)
์ฌ์ฉ๋ฒ
ํจํค์ง๋ช ์ด๋ ํด๋์ค ์ ์ธ, ๋ฉ์๋ ์ ์ธ, ํ๋ ์ ์ธ, ์ธ์๊ฐ ์ ์ธ ์์๋ค๊ฐ
@์ด๋ฆ(์ธ์)
๋ฐฉ์์ผ๋ก ๋ฃ์ด์ฃผ๊ณ ๋ง์ฝ ์ธ์๊ฐ ์์๊ฒฝ์ฐ์๋ (์ธ์) ๋ถ๋ถ์ ๋นผ๊ณ ์ฌ์ฉํ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ค
์ ์ธ๋ฐฉ๋ฒ
@Retention()
@Target()
public @interface ExAnnotation{
//ํ์
์์๋ช
() default๊ฐ;
String value(); // Stringํ ๊ธฐ๋ณธ ์์
int value2() default 5; //intํ ์์ ์ด๊ธฐ๊ฐ 5
}
์ด๋ ๊ฒ annotation์ ๋ฉค๋ฒ๋ฅผ ๊ฐ์ง๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ฉฐ, ํ์ , ์ด๋ฆ, dafault๊ฐ์ ์ค์ ํ๋ ๊ฒ์ด ๊ฐ๋ฅํ๋ค
๋ง์ฝ default๊ฐ์ ๋ฐ๋ก ์ ํด์ฃผ์ง ์๋๋ค๋ฉด ๊ธฐ๋ณธ ์์๊ฐ ๋๋ค
์ฌ์ฉ๋ฐฉ๋ฒ
@ExAnnotation(value = "Hello", value2 = 1);
@ExAnnoation("Hello");
๋ง์ฝ annotation ์์ ์๋ ์์์ ๊ฐ์ default๊ฐ์ด ์ค์ ๋์ด ์๋ค๋ฉด, ๊ฐ์ ๋ฐ๋ก ์ค์ ํ์ง ์์๋ ๋์ง๋ง(int) default๊ฐ์ด ์๋ ๊ฒฝ์ฐ์๋ ๊ฐ์ ์ค์ ํด์ค์ผ ํจ(String)
ํ์ค Annotation
@FunctionalInterface - Specifies that the type declaration is intended to be a functional interface, since Java 8.
@Override : ๋ฉ์๋๊ฐ override๋์๋์ง ํ์ธํ๊ณ ๋ง์ฝ ๋ถ๋ชจ ํด๋์ค๋ ๊ตฌํ๋ ์ธํฐํ์ด์ค์์ ๋ฉ์๋๋ฅผ ์ฐพ์ง ๋ชปํ์ ๋ ์ปดํ์ผ ๊ฒฝ๊ณ ๋ฅผ ๋ฐ์์ํจ๋ค
@Deprecated : ๋ฉ์๋์ ๋งํน์ ํด๋๊ณ ๋ง์ฝ ๋ฉ์๋๊ฐ ์ฌ์ฉ๋์์ ๋ ์ปดํ์ผ ๊ฒฝ๊ณ ๋ฅผ ์ค
@SafeVarargs : ์๋ฐ 7๋ถํฐ ๊ฐ๋ณ์ธ์ ๋งค๊ฐ๋ณ์๋ฅผ ์ฌ์ฉํ ๋ฉ์๋๋ ์์ฑ์๋ฅผ ๋ถ๋ฅด๋ ๊ฒ๋ค์ ๊ฒฝ๊ณ ๋ค์ ๋ฌด์
@SurppressWarnings : annotation ์ธ์๋ค ์์ ์ปดํ์ผ ์์ ๊ฒฝ๊ณ ๋ฅผ ์ปดํ์ผ๋ฌ๋ก ํ์ฌ๊ธ ๋ฌด์ํ๋๋ก ํจ
@FunctionalInterface : ์๋ฐ 8๋ถํฐ ๊ธฐ๋ฅ์ ์ธํฐํ์ด์ค๊ฐ ๋๊ธฐ ์ํด ์๋๋ ํ์ ์ ์ธ์ ๊ตฌ์ฒดํํจ
@Retention์ด๋
์ด๋ ์์ ๊น์ง Annotation์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ์ ธ๊ฐ ๊ฑด์ง ์ค์ ํ๋ ๋ถ๋ถ
์กฐ๊ธ ๋ ์์๋ณด๊ธฐ ์ํด์ ๋ด๋ถ๋ฅผ ๋ค์ฌ๋ค ๋ณด์๋ค
package java.lang.annotation;
/**
* Indicates how long annotations with the annotated type are to
* be retained. If no Retention annotation is present on
* an annotation type declaration, the retention policy defaults to
* {@code RetentionPolicy.CLASS}.
*
* <p>A Retention meta-annotation has effect only if the
* meta-annotated type is used directly for annotation. It has no
* effect if the meta-annotated type is used as a member type in
* another annotation type.
*
* @author Joshua Bloch
* @since 1.5
* @jls 9.6.3.2 @Retention
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
/**
* Returns the retention policy.
* @return the retention policy
*/
RetentionPolicy value();
}
annotation ์ ํ์ด ์๋ annotation์ด ๋ณด์กด๋๋ ๊ธฐ๊ฐ์ ๋ํ๋
์ธ์๋ก๋ RetentionPolicy๊ฐ ๋ค์ด๊ฐ๊ฒ ๋๋ค
์ธ์๋ก ๋ค์ด๊ฐ๋ RetentionPolicy๋ฅผ ์์๋ณด๊ธฐ ์ํด ๋ค์ ๋ ๋ด๋ถ๋ฅผ ๋ค์ฌ๋ค๋ดค๋ค
package java.lang.annotation;
/**
* Annotation retention policy. The constants of this enumerated type
* describe the various policies for retaining annotations. They are used
* in conjunction with the {@link Retention} meta-annotation type to specify
* how long annotations are to be retained.
*
* @author Joshua Bloch
* @since 1.5
*/
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
*
* @see java.lang.reflect.AnnotatedElement
*/
RUNTIME
}
RetentionPolicy๊ฐ์ ๋ฃ์ด์ฃผ๋ฉด ๊ทธ๊ฒ์ผ๋ก annotation์ ๋ฉ๋ชจ๋ฆฌ ๋ณด์ ๋ฒ์๊ฐ ๊ฒฐ์ ๋๋ค
์์
SOURCE : ํด๋น annotation์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ปดํ์ผ๋ฌ๊ฐ ๋ฒ๋ฆฐ๋ค
CLASS : ์ปดํ์ผ๋ฌ๊ฐ ์ปดํ์ผ์์๋ annotation์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ฐ์ ธ๊ฐ์ง๋ง ์ค์ ๋ก ๋ฐํ์์์ ์ฌ๋ผ์ง๋ค
RUNTIME : ์ปดํ์ผ๋ฌ์ ์ํด์ ํด๋์ค ํ์ผ์ ๊ธฐ๋ก๋๊ณ , ๋ฐํ์ ์ JVM์ ์ํด ์ ์ง๋๋ค.
@Retention(RetentionPolicy.RUNTIME)
์ด๋ ๊ฒ ์ฌ์ฉํ๊ณ RUNTIME ์๋ฆฌ์ ํ์ํ ์์๋ค์ ๋ฃ์ด์ ์ฌ์ฉ๊ฐ๋ฅ
@Target์ด๋
annotation์ด ์ ์ฉ๋ ์์น๋ฅผ ๊ฒฐ์
์ด๊ฒ๋ ๋ด๋ถ๋ฅผ ๋ค์ฌ๋ค๋ณด๋ฉด ์ด๋ ๊ฒ ์ค๋ช ํด๋์๋ค.
package java.lang.annotation;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
/**
* Returns an array of the kinds of elements an annotation type
* can be applied to.
* @return an array of the kinds of elements an annotation type
* can be applied to
*/
ElementType[] value();
}
์์์์ Retention ์ฒ๋ผ ElementType์ ์์๋ค์ ์ํ๋ ๋งํผ ๋ฃ์ ์ ์๋ ๊ฒ ์ฒ๋ผ ๋ณด์ธ๋ค
๋ค์ ElementType์ ์ฐพ์๋ณด์
package java.lang.annotation;
public enum ElementType {
/** Class, interface (including annotation type), or enum declaration */
TYPE,
/** Field declaration (includes enum constants) */
FIELD,
/** Method declaration */
METHOD,
/** Formal parameter declaration */
PARAMETER,
/** Constructor declaration */
CONSTRUCTOR,
/** Local variable declaration */
LOCAL_VARIABLE,
/** Annotation type declaration */
ANNOTATION_TYPE,
/** Package declaration */
PACKAGE,
/**
* Type parameter declaration
*
* @since 1.8
*/
TYPE_PARAMETER,
/**
* Use of a type
*
* @since 1.8
*/
TYPE_USE
}
TYPE : ํ์ (ํด๋์ค, ์ธํฐํ์ด์ค, enum) ์ ์ธ ์ ์ฌ์ฉ
FIELD : ๋ฉค๋ฒ ๋ณ์ ์ ์ธ ์ ์ฌ์ฉ(enum ํฌํจ)
METHOD : ๋ฉ์๋ ์ ์ธ ์ ์ฌ์ฉ
PARAMETER : ๋งค๊ฐ ๋ณ์ ์ ์ธ ์ ์ฌ์ฉ
CONSTRUCTOR : ์์ฑ์ ์ ์ธ ์ ์ฌ์ฉ
LOCAL_VARIABLE : ์ง์ญ ๋ณ์ ์ ์ธ ์ ์ฌ์ฉ
ANNOTATION_TYPE : Annotation type ์ ์ธ ์
PACKAGE : ํจํค์ง ์ ์ธ ์
TYPE_PARAMETER : ๋ณ์ ํ์ ์ ์ธ ์
TYPE_USER : ํ์ ์ฌ์ฉ ์
@Documented์ด๋
ํํ๋ฅผ ๊ฐ์ง๋ annotation์ด javadoc ๊ฐ์ ํด์ ์ํด์ default๋ก ๋ฌธ์ํ ๋๋ ๊ฒ์ ๋ํ๋
=๋ฌธ์์ ์ ๋ณด๊ฐ ํํ๋๋ค๋ผ๊ณ ์๊ฐํ๋ฉด ๋ ๋ฏํ๋ค
package java.lang.annotation;
/**
* Indicates that annotations with a type are to be documented by javadoc
* and similar tools by default. This type should be used to annotate the
* declarations of types whose annotations affect the use of annotated
* elements by their clients. If a type declaration is annotated with
* Documented, its annotations become part of the public API
* of the annotated elements.
*
* @author Joshua Bloch
* @since 1.5
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}
์ ๋
ธํ
์ด์
ํ๋ก์ธ์
Annotation Processing์ด๋? (from Stackoverflow)
-> ์๋ฐ ์ปดํ์ผ๋ฌ์ ์ปดํ์ผ ๋จ๊ณ์์ ์ ์ ๊ฐ ์ ์ํ annotation์ ์์ค์ฝ๋๋ฅผ ๋ถ์ํ๊ณ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์ฌ์ฉ๋๋ ํ ์ด๋ค. ์ปดํ์ผ ์๋ฌ๋ ์ปดํ์ผ ๊ฒฝ๊ณ ๋ฅผ ๋ง๋ค์ด๋ด๊ฑฐ๋ ์์ค์ฝ๋(.java)์ ๋ฐ์ดํธ์ฝ๋(.class)๋ฅผ ๋ด๋ณด๋ด๊ธฐ๋ ํ๋ค
์ด๊ฒ์ ๊ฐ์ฅ ๋ํ์ ์ธ ์์๋ lombok๊ฐ ์์ผ๋ฉฐ
์ฅ์ ์ ๋ฐํ์ ๋น์ฉ์ด ์๋ ๋ค๋์ ์ด๊ณ
๋จ์ ์ ๊ธฐ์กด์ ํด๋์ค ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ ๋๋ ๋ด๋ถ ํด๋์ค๋ฅผ ์ฌ์ฉํด์ ๊ธฐ์กด ์ฝ๋๋ฅผ ๋ง์ง๊ธฐ ๋๋ฌธ์ ํดํน์ ๋น์ทํ๋ค
annotation process๋ฅผ ์ง์ ์์ฑํ๊ฑฐ๋ ๋ง๋ค์ด์ง annotation processor๋ฅผ ํ์ธํ๋ฉด Abstract Processor ํด๋์ค๋ฅผ ์์๋ฐ๋ ๊ฒ์ ์ ์ ์๋ค.
์ฐธ๊ณ ์ฌ์ดํธ
https://www.nextree.co.kr/p5864/
https://coding-factory.tistory.com/575
http://cris.joongbu.ac.kr/course/java/api/java/lang/annotation/Documented.html
Last updated
Was this helpful?