File: KeyForPostcondition.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (47 lines) | stat: -rw-r--r-- 1,243 bytes parent folder | download | duplicates (3)
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
import java.util.HashMap;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.EnsuresKeyFor;
import org.checkerframework.checker.nullness.qual.EnsuresKeyForIf;
import org.checkerframework.checker.nullness.qual.KeyFor;

class KeyForPostcondition {

    public static Map<String, Integer> m = new HashMap<>();

    // public static @KeyFor("m") String key = "hello";

    public static boolean b;

    @EnsuresKeyFor(value = "#1", map = "m")
    public void putKey(String x) {
        m.put(x, 22);
    }

    public void usePutKey(String x) {
        // :: error: (assignment.type.incompatible)
        @KeyFor("m") String a = x;
        putKey(x);
        @KeyFor("m") String b = x;
    }

    @EnsuresKeyForIf(expression = "#1", result = true, map = "m")
    public boolean tryPutKey(String x) {
        if (b) {
            putKey(x);
            return true;
        } else {
            return false;
        }
    }

    public void useTryPutKey(String x) {
        // :: error: (assignment.type.incompatible)
        @KeyFor("m") String a = x;
        if (tryPutKey(x)) {
            @KeyFor("m") String b = x;
        }

        // :: error: (assignment.type.incompatible)
        @KeyFor("m") String c = x;
    }
}