Not annotated method overrides method annotated with @NotNull
Asked Answered
A

4

68

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:

Not annotated method overrides method annotated with @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 answer to (hopefully) resolve your issue.


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:

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);
}
Autobiographical answered 30/6, 2014 at 17:13 Comment(7)
Intellij has its own NotNull annotation class. I wonder if its doing some slight of hand around the return list.toArray which is the only place you can have an NPE potentialJaime
It's not possible for the list field to be null. Unless I missed something?Autobiographical
there is an implicit ListHashSet() constructor added in by the compilerJaime
try intellij forums/support?Jaime
I tried asking over at the IntelliJ forums, but got nowhere. This is probably a bug in IntelliJAutobiographical
Sometimes, it helps to add a nullability check. In my case, there was the @ParametersAreNonnullByDefault an annotation on both the interface and implementing class packages. However, IDEA warned me that the overridden method param is not annotated until I added Preconditions.checkNotNull(...) for the param in the implementing class.Allerie
It's weird I'm still getting upvotes for this question, 5 years later. No one's made a fix?Autobiographical
S
99

Try adding @Nonnull (javax.annotation.Nonnull) to the toArray method.

The warning disappeared for me when I added this annotation. I think the warning message is incorrect which says that @NotNull is missing.

Slesvig answered 14/1, 2015 at 12:23 Comment(3)
Oh man finally! that worked! So, you're suggesting that the warning saying "@NotNull is missing" is actually a typo in the warning message?Autobiographical
For those who don't want to put an annotation on every method method in a large class, you can add @NonNullApi on the package level: https://mcmap.net/q/281599/-how-do-i-add-package-level-annotations-or-edit-package-info-javaKendyl
For those using newer versions of Java/SpringBoot, import is jakarta.annotation.NonnullEuphemize
B
8

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.

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")

Breger answered 12/11, 2015 at 0:3 Comment(3)
IIRC we were using the Oracle JDK, so OpenJDK's implementation might be differentAutobiographical
+1 for stating the correct @SuppressWarnings directive which seems to be the only option no relying on dependencies.Wendish
I would disagree that "SuppressWarnings" is the correct approach here.Autobiographical
A
1

I also face the same and solve it by adding package-info.java for that package. Under package-info.java added annotation @NonNullApi. my package-info.java looks like -

@NonNullApi
package org.example.auth.repositories;

import org.springframework.lang.NonNullApi;
Automobile answered 22/5, 2023 at 12:55 Comment(0)
J
0

Try adding:

private ListHashSet() {}
Jaime answered 30/6, 2014 at 17:59 Comment(2)
I actually had these additional constructors (Updated in the original question) ^Autobiographical
This isn't the cause, but you think you might want to set list here? private ArrayList<T> getList() { return list != null ? list : new ArrayList<T>(); }Jaime

© 2022 - 2024 — McMap. All rights reserved.