BrD*_*aHa 24 java annotations intellij-idea notnull
我正在实现一个自定义数据结构,它给我一些集合的属性和列表的其他属性.但是对于大多数实现的方法,我在Java 7上的IntelliJ IDEA中得到了这个奇怪的警告:
未注释的方法覆盖使用@NotNull注释的方法
编辑:下面的代码与问题无关,而是原始问题的一部分.由于IntelliJ中存在错误,此警告会显示.查看答案(希望如此)解决您的问题.
我一直无法找到任何相关的东西,我不确定我是否真的错过了某种检查,但我已经查看了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();
}
Run Code Online (Sandbox Code Playgroud)
编辑:我在类中有这些额外的构造函数:
public ListHashSet(int initialCapacity) {
this(initialCapacity, .75f);
}
public ListHashSet() {
this(16, .75f);
}
Run Code Online (Sandbox Code Playgroud)
Inv*_*row 29
尝试将@Nonnull
(javax.annotation.Nonnull)添加到toArray
方法中.
当我添加此注释时,警告消失了.我认为警告信息不正确,表示@NotNull
缺少.
小智 6
我同意这是 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")