Programing

한 요소에 동일한 유형의 여러 주석이 있습니까?

lottogame 2020. 9. 17. 18:52
반응형

한 요소에 동일한 유형의 여러 주석이 있습니까?


단일 요소 (이 경우 메서드)에 동일한 유형의 두 개 이상의 주석을 슬랩하려고합니다. 작업중인 대략적인 코드는 다음과 같습니다.

public class Dupe {
    public @interface Foo {
      String bar();
    }

    @Foo(bar="one")
    @Foo(bar="two")
    public void haha() {}
}

위를 컴파일 할 때 javac는 중복 주석에 대해 불평합니다.

max @ upsight : ~ / work / daybreak $ javac Dupe.java 
Dupe.java:5 : 중복 주석

단순히 이와 같은 주석을 반복 할 수 없습니까? 간절히 말하면 위의 @Foo 두 인스턴스는 내용이 다르기 때문에 다르지 않습니까?

위의 방법이 가능하지 않은 경우 잠재적 인 해결 방법은 무엇입니까?

업데이트 : 내 사용 사례를 설명하라는 요청을 받았습니다. 여기 있습니다.

저는 MongoDB와 같은 문서 저장소에 POJO를 "매핑"하는 구문 설탕 메커니즘을 구축하고 있습니다. getter 또는 setter에 대한 주석으로 인덱스를 지정하도록 허용하고 싶습니다. 다음은 인위적인 예입니다.

public class Employee {
    private List<Project> projects;

    @Index(expr = "project.client_id")
    @Index(expr = "project.start_date")
    public List<Project> getProjects() { return projects; }
}

당연히 Project의 다양한 속성별로 Employee의 인스턴스를 빠르게 찾을 수 있기를 바랍니다. 다른 expr () 값으로 @Index를 두 번 지정하거나 허용 된 답변에 지정된 접근 방식을 사용할 수 있습니다. Hibernate가이를 수행하고 해킹으로 간주되지는 않지만 적어도 단일 요소에 동일한 유형의 여러 주석을 허용하는 것이 여전히 타당하다고 생각합니다.


동일한 유형의 두 개 이상의 주석은 허용되지 않습니다. 그러나 다음과 같이 할 수 있습니다.

public @interface Foos {
    Foo[] value();
}

@Foos({@Foo(bar="one"), @Foo(bar="two")})
public void haha() {}

하지만 코드에서 Foos 주석을 전용으로 처리해야합니다.

btw, 나는 2 시간 전에 동일한 문제를 해결하기 위해 이것을 사용했습니다. :)


Java 8 (2014 년 3 월 출시)에서는 반복 / 중복 주석을 작성할 수 있습니다. http://docs.oracle.com/javase/tutorial/java/annotations/repeating.html을 참조 하십시오 .


http://docs.oracle.com/javase/tutorial/java/annotations/repeating.html

Java8부터 반복 가능한 주석을 설명 할 수 있습니다.

@Repeatable(FooValues.class)
public @interface Foo {
    String bar();
}

public @interface FooValues {
    Foo[] value();
}

참고 value값 목록 필드가 필요합니다.

이제 배열을 채우는 대신 주석을 반복하여 사용할 수 있습니다.

@Foo(bar="one")
@Foo(bar="two")
public void haha() {}

언급 된 다른 방법 외에도 Java8에는 덜 장황한 방법이 하나 더 있습니다.

@Target(ElementType.TYPE)
@Repeatable(FooContainer.class)
@Retention(RetentionPolicy.RUNTIME)
@interface Foo {
    String value();

}

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface FooContainer {
        Foo[] value();
        }

@Foo("1") @Foo("2") @Foo("3")
class Example{

}

기본적으로 예 FooContainer는 주석 으로 가져 옵니다.

    Arrays.stream(Example.class.getDeclaredAnnotations()).forEach(System.out::println);
    System.out.println(Example.class.getAnnotation(FooContainer.class));

위의 두 인쇄 :

@ com.FooContainer (value = [@ com.Foo (value = 1), @ com.Foo (value = 2), @ com.Foo (value = 3)])

@com.FooContainer(value=[@com.Foo(value=1), @com.Foo(value=2), @com.Foo(value=3)])


As said by sfussenegger, this isn't possible.

The usual solution is to build an "multiple" annotation, that handles an array of the previous annotation. It is typically named the same, with an 's' suffix.

By the way, this is very used in big public projects (Hibernate for example), so it shouldn't be considered as a hack, but rather a correct solution for this need.


Depending on your needs, it could be better to allow your earlier annotation to handle multiple values.

Example:

    public @interface Foo {
      String[] bars();
    }

combining the other answers into the simplest form ... an annotation with a simple list of values ...

@Foos({"one","two"})
private String awk;

//...

public @interface Foos{
    String[] value();
}

If you have only 1 parameter "bar" you can name it as "value". In this case you wont have to write the parameter name at all when you use it like this:

@Foos({@Foo("one"), @Foo("two")})
public void haha() {}

a bit shorter and neater, imho..


In the current version of Java, I was able to resolve this issue with the following annotation:

@Foo({"one", "two"})

참고URL : https://stackoverflow.com/questions/1554112/multiple-annotations-of-the-same-type-on-one-element

반응형