1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.KeyFor;
import org.checkerframework.checker.nullness.qual.KeyForBottom;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.checkerframework.dataflow.qual.Pure;
import org.checkerframework.framework.qual.Covariant;
import org.checkerframework.framework.qual.DefaultQualifier;
import org.checkerframework.framework.qual.TypeUseLocation;
@DefaultQualifier(value = NonNull.class, locations = TypeUseLocation.IMPLICIT_UPPER_BOUND)
public class KeyForChecked {
interface KFMap<@KeyForBottom K extends @NonNull Object, V extends @NonNull Object> {
@Covariant(0)
public static interface Entry<
@KeyForBottom K1 extends @Nullable Object, V1 extends @Nullable Object> {
K1 getKey();
V1 getValue();
}
@Pure
boolean containsKey(@Nullable Object a1);
@Pure
@Nullable V get(@Nullable Object a1);
@Nullable V put(K a1, V a2);
Set<@KeyFor("this") K> keySet();
Set<KFMap.Entry<@KeyFor("this") K, V>> entrySet();
KFIterator<K> iterator();
}
class KFHashMap<@KeyForBottom K2 extends @NonNull Object, V2 extends @NonNull Object>
implements KFMap<K2, V2> {
@Pure
public boolean containsKey(@Nullable Object a1) {
return false;
}
@Pure
public @Nullable V2 get(@Nullable Object a1) {
return null;
}
public @Nullable V2 put(K2 a1, V2 a2) {
return null;
}
public Set<@KeyFor("this") K2> keySet() {
return new HashSet<@KeyFor("this") K2>();
}
public Set<KFMap.Entry<@KeyFor("this") K2, V2>> entrySet() {
return new HashSet<KFMap.Entry<@KeyFor("this") K2, V2>>();
}
public KFIterator<K2> iterator() {
return new KFIterator<K2>();
}
}
@Covariant(0)
class KFIterator<@KeyForBottom E extends @Nullable Object> {}
void incorrect1(Object map) {
String nonkey = "";
// :: error: (assignment.type.incompatible)
@KeyFor("map") String key = nonkey;
}
void correct1(Object map) {
String nonkey = "";
@SuppressWarnings("assignment.type.incompatible")
@KeyFor("map") String key = nonkey;
}
void incorrect2() {
KFMap<String, Object> m = new KFHashMap<>();
m.put("a", new Object());
m.put("b", new Object());
m.put("c", new Object());
Collection<@KeyFor("m") String> coll = m.keySet();
@SuppressWarnings("assignment.type.incompatible")
@KeyFor("m") String newkey = "new";
coll.add(newkey);
// TODO: at this point, the @KeyFor annotation is violated
m.put("new", new Object());
}
void correct2() {
KFMap<String, Object> m = new KFHashMap<>();
m.put("a", new Object());
m.put("b", new Object());
m.put("c", new Object());
Collection<@KeyFor("m") String> coll = m.keySet();
@SuppressWarnings("assignment.type.incompatible")
@KeyFor("m") String newkey = "new";
m.put(newkey, new Object());
coll.add(newkey);
}
void iter() {
KFMap<String, Object> emap = new KFHashMap<>();
Set<@KeyFor("emap") String> s = emap.keySet();
Iterator<@KeyFor("emap") String> it = emap.keySet().iterator();
Iterator<@KeyFor("emap") String> it2 = s.iterator();
Collection<@KeyFor("emap") String> x = Collections.unmodifiableSet(emap.keySet());
for (@KeyFor("emap") String st : s) {}
for (String st : s) {}
Object bubu = new Object();
// :: error: (enhancedfor.type.incompatible)
for (@KeyFor("bubu") String st : s) {}
}
<T> void dominators(KFMap<T, List<T>> preds) {
for (T node : preds.keySet()) {}
for (@KeyFor("preds") T node : preds.keySet()) {}
}
void entrySet() {
KFMap<String, Object> emap = new KFHashMap<>();
Set<KFMap.Entry<@KeyFor("emap") String, Object>> es = emap.entrySet();
// KeyFor has to be explicit on the component to Entry sets because
// a) it's not clear which map the Entry set may have come from
// b) and there is no guarantee the map is still accessible
// :: error: (assignment.type.incompatible)
Set<KFMap.Entry<String, Object>> es2 = emap.entrySet();
}
public static <K, V> void mapToString(KFMap<K, V> m) {
Set<KFMap.Entry<@KeyFor("m") K, V>> eset = m.entrySet();
for (KFMap.Entry<@KeyFor("m") K, V> entry : m.entrySet()) {}
}
void testWF(KFMap<String, Object> m) {
KFIterator<String> it = m.iterator();
}
}
|