[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item27 ์™„๋ฒฝ๊ณต๋žต. ์• ๋„ˆํ…Œ์ด์…˜

2023. 3. 16. 10:53ใ†JAVA/Effective JAVA

728x90

 

item27. ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ œ๊ฑฐํ•˜๋ผ. 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item27. ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ œ๊ฑฐํ•˜๋ผ. (tistory.com)

 

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item27. ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ œ๊ฑฐํ•˜๋ผ.

[์ดํŽ™ํ‹ฐ๋ธŒ ์ž๋ฐ”] Item27. ๋น„๊ฒ€์‚ฌ ๊ฒฝ๊ณ ๋ฅผ ์ œ๊ฑฐํ•˜๋ผ. ์ œ๋„ค๋ฆญ์„ ์ด์ œ ์‚ฌ์šฉํ•˜๊ธฐ ์‹œ์ž‘ํ•˜๋ฉด ๋งŽ์€ ์ปดํŒŒ์ผ๋Ÿฌ ๊ฒฝ๊ณ ๋ฅผ ๋ณด๊ฒŒ ๋œ๋‹ค. ๋น„๊ฒ€์‚ฌ ํ˜•๋ณ€ํ™˜ ๊ฒฝ๊ณ , ๋น„๊ฒ€์‚ฌ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๊ฒฝ๊ณ , ๋น„๊ฒ€์‚ฌ ๋งค๊ฐœ๋ณ€์ˆ˜ํ™” ๊ฐ€๋ณ€์ธ์ˆ˜

hyejin.tistory.com

 

์–ด๋…ธํ…Œ์ด์…˜ annotation ์ด๋ž€? 

: ์ž๋ฐ”์—์„œ ์–ด๋…ธํ…Œ์ด์…˜์€ ์‚ฌ์ „์ ์ธ ์˜๋ฏธ๋กœ๋Š” ์ฃผ์„์ด๋ผ๋Š” ๋œป์œผ๋กœ ์†Œ์Šค์ฝ”๋“œ์— ์ถ”๊ฐ€ํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ผ์ข…์˜ ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ์˜ ์ผ์ข…์ด๋‹ค. 

๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๋ž€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์ด ์ฒ˜๋ฆฌํ•ด์•ผํ•  ๋ฐ์ดํ„ฐ๊ฐ€ ์•„๋‹ˆ๋ผ ์ปดํŒŒ์ผ ๊ณผ์ •๊ณผ ์‹คํ–‰ ๊ณผ์ •์—์„œ ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•ด์•ผํ•˜๋Š”์ง€๋ฅผ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•œ ์ถ”๊ฐ€ ์ •๋ณด์ด๋‹ค. 

 

 

์ž๋ฐ” ์–ด๋…ธํ…Œ์ด์…˜์˜ ๊ธฐ๋Šฅ 

- ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ์ฝ”๋“œ ์ž‘์„ฑ ๋ฌธ๋ฒ• ์—๋Ÿฌ๋ฅผ ์ฒดํฌํ•˜๋„๋ก ์ •๋ณด ์ œ๊ณต 

- ์†Œํ”„ํŠธ์›จ์–ด ๊ฐœ๋ฐœ ํ™˜๊ฒฝ์ด ๋นŒ๋“œ๋‚˜ ๋ฐฐํฌ์‹œ ์ฝ”๋“œ๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋„๋ก ์ •๋ณด ์ œ๊ณต 

- ๋Ÿฐํƒ€์ž„์— ํŠน์ • ๊ธฐ๋Šฅ์„ ์‹คํ–‰ํ•˜๋„๋ก ์ •๋ณด ์ œ๊ณต 

 

 

@Target({ElementType.TYPE, ElementType.METHOD}) 
@Retention(RetentionPolicy.RUNTIME) 
@Documented 
public @interface MyAnnotation
{

}
@MyAnnotation
public class MyClass
{
   @MyAnnotation
   public static void main(String[] args)
   {
      Arrays.stream(MyClass.class.getAnnotations()).forEach(System.out::println);
   }
}

@Documented 

: ๊ฐ„๋‹จํ•œ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์ด ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•œ ์ฝ”๋“œ์˜ javadoc์— ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ณด๊ฐ€ ํฌํ•จ๋˜๋„๋ก ํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด๋‹ค. 

 

@Retention 

: ์–ด๋…ธํ…Œ์ด์…˜์˜ ์ •๋ณด๋ฅผ ์–ผ๋งˆ๋‚˜ ์˜ค๋ž˜ ์œ ์ง€ํ•  ๊ฒƒ์ธ๊ฐ€. 

RetentionPolicy.RUNTIME, SOURCE, CLASS ์œผ๋กœ ๋ฒ”์œ„๋ฅผ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค. 

RUNTIME : ๋ฐ”์ดํŠธ ์ฝ”๋“œ ํŒŒ์ผ๊นŒ์ง€ ์–ด๋…ธํ…Œ์ด์…˜  ์ •๋ณด๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ๋ฆฌํ”Œ๋ ‰์…˜์„ ์ด์šฉํ•ด ๋Ÿฐํƒ€์ž„์— ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 

SOURCE :  compile ์ดํ›„์—๋Š” ์‚ญ์ œ๋œ๋‹ค. 

CLASS : ๋ฐ”์ดํŠธ ์ฝ”๋“œ ํŒŒ์ผ๊นŒ์ง€ ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ณด๋Š” ์œ ์ง€ํ•˜์ง€๋งŒ, ๋ฆฌํ”Œ๋ ‰์…˜์„ ์ด์šฉํ•ด์„œ ์–ด๋…ธํ…Œ์ด์…˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ๋Š” ์—†๋‹ค. 

 

@Target

: ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜ 

ElementType.Tye, Filed, Method, Parameter .... 

Type : ํด๋ž˜์Šค, ์ธํ„ฐํŽ˜์ด์Šค, ์—ด๊ฑฐํƒ€์ž… 

Filed : ํ•„๋“œ 

Method: ๋ฉ”์„œ๋“œ 

... 

 

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, MODULE})
@Retention(RetentionPolicy.SOURCE)
public @interface SuppressWarnings {
    /**
     * The set of warnings that are to be suppressed by the compiler in the
     * annotated element.  Duplicate names are permitted.  The second and
     * successive occurrences of a name are ignored.  The presence of
     * unrecognized warning names is <i>not</i> an error: Compilers must
     * ignore any warning names they do not recognize.  They are, however,
     * free to emit a warning if an annotation contains an unrecognized
     * warning name.
     *
     * <p> The string {@code "unchecked"} is used to suppress
     * unchecked warnings. Compiler vendors should document the
     * additional warning names they support in conjunction with this
     * annotation type. They are encouraged to cooperate to ensure
     * that the same names work across multiple compilers.
     * @return the set of warnings to be suppressed
     */
    String[] value();
}

@SuppressWarnings ์–ด๋…ธํ…Œ์ด์…˜์˜ @Retention์„ ๋ณด๋ฉด SOURCE๊นŒ์ง€๋กœ compile ์ดํ›„์—๋Š” ์‚ญ์ œ๋œ๋‹ค. 

๊ทธ๋ฆฌ๊ณ  @Target์„ ๋ณด๋ฉด ์–ด๋…ธํ…Œ์ด์…˜์ด ์„ ์–ธ์—๋งŒ ๋‹ฌ ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ๋„ ์•Œ ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

 

 

 

 

 

 

 

 

 

 

728x90