java - 未注解的方法会覆盖使用@NotNull 注解的方法

标签 java intellij-idea annotations notnull

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

Not annotated method overrides method annotated with @NotNull

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


我找不到任何相关的东西,我不确定我是否真的错过了某种检查,但我已经查看了 ArrayList 和 List 接口(interface)的源代码并且可以'看不到这个警告实际上是关于什么的。它在引用列表字段的每个实现的方法上。这是我制作的类(class)的片段:

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);
}

最佳答案

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

当我添加此注释时,警告消失了。我认为警告消息不正确,它说 @NotNull 丢失。

关于java - 未注解的方法会覆盖使用@NotNull 注解的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24495448/

相关文章:

java - 动态类加载在运行时失败

unit-testing - 测试框架意外退出 - Mac 上的 Dart 项目

git - 在 GIT 中推送 intellij 12/13 不工作

java - Spring MVC 注解总是需要 Response 对象

mysql - 从 Java 配置数据源(无 XML)

java - 我可以使用 Java 中的 if/else 语句向字符串添加单词吗?

java - Spring mongo 上的聚合

java - Java 文件扫描器的 NoSuchElementException

android - gradle项目中如何设置SDK目录

java - 如何添加 <a href> 链接以使元素在 Java 中可单击以供 Swagger 使用