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
|
// Test for Checker Framework issue 273:
// https://github.com/typetools/checker-framework/issues/273
import java.util.HashMap;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.*;
class KeyForShadowing {
public static void main(String... p) {
Map<String, Integer> m0 = new HashMap<>();
Map<String, Integer> m1 = new HashMap<>();
String k = "key";
m0.put(k, 1); // k is @KeyFor("m0") after this line
// We expect an error for the next one since we are not
// respecting the method contract. It expects the
// key to be for the second parameter, not the first.
// :: error: (argument.type.incompatible)
getMap3(m0, m1, k).toString();
// We expect an error for the next one since although
// we are respecting the method contract, since the
// key is for the first parameter, the Nullness Checker
// is misinterpreting "m1" to be the local m1 to this
// method, and not the first parameter to the method.
// :: error: (argument.type.incompatible)
getMap2(m0, m1, k).toString();
// :: error: (argument.type.incompatible)
getMap1(m0, m1, k).toString();
getMap4(m0, m1, k).toString();
}
public static @NonNull Integer getMap1(
Map<String, Integer> m1, // m1,m0 flipped
Map<String, Integer> m0,
// :: error: (expression.unparsable.type.invalid)
@KeyFor("m0") String k) {
// :: error: (return.type.incompatible)
return m0.get(k);
}
public static @NonNull Integer getMap2(
Map<String, Integer> m1, // m1,m0 flipped
Map<String, Integer> m0,
// :: error: (expression.unparsable.type.invalid)
@KeyFor("m1") String k) {
// This method body is incorrect.
// We expect this error because we are indicating that
// the key is for m1, so m0.get(k) is @Nullable.
// :: error: (return.type.incompatible)
return m0.get(k);
}
public static @NonNull Integer getMap3(
Map<String, Integer> m1, // m1,m0 flipped
Map<String, Integer> m0,
@KeyFor("#2") String k) {
return m0.get(k);
}
public static @NonNull Integer getMap4(
Map<String, Integer> m1, // m1,m0 flipped
Map<String, Integer> m0,
@KeyFor("#1") String k) {
// This method body is incorrect.
// We expect this error because we are indicating that
// the key is for m1, so m0.get(k) is @Nullable.
// :: error: (return.type.incompatible)
return m0.get(k);
}
}
|