JAVA Annotation - ynjch97/YNJCH_WIKI GitHub Wiki
โท @SuppressWarnings ("unused")
- all : ๋ชจ๋ ๊ฒฝ๊ณ ๋ฅผ ์ต์
- cast : ์บ์คํธ ์ฐ์ฐ์ ๊ด๋ จ ๊ฒฝ๊ณ ์ต์
- dep-ann : ์ฌ์ฉํ์ง ๋ง์์ผ ํ ์ฃผ์ ๊ด๋ จ ๊ฒฝ๊ณ ์ต์
- deprecation : ์ฌ์ฉํ์ง ๋ง์์ผ ํ ๋ฉ์๋ ๊ด๋ จ ๊ฒฝ๊ณ ์ต์
- fallthrough : switch๋ฌธ์์์ break ๋๋ฝ ๊ด๋ จ ๊ฒฝ๊ณ ์ต์
- finally : ๋ฐํํ์ง ์๋ finally ๋ธ๋ญ ๊ด๋ จ ๊ฒฝ๊ณ ์ต์
- null : null ๋ถ์ ๊ด๋ จ ๊ฒฝ๊ณ ์ต์
- rawtypes : ์ ๋ค๋ฆญ์ ์ฌ์ฉํ๋ ํด๋์ค ๋งค๊ฐ ๋ณ์๊ฐ ๋ถํน์ ์ผ ๋์ ๊ฒฝ๊ณ ์ต์
- unchecked : ๊ฒ์ฆ๋์ง ์์ ์ฐ์ฐ์ ๊ด๋ จ ๊ฒฝ๊ณ ์ต์
- unused : ์ฌ์ฉํ์ง ์๋ ์ฝ๋ ๊ด๋ จ ๊ฒฝ๊ณ ์ต์
โท ์ฌ์ฉ์ ์ ์ Annotation ์์ฑ ์ ์ฐ์ด๋ Annotation
- @Retention
- ์๋ฐ ์ปดํ์ผ๋ฌ๊ฐ ์ด๋ ธํ ์ด์ ์ ๋ค๋ฃจ๋ ๋ฐฉ๋ฒ
- ์ด๋ค ์์ ๊น์ง ์ํฅ์ ๋ฏธ์น๋์ง๋ฅผ ๊ฒฐ์
- RetentionPolicy.SOURCE : ์ปดํ์ผ ์ ๊น์ง๋ง ์ ํจ (์ปดํ์ผ ์ดํ์๋ ์ฌ๋ผ์ง)
- RetentionPolicy.CLASS : ์ปดํ์ผ๋ฌ๊ฐ ํด๋์ค๋ฅผ ์ฐธ์กฐํ ๋๊น์ง ์ ํจ
- RetentionPolicy.RUNTIME : ์ปดํ์ผ ์ดํ์๋ JVM์ ์ํด ๊ณ์ ์ฐธ์กฐ๊ฐ ๊ฐ๋ฅ (๋ฆฌํ๋ ์ ์ฌ์ฉ)
- @Target
- ์ด๋
ธํ
์ด์
์ด ์ ์ฉํ ์์น๋ฅผ ์ ํ
- ElementType.PACKAGE : ํจํค์ง ์ ์ธ
- ElementType.TYPE : ํ์ ์ ์ธ
- ElementType.ANNOTATION_TYPE : ์ด๋ ธํ ์ด์ ํ์ ์ ์ธ
- ElementType.CONSTRUCTOR : ์์ฑ์ ์ ์ธ
- ElementType.FIELD : ๋ฉค๋ฒ ๋ณ์ ์ ์ธ
- ElementType.LOCAL_VARIABLE : ์ง์ญ ๋ณ์ ์ ์ธ
- ElementType.METHOD : ๋ฉ์๋ ์ ์ธ
- ElementType.PARAMETER : ์ ๋ฌ์ธ์ ์ ์ธ
- ElementType.TYPE_PARAMETER : ์ ๋ฌ์ธ์ ํ์ ์ ์ธ
- ElementType.TYPE_USE : ํ์ ์ ์ธ
- ์ด๋
ธํ
์ด์
์ด ์ ์ฉํ ์์น๋ฅผ ์ ํ
- @Documented
- ํด๋น ์ด๋ ธํ ์ด์ ์ Javadoc์ ํฌํจ์ํด
- @Inherited
- ์ด๋ ธํ ์ด์ ์ ์์์ ๊ฐ๋ฅํ๊ฒ ํจ
- @Repeatable
- ์ฐ์์ ์ผ๋ก ์ด๋ ธํ ์ด์ ์ ์ ์ธํ ์ ์๊ฒ ํจ (Java8 ๋ถํฐ ์ง์)
โท ์ฌ์ฉ์ ์ ์ Annotation ์์ฑ
- @ ํ์๋ฅผ ๋ถ์ฌ ์ธํฐํ์ด์ค ์์ฑ
// ์ ์ ๊ฐ ์ฃผ์
์์
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface InsertIntData {
int data() default 0;
}
- myAge ์๋ 30์ผ๋ก ๊ฐ์ ์ฃผ์ , defaultAge ์๋ ๊ฐ์ด ์์ผ๋ฏ๋ก ๊ธฐ๋ณธ 0์ด ์ฃผ์ ๋จ
public class AnnotationExam01 {
@InsertIntData(data = 30)
private int myAge;
@InsertIntData
private int defaultAge;
public int getMyAge() {
return myAge;
}
public int getDefaultAge() {
return defaultAge;
}
}
โท ์ฌ์ฉ์ ์ ์ Annotation ์์
@Target(value={ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Component
public @interface AnnotationTest {
public enum ResultType {JSON, OBJECT};
public enum CheckSession {TRUE, FALSE};
ResultType ResultType() default ResultType.JSON;
CheckSession adminSession() default CheckSession.TRUE;
CheckSession frontSession() default CheckSession.TRUE;
boolean setTest1() default false;
String[] setTest2() default {};
int setTest3() default 0;
}
@AnnotationTest(adminSession=AnnotationTest.CheckSession.FALSE)
@RequestMapping("/admin/memberList.do")
public @ResponseBody TestVO selectMemberList(@RequestParam Map<String, Object> hashMap, HttpServletRequest request) throws Exception {
// (์๋ต)