开发者

What happens when two different annotations have the same name?

I never encountered the case and now I'm wondering: what happens when/if two different annotations have the same name? (I'll give here an example using an annotation provided by IntelliJ b开发者_C百科ut the question is really generic so I'm not tagging it 'IntelliJ')

For example the first sentence here:

http://youtrack.jetbrains.net/issue/IDEABKL-4959

says:

Many libraries have their own @NotNull annotations (intellij, hibernate-validation, ..).

What happens exactly if I wanted to use, say, both IntelliJ's @NotNull and Hibernate's @NotNull? (once again @NotNull is just an example where I happen to find a clash)

Are they incompatible? If they're incompatible, is it for the whole project?

This is something I'm really not familiar with...


In such a case you need to specify the full qualified name, e.g

@bar.baz.Foo 
@org.fubar.Foo
void myMethod() {...}


There is no ambiguity, because the annotation's package name will be specified in an import or on the annotation itself.

JSR-305 addresses your specific example. It seeks a standard set of annotations, and refers specifically to FindBugs' and IntelliJ's nullability annotations.

Nullness annotations (e.g., @NonNull and @CheckForNull). Both FindBugs and IntelliJ already support their own versions of nullness annotations.


They don't as the full package is part of the name. The effect is that you can only import one and will have to refer to any other with its fully qualified name. Like so

@NotNull
@com.jetbrains.annotations.NotNull
public Object ...


That won't matter since each annotation's full qualified name won't be the same. You can declare the qualified name on the import section.


The issue is the same for two classes/interafces/enums/annotations with the same name. They should appear in different packages. If they are in the same package (e.g. different versions) but different jar/directories, then the first one found in the classpath is chosen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜