I get the error...
The return type is incompatible with 'Set<Map.Entry<K,T>>' returned from Map<K,T>.entrySet() (mismatching null constraints)
...when implementing a Map
and overriding Map.entrySet
like this:
package org.abego.util;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
...
public abstract class MyMap<K, T> implements Map<K, T> {
private Map<K, T> map = new LinkedHashMap<>();
@Override
public Set<java.util.Map.Entry<K, T>> entrySet() {
return map.entrySet();
}
...
}
The package org.abego.util
defines the default nullness to be @NonNull
:
@org.eclipse.jdt.annotation.NonNullByDefault
package org.abego.util;
The only way I found to get rid of the error was to 'remove the default nullness annotation' for entrySet
using an @NonNullByDefault({})
annotation:
package org.abego.util;
...
import org.eclipse.jdt.annotation.NonNullByDefault;
public abstract class MyMap<K, T> implements Map<K, T> {
...
@Override
@NonNullByDefault({})
public Set<java.util.Map.Entry<K, T>> entrySet() {
return map.entrySet();
}
...
}
While this does work I am wondering if this is the correct way to fix the error.
(I am using Eclipse 4.5 (Mars) and jdk1.8.0_60.)