問題描述
我試圖在單個元素上添加兩個或多個相同類型的注釋,在本例中是一個方法.這是我正在使用的大致代碼:
public class Dupe {公共@interface Foo {字符串條();}@Foo(bar="one")@Foo(bar="兩個")公共無效哈哈(){}}
編譯上面的時候,javac報錯注解:
<上一頁>max@upsight:~/work/daybreak$ javac Dupe.javaDupe.java:5:重復注釋難道就不能像這樣重復注釋嗎?學究起來,上面的兩個@Foo 實例不是因為內容不同而不同嗎?
如果上述方法不可行,有哪些潛在的解決方法?
更新:我被要求描述我的用例.來了.
我正在構建一種語法糖化機制,將 POJO映射"到 MongoDB 等文檔存儲.我想允許將索引指定為 getter 或 setter 上的注釋.這是一個人為的例子:
公共類員工{私人名單<項目>項目;@Index(expr = "project.client_id")@Index(expr = "project.start_date")公開列表<項目>getProjects() { 返回項目;}}
顯然,我希望能夠通過 Project 的各種屬性快速找到 Employee 的實例.我可以使用不同的 expr() 值指定 @Index 兩次,或者采用接受的答案中指定的方法.盡管 Hibernate 做到了這一點并且它不被認為是 hack,但我認為至少允許在單個元素上具有多個相同類型的注釋仍然是有意義的.
注意:由于 Java 8 引入了 @Repeatable
批注(請參閱 @mernst 的回答),此答案已部分過時.但是仍然需要 @Foos
容器注釋和專用處理.
不允許使用兩個或多個相同類型的注釋.但是,您可以這樣做:
public @interface Foos {Foo[] 值();}//Java 8 之前@Foos({@Foo(bar=一個"), @Foo(bar=兩個")})公共無效哈哈(){}//在@Foo 上發布帶有@Repeatable(Foos.class) 的Java 8@Foo(bar=一個") @Foo(bar=兩個")公共無效哈哈(){}
您需要專門處理代碼中的 Foos
注釋.
I'm attempting to slap two or more annotations of the same type on a single element, in this case, a method. Here's the approximate code that I'm working with:
public class Dupe {
public @interface Foo {
String bar();
}
@Foo(bar="one")
@Foo(bar="two")
public void haha() {}
}
When compiling the above, javac complains about a duplicate annotation:
max@upsight:~/work/daybreak$ javac Dupe.java Dupe.java:5: duplicate annotation
Is it simply not possible to repeat annotations like this? Pedantically speaking, aren't the two instances of @Foo above different due to their contents being different?
If the above isn't possible, what are some potential workarounds?
UPDATE: I've been asked to describe my use case. Here goes.
I'm building a syntax sugarish mechanism to "map" POJOs to document stores such as MongoDB. I want to allow indexes to be specified as annotations on the getters or setters. Here's a contrived example:
public class Employee {
private List<Project> projects;
@Index(expr = "project.client_id")
@Index(expr = "project.start_date")
public List<Project> getProjects() { return projects; }
}
Obviously, I want to be able to quickly find instances of Employee by various properties of Project. I can either specify @Index twice with different expr() values, or take the approach specified in the accepted answer. Even though Hibernate does this and it's not considered a hack, I think it still makes sense to at least allow having multiple annotations of the same type on a single element.
Note: This answer is partially outdated since Java 8 introduced the @Repeatable
annotation (see answer by @mernst). The need for a @Foos
container annotation and dedicated handling still remain though.
Two or more annotations of same type aren't allowed. However, you could do something like this:
public @interface Foos {
Foo[] value();
}
// pre Java 8
@Foos({@Foo(bar="one"), @Foo(bar="two")})
public void haha() {}
// post Java 8 with @Repeatable(Foos.class) on @Foo
@Foo(bar="one") @Foo(bar="two")
public void haha() {}
You'll need dedicated handling of Foos
annotation in code though.
這篇關于一個元素上有多個相同類型的注釋?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!