티스토리 뷰
반응형
Java의 중요 애노테이션
- @Retention : 해당 애노테이션을 언제까지 유지할 것인가? source, class, runtime
- @Inherited : 해당 애노테이션을 하위 클래스까지 전달한다.
- @Target : 어디에 사용할 수 있는가? type, field, method ...
리플렉션을 사용하여 애노테이션들의 정보를 살펴보자
//custom annotation 1
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
@Inherited
public @interface MyAnnotation {
String value() default "mesung";
int number() default 100;
}
//custom annotation 2
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.FIELD})
@Inherited
public @interface AnotherAnnotation {
String value() default "kimpo";
int number() default 100;
}
//custom class(Book)
@MyAnnotation("ha ong")
public class Book {
private static String B = "BOOK";
private static final String C = "BOOK";
@AnotherAnnotation(value = "hello", number = 20)
private String a = "a";
@AnotherAnnotation(value = "hi", number = 60)
public String d = "d";
protected String e = "e";
public Book() {
}
@AnotherAnnotation
public Book(String a, String d, String e) {
this.a = a;
this.d = d;
this.e = e;
}
private void f() {
System.out.println("F");
}
public void g() {
System.out.println("g");
}
public int h () {
return 100;
}
}
//super class(MyBook)
@AnotherAnnotation
public class MyBook extends Book implements MyInterface{
}
-
해당 클래스에 붙어있는 애노테이션의 정보를 보자
public static void main( String[] args ) throws ClassNotFoundException { Arrays.stream(MyBook.class.getDeclaredAnnotations()).forEach(System.out::println); }
-
해당 클래스나 상위 클래스에 붙어있는 애노테이션의 정보를 보자
public static void main( String[] args ) throws ClassNotFoundException { //1. MyAnnotation만 호출 Arrays.stream(Book.class.getAnnotations()).forEach(System.out::println); //2. MyAnnotation 및 AnotherAnnotation 호출 Arrays.stream(MyBook.class.getAnnotations()).forEach(System.out::println); }
결과 1
결과 2
-
해당 클래스 필드에 있는 애노테이션 정보를 보자
public static void main( String[] args ) throws ClassNotFoundException { Arrays.stream(Book.class.getDeclaredFields()).forEach(f -> { Arrays.stream(f.getAnnotations()).forEach(System.out::println); }); }
결과
-
해당 애노테이션에 입력된 값들을 가져오자
public static void main( String[] args ) throws ClassNotFoundException { Arrays.stream(Book.class.getDeclaredFields()).forEach(f -> { Arrays.stream(f.getAnnotations()).forEach(a -> { if(a instanceof AnotherAnnotation) { AnotherAnnotation anotherAnnotation = (AnotherAnnotation) a; System.out.println(anotherAnnotation.value()); System.out.println(anotherAnnotation.number()); } }); }); }
결과
반응형
'Java > The Java' 카테고리의 다른 글
[The Java] 프록시 패턴 (0) | 2021.01.03 |
---|---|
[The Java] 리플렉션 API(3) : 나만의 DI를 만들자 (0) | 2021.01.03 |
[The Java] 리플렉션 API(2) : 클래스 정보 수정 (0) | 2021.01.03 |
[The Java] 리플렉션 API(1) : 클래스 정보 조회 (0) | 2021.01.03 |
[The Java] 클래스 로더와 JVM (0) | 2021.01.03 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- mustache
- 빌더 패턴
- effectivejava
- java
- 김영한
- @Lazy
- Effective Java
- 정적팩터리메서드
- 이펙티브 자바
- JPA
- 연관관계
- 빈 순환 참조
- java8
- Spring
- try catch finally
- 점층적 생성 패턴
- springboot
- 자바8
- 복사 팩토리
- flatMap
- 이펙티브자바
- package-private
- 인프런
- 생성자
- jdk버전
- 팩토리 메소드 패턴
- 스프링부트
- ifPresent
- junit
- try with resources
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
글 보관함