Post

Alias Annotation

@AliasFor에 정의된 애노테이션의 메타데이터 값을 가져오는 역할을 한다

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface AliasFor {

  @AliasFor("attribute")
  String value() default "";

  @AliasFor("value")
  String attribute() default "";

  Class<? extends Annotation> annotation() default Annotation.class;

}

이렇게 생겼다. 이를 사용하는 예제는 다음과 같다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
  String value() default "value";
}

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

  @AliasFor(annotation = MyAnnotation.class, attribute = "value")
  String aliasAlias() default "aliasValue";
}

@AliasAnnotation(aliasAlias = "hello")
public class SampleClass {
}

이처럼 값을 이어받을 수 있게 된다. 이때 중요한 점은 이어 받을 애노테이션을 붙여줘야한다는 것이다.

This post is licensed under CC BY 4.0 by the author.