我正在尝试为静态内部类Map.Entry编写
Eclipse External Annotation文件.我该怎么做呢?
我在所有外部注释文件所在的文件夹下的java / util子文件夹中创建了一个名为Map $Entry.eea的文件.以下是该文件的内容.
class java/util/Map$Entry
comparingByKey
<K::Ljava/lang/Comparable<-TK;>;V:Ljava/lang/Object;>()Ljava/util/Comparator<Ljava/util/Map$Entry<TK;TV;>;>;
<K::Ljava/lang/Comparable<-TK;>;V:Ljava/lang/Object;>()L1java/util/Comparator<L1java/util/Map$Entry<T1K;TV;>;>;
comparingByKey
<K:Ljava/lang/Object;V:Ljava/lang/Object;>(Ljava/util/Comparator<-TK;>;)Ljava/util/Comparator<Ljava/util/Map$Entry<TK;TV;>;>;
<K:Ljava/lang/Object;V:Ljava/lang/Object;>(L1java/util/Comparator<-TK;>;)L1java/util/Comparator<L1java/util/Map$Entry<TK;TV;>;>;
comparingByValue
<K:Ljava/lang/Object;V::Ljava/lang/Comparable<-TV;>;>()Ljava/util/Comparator<Ljava/util/Map$Entry<TK;TV;>;>;
<K:Ljava/lang/Object;V::Ljava/lang/Comparable<-TV;>;>()L1java/util/Comparator<L1java/util/Map$Entry<TK;T1V;>;>;
comparingByValue
<K:Ljava/lang/Object;V:Ljava/lang/Object;>(Ljava/util/Comparator<-TV;>;)Ljava/util/Comparator<Ljava/util/Map$Entry<TK;TV;>;>;
<K:Ljava/lang/Object;V:Ljava/lang/Object;>(L1java/util/Comparator<-TV;>;)L1java/util/Comparator<L1java/util/Map$Entry<TK;TV;>;>;
equals
(Ljava/lang/Object;)Z
(L0java/lang/Object;)Z
getKey
()TK;
()TK;
getValue
()TV;
()TV;
setValue
(TV;)TV;
(TV;)TV;
Eclipse仍然在以下代码中的entry.getValue()上标记警告:
Map.Entry<@Nullable String, @NonNull Object> entry;
@NonNull Object value = entry.getValue();
警告是:
Unsafe interpretation of method return type as '@NonNull' based on the receiver type
'Map.<@NonNull Entry<@Nullable String, @NonNull Object>>'. Type Map.Entry<K, V> doesn't seem
to be designed with null type annotations in mind.
最佳答案 getValue之后的第二行必须是()T1V;而不是()电视;对于@NonNull:
class java/util/Map$Entry
getValue
()TV;
()T1V;
对于@Nullable,它将是()T0V;
但是因为map条目的值可以为null(“it’s also possible that the map explicitly maps the key to null”),所以@NonNull只应该有一个警告Object value = entry.getValue();如果条目的类型是Map.Entry< String,@ Nullable Object>但不是如果类型是Map.Entry< String,@ NonNull Object>.要告诉Eclipse Map.Entry设计时带有null类型注释,java / util / Map $Entry.eea文件必须几乎为空:
class java/util/Map$Entry
但要小心,如果你对Map做同样的事情,那么@NonNull对象o = map.get(“foo”);就没有警告.您必须显式注释所有@Nullable类型参数:
class java/util/Map
get
(Ljava/lang/Object;)TV;
(Ljava/lang/Object;)T0V;
另见:Eclipse 4.6 New and Noteworthy – Improved null analysis with generics