I am trying to write an Eclipse External Annotation file for the static inner class Map.Entry. How do I do this?
I created a file named Map$Entry.eea in the java/util sub-folder under the folder where all of my External Annotation files are located. Here are the contents of that file.
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 still flags a warning on entry.getValue() in the following code:
Map.Entry<@Nullable String, @NonNull Object> entry;
@NonNull Object value = entry.getValue();
The warning is:
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.
The second line after
getValuemust be()T1V;instead of()TV;for@NonNull:For
@Nullableit would be()T0V;But because the value of a map entry can be null ("it's also possible that the map explicitly maps the key to null"), there should only be a warning at
@NonNull Object value = entry.getValue();if the type ofentryisMap.Entry<String, @Nullable Object>but not if the type isMap.Entry<String, @NonNull Object>. To tell Eclipse thatMap.Entryis designed with null type annotations in mind thejava/util/Map$Entry.eeafile must be almost empty:But be careful, if you do the same for
Mapthen there would be no warning for@NonNull Object o = map.get("foo");. You have to annotate all@Nullabletype parameters explicitly:See also: Eclipse 4.6 New and Noteworthy - Improved null analysis with generics