File: EnsuresNonNullIfTest2.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 (67 lines) | stat: -rw-r--r-- 1,761 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import org.checkerframework.checker.nullness.qual.*;
import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf;

/** Test case for issue 53: https://github.com/typetools/checker-framework/issues/53 */
public class EnsuresNonNullIfTest2 {

    private @Nullable Long id;

    public @org.checkerframework.dataflow.qual.Pure @Nullable Long getId() {
        return id;
    }

    @EnsuresNonNullIf(result = true, expression = "getId()")
    public boolean hasId2() {
        return getId() != null;
    }

    @EnsuresNonNullIf(result = true, expression = "id")
    public boolean hasId11() {
        return id != null;
    }

    @EnsuresNonNullIf(result = true, expression = "id")
    public boolean hasId12() {
        return this.id != null;
    }

    @EnsuresNonNullIf(result = true, expression = "this.id")
    public boolean hasId13() {
        return id != null;
    }

    @EnsuresNonNullIf(result = true, expression = "this.id")
    public boolean hasId14() {
        return this.id != null;
    }

    void client() {
        if (hasId11()) {
            id.toString();
        }
        if (hasId12()) {
            id.toString();
        }
        if (hasId13()) {
            id.toString();
        }
        if (hasId14()) {
            id.toString();
        }
        // :: error: (dereference.of.nullable)
        id.toString();
    }

    // Expressions referring to enclosing classes should be resolved.
    class Inner {
        @EnsuresNonNullIf(result = true, expression = "getId()")
        public boolean innerHasGetIdMethod() {
            return getId() != null;
        }

        @EnsuresNonNullIf(result = true, expression = "id")
        public boolean innerHasIdField() {
            return id != null;
        }
    }
}