Home Java의 Annotation 알아보기 - @AliasFor
Post
Cancel

Java의 Annotation 알아보기 - @AliasFor



AliasFor

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@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;
}

사용 사례들

  • Explicit aliases within an annotation
    (단일 애너테이션에 대해 명시적인 여러 alias들)
    : 단일 애너테이션 내에서,
    한 쌍의 속성에 대하여
    그 둘이 상호교환적인 alias임을 나타내기 위해
    @AliasFor를 사용할 수 있다.

  • Explicit alias for attribute in meta-annotation
    (메타-애너테이션 내의 속성에 대해 명시적인 alias)
    @AliasFor의 annotation() 속성이,
    해당 애너테이션이 아닌
    다른 애너테이션에서 설정set될 경우,
    이때 attribute()는
    메타 애너테이션의 속성에 대한 alias가 된다.
    (즉, 이것의 속성에 대한 명시적 오버라이드)

    이렇게 함으로써,
    애너테이션 계층 하에서
    어떤 속성을 오버라이드 할 지를
    문제없이 제어할 수 있다.

    사실, @AliasFor를 이용하여
    메타 애너테이션의 value 속성에 대한
    alis를 선언하는 것도 가능하다.

  • Implicit aliases within an annotation
    (단일 애너테이션에 대해 암묵적인 alias들)
    : 단일 애너테이션의 하나 이상의 속성들이
    동일한 메타 애너테이션 속성에 대한
    오버라이드로 선언될 경우
    (직접적이든, 간접적 변환이든),
    이러한 속성들은 서로에 대하여
    암묵적인 alias들의 집합으로 간주된다.
    그리고, 이 속성들은
    단일 애너테이션에 대한
    명시적인 경우와 유사하게 형성된다.

예제 - 명시적 / 단일 애너테이션

Explicit Aliases within an Annotation

@ContextConfiguration에서,
value와 locations는
상호 명시적인 alias들이다.

1
2
3
4
5
6
7
8
9
10
 public @interface ContextConfiguration {

    @AliasFor("locations")
    String[] value() default {};

    @AliasFor("value")
    String[] locations() default {};

    // ...
 }

예제 - 명시적 / 메타 애너테이션

Explicit Alias for Attribute in Meta-annotation

@XmlTestConfig에서, xmlFiles는
@ContextConfiguration의 locations에 대한
명시적 alias이다.
달리 말하면, xmlFiles이
@ContextConfiguration의 locations 속성을
상속한다고 할 수 있다.

1
2
3
4
5
6
7
 @ContextConfiguration
 public @interface XmlTestConfig {

    @AliasFor(annotation = ContextConfiguration.class, 
              attribute = "locations")
    String[] xmlFiles();
 }

예제 - 단일 애너테이션 내의 암묵적 alias들

Example: Implicit Aliases within an Annotation

In @MyTestConfig에서,
value, groovyScripts, xmlFiles는 모두
@ContextConfiguration의
locations 속성에 대한,
메타 애너테이션의 명시적 속성 오버라이드이다.
또한, 이 세 속성들은 서로에 대하여
암묵적인 alias가 된다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 @ContextConfiguration
 public @interface MyTestConfig {

    @AliasFor(annotation = ContextConfiguration.class, 
              attribute = "locations")
    String[] value() default {};

    @AliasFor(annotation = ContextConfiguration.class, 
              attribute = "locations")
    String[] groovyScripts() default {};

    @AliasFor(annotation = ContextConfiguration.class, 
              attribute = "locations")
    String[] xmlFiles() default {};
 }

예제 - 단일 애너테이션 내의 암묵적 변환 alias

Example: Transitive Implicit Aliases within an Annotation

@GroovyOrXmlTestConfig에서, groovy는
@MyTestConfig의 groovyScripts를 명시적으로 오버라이드한 것이다.
그런 반면, xml은 마찬가지로
@ContextConfiguration의 locations 속성에 대하여
명시적으로 오버라이드한 것이다.

이에 따라,
groovy와 xml은 둘 모두
@ContextConfiguration의 locations를
오버라이드한 것의 효과를 지니므로
이 둘은 서로에 대하여 암묵적 변환 alias이다.
서로에 대하여

1
2
3
4
5
6
7
8
9
10
11
12
@MyTestConfig
 public @interface GroovyOrXmlTestConfig {

    @AliasFor(annotation = MyTestConfig.class,
              attribute = "groovyScripts")
    String[] groovy() default {};

    @AliasFor(annotation 
                  = ContextConfiguration.class,      
              attribute = "locations")
    String[] xml() default {};
 }

스프링 애너테이션 중 속성 Alias들을 지원하는 것

스프링 프레임워크 4.2 기준으로,
스프링에서 핵심이 되는 몇몇 애너테이션은
@AliasFor를 사용함으로써
내부 속성 alias들을 설정하는 것으로 업데이트 되었다.
세부 사항은 javadoc의 개별 애너테이션 부분과
레퍼런스 매뉴얼을 참조하면 된다.

Contents