未注释的方法覆盖使用@NotNull注释的方法

问题描述 投票:19回答:3

我正在实现一个自定义数据结构,它给我一些集合的属性和列表的其他属性。但是对于大多数实现的方法,我在Java 7上的IntelliJ IDEA中得到了这个奇怪的警告:

未注释的方法覆盖使用@NotNull注释的方法

编辑:下面的代码与问题无关,而是原始问题的一部分。由于IntelliJ中存在错误,此警告会显示。请参阅answer(希望)解决您的问题。


我一直无法找到任何相关的东西,我不确定我是否真的错过了某种检查,但我已经查看了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();
}

编辑:我在类中有这些额外的构造函数:

public ListHashSet(int initialCapacity) {
    this(initialCapacity, .75f);
}

public ListHashSet() {
    this(16, .75f);
}
java intellij-idea annotations notnull
3个回答
22
投票

尝试将@Nonnulljavax.annotation.Nonnull)添加到toArray方法中。

当我添加此注释时,警告消失了。我认为警告信息不正确,表示缺少@NotNull


1
投票

我同意这是Nonnull与NotNull的拼写错误。但是,似乎还有一些其他错误正在发生。我正在实现一个自定义Set,它抱怨Iterator和toArray方法没有注释。但是,看看JDK,在Set或Collection的界面上似乎没有任何这样的注释。

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

奇怪的。

无论如何,另一个选择是在您的类或方法中添加@SuppressWarnings标记:@SuppressWarnings(“NullableProblems”)


0
投票

尝试添加:

private ListHashSet() {}
© www.soinside.com 2019 - 2024. All rights reserved.