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

https://yadon079.github.io/2021/java study halle/week-12

https://en.wikipedia.org/wiki/Java_annotation

Last updated

Was this helpful?