Java/The Java

[The Java] 애노테이션과 리플렉션

메성 2021. 1. 3. 22:07
반응형

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

    image

    결과 2

    image
  • 해당 클래스 필드에 있는 애노테이션 정보를 보자

    public static void main( String[] args ) throws ClassNotFoundException {
      Arrays.stream(Book.class.getDeclaredFields()).forEach(f -> {
        Arrays.stream(f.getAnnotations()).forEach(System.out::println);
      });
    }

    결과

    image
  • 해당 애노테이션에 입력된 값들을 가져오자

    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());
          }
        });
      });
    }

    결과

    image
반응형