이 예제 코드에서 경고를 제거하는 방법.TreeMap의 eclipse 네온에서 "Null 타입 안전"경고를 제거하는 방법
내가 자바 1.8와 이클립스 네온을 사용하고 org.eclipse.jdt.annotation_2.1.0
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@NonNullByDefault
public class NullAnnotationTest4 {
public static void main(String[] args) {
final TreeMap<@Nullable Integer, @Nullable String> treeMap = new TreeMap<>();
treeMap.put(3, "Test1");
treeMap.put(null, null);
//This produces the warning
final Set<@Nullable Entry<@Nullable Integer, @Nullable String>> set = treeMap.entrySet();
for (final Iterator<@Nullable Entry<@Nullable Integer, @Nullable String>> it = set.iterator(); it.hasNext();) {
final Entry<@Nullable Integer, @Nullable String> entry = it.next();
if (entry != null && entry.getKey() == null && entry.getValue() != null)
System.out.println(entry.getKey()+" is mapped to "+entry.getValue());
}
}
}
경고입니다 : 내가 @Nullable과의 여러 가지 조합을 시도
Null type safety (type annotations):
The expression of type
'Set<Map.Entry<@Nullable Integer,@Nullable String>>'
needs unchecked conversion to conform to
'Set<[email protected] Entry<@Nullable Integer,@Nullable String>>'
@ NonNullable. 심지어 ? extends
같은 비슷한 경우에 제안했다 : https://bugs.eclipse.org/bugs/show_bug.cgi?id=507779
그러나 경고는 항상 이동하지만 완전히 멀리 가지 않습니다.
업데이트 :이 줄을 사용하여 경고를 제거있어
:
final Set<? extends @Nullable Entry<@Nullable Integer, @Nullable String>> set = treeMap.entrySet();
하지만 완전히 이유로 손실입니다. 나에게 트랙이나 무언가를 잃어 버리면 유효성 검사기를 속이며 코드가 실제로 추악 해지기 시작합니다.
전체 새로운 코드 :
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
@NonNullByDefault
public class NullAnnotationTest4 {
public static void main(String[] args) {
final TreeMap<@Nullable Integer, @Nullable String> treeMap = new TreeMap<>();
treeMap.put(3, "Test1");
treeMap.put(null, null);
final Set<? extends @Nullable Entry<@Nullable Integer, @Nullable String>> set = treeMap.entrySet();
for (final Iterator<? extends @Nullable Entry<@Nullable Integer, @Nullable String>> it = set.iterator(); it.hasNext();) {
final Entry<@Nullable Integer, @Nullable String> entry = it.next();
if (entry != null && entry.getKey() == null && entry.getValue() != null)
System.out.println(entry.getKey()+" is mapped to "+entry.getValue());
}
}
}
업데이트 2 쇼티는 잘못된 코드를 붙여 넣습니다.
'set' 변수는'Set>'일까요? 엔트리 세트의 엔트리는 아마 null이 아니어야합니다. –
나는 그것을 먼저 시도했다. 그러나 오류는 @NonNullByDefault 때문에'Set
Torge