@TypeQualifier @SubtypeOf(value=UnknownKeyFor.class) @Documented @Retention(value=RUNTIME) @Target(value={TYPE_USE,TYPE_PARAMETER}) public @interface KeyFor
The value of the annotation should be the reference name of the map. The following declaration for example:
Map<String, String> config = ...;
@KeyFor("config") String key = "HOSTNAME";
String hostname = config.get(key); // known to be non-null
indicates that "HOSTNAME" is a key in config. The Nullness
checker deduce this information to deduce that hostname
reference
is a nonnull reference.
Here is a non-trivial example use:
// Return a sorted version of the Map's key set public static <K,V> Collection<@KeyFor("#1") K> sortedKeySet(Map<K,V> m, Comparator<K> comparator) { ArrayList<@KeyFor("#1") K> theKeys = new ArrayList<@KeyFor("#1") K> (m.keySet()); Collections.sort (theKeys, comparator); return theKeys; }
Limitation: The Nullness Checker trusts the user and doesn't validate the annotations. Future releases will check for the presence of the key in the map (when possible).
Modifier and Type | Required Element and Description |
---|---|
java.lang.String[] |
value
Java expression(s) that evaluate to a map for which the annotated type is a key.
|
public abstract java.lang.String[] value