Not annotated method overrides method annotated with @NotNull

JavaIntellij IdeaAnnotationsNotnull

Java Problem Overview


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

Java Solutions


Solution 1 - Java

Try adding @Nonnull (https://jsr-305.googlecode.com/svn/trunk/javadoc/javax/annotation/Nonnull.html">javax.annotation.Nonnull</a>;) 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.

Solution 2 - Java

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

Solution 3 - Java

Try adding:

private ListHashSet() {}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBrDaHaView Question on Stackoverflow
Solution 1 - JavaInvisible ArrowView Answer on Stackoverflow
Solution 2 - JavadokuView Answer on Stackoverflow
Solution 3 - JavaTotoroView Answer on Stackoverflow