Java 未注释的方法覆盖用@NotNull 注释的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24495448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Not annotated method overrides method annotated with @NotNull
提问by BrDaHa
I'm implementing a custom data structure that gives me some properties of sets and other properties of lists. For most of the implemented methods though, I get this weird warning in IntelliJ IDEA on Java 7:
我正在实现一个自定义数据结构,它为我提供了集合的一些属性和列表的其他属性。不过,对于大多数已实现的方法,我在 Java 7 上的 IntelliJ IDEA 中收到了这个奇怪的警告:
Not annotated method overrides method annotated with @NotNull
未注释的方法覆盖用@NotNull 注释的方法
EDIT: The code below isn't relevant to the issue, but part of the original question. This warning shows up because of a bug in IntelliJ. See the answerto (hopefully) resolve your issue.
编辑:下面的代码与问题无关,而是原始问题的一部分。出现此警告是因为 IntelliJ 中的一个错误。请参阅(希望)解决您的问题的答案。
I haven't been able to find anything relevant about it and I'm not sure if I'm actually missing some sort of check, but I've looked through the source of both ArrayList and the List interface and can't see what this warning is actually about. It's on every implemented method that references the list field. Here's a snippet of the class I've made:
我找不到任何相关的东西,我不确定我是否真的错过了某种检查,但我已经查看了 ArrayList 和 List 接口的源代码,但看不到什么这个警告实际上是关于。它在每个引用列表字段的已实现方法上。这是我制作的课程的片段:
public class ListHashSet<T> implements List<T>, Set<T> {
private ArrayList<T> list;
private HashSet<T> set;
/**
* Constructs a new, empty list hash set with the specified initial
* capacity and load factor.
*
* @param initialCapacity the initial capacity of the list hash set
* @param loadFactor the load factor of the list hash set
* @throws IllegalArgumentException if the initial capacity is less
* than zero, or if the load factor is nonpositive
*/
public ListHashSet(int initialCapacity, float loadFactor) {
set = new HashSet<>(initialCapacity, loadFactor);
list = new ArrayList<>(initialCapacity);
}
...
/**
* The Object array representation of this collection
* @return an Object array in insertion order
*/
@Override
public Object[] toArray() { // warning is on this line for the toArray() method
return list.toArray();
}
EDIT: I have these additional constructors in the class:
编辑:我在类中有这些额外的构造函数:
public ListHashSet(int initialCapacity) {
this(initialCapacity, .75f);
}
public ListHashSet() {
this(16, .75f);
}
采纳答案by Invisible Arrow
Try adding @Nonnull
(javax.annotation.Nonnull) to the toArray
method.
尝试将@Nonnull
( javax.annotation.Nonnull)添加到toArray
方法中。
The warning disappeared for me when I added this annotation. I think the warning message is incorrect which says that @NotNull
is missing.
当我添加此注释时,警告对我来说消失了。我认为警告消息是不正确的,它说它@NotNull
丢失了。
回答by Totoro
Try adding:
尝试添加:
private ListHashSet() {}
回答by doku
I agree it's a typo on Nonnull vs. NotNull. However, there also seems to be some other bug going on. I was implementing a custom Set and it was complaining about Iterator and toArray methods not having the annotation. But, looking at the JDK, there doesn't seem to be any such annotation on the interface for Set or Collection.
我同意这是 Nonnull 与 NotNull 的拼写错误。但是,似乎还有其他一些错误正在发生。我正在实现一个自定义 Set 并且它抱怨 Iterator 和 toArray 方法没有注释。但是,查看 JDK,在 Set 或 Collection 的接口上似乎没有任何此类注释。
http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/0eb62e4a75e6/src/share/classes/java/util/Set.javahttp://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/0eb62e4a75e6/src/share/classes/java/util/Collection.java
http://hg.openjdk.java.net/jdk8u/jdk8u/jdk/file/0eb62e4a75e6/src/share/classes/java/util/Set.java http://hg.openjdk.java.net/jdk8u/jdk8u /jdk/file/0eb62e4a75e6/src/share/classes/java/util/Collection.java
Weird.
奇怪的。
Anyway, the other option is to add a @SuppressWarnings tag to your class or method: @SuppressWarnings("NullableProblems")
无论如何,另一种选择是在您的类或方法中添加一个@SuppressWarnings 标签:@SuppressWarnings("NullableProblems")