File: Ternary.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,840 kB
  • sloc: java: 145,910; xml: 839; sh: 518; makefile: 401; perl: 26
file content (74 lines) | stat: -rw-r--r-- 1,742 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
68
69
70
71
72
73
74
import java.lang.ref.WeakReference;

class Ternary<F> {
    void m1(boolean b) {
        String s = b ? new String("foo") : null;
    }

    void m2(boolean b) {
        String s = b ? null : new String("foo");
    }

    @SuppressWarnings("nullness") // Don't want to depend on @Nullable
    String m3(boolean b) {
        return b ? new String("foo") : null;
    }

    void m4(boolean b) {
        String[] s = b ? new String[] {""} : null;
    }

    void m5(boolean b) {
        Object o = new Object();
        String s = b ? (String) o : null;
    }

    void m6(boolean b) {
        String p = "x*(((";
        String s = b ? p : null;
    }

    class Generic<T extends Object> {
        void cond(boolean b, T p1, T p2) {
            p1 = b ? p1 : p2;
        }
    }

    void array(boolean b) {
        String[] s = b ? new String[] {""} : null;
    }

    void generic(boolean b, Generic<String> p) {
        Generic<String> s = b ? p : null;
    }

    void primarray(boolean b) {
        long[] result = b ? null : new long[10];
    }

    void vars() {
        // String and Integer generate an intersection type.
        String c = null;
        Integer m = null;
        Object s = (m != null) ? m : c;
    }

    void vars2() {
        // String and Integer generate an intersection type.
        String c = null;
        Integer m = null;
        Object s = (m != null) ? m : c;
    }

    public void test(MyWeakRef<? extends F> existingRef) {
        @SuppressWarnings("known.nonnull")
        F existing = existingRef == null ? null : existingRef.get();
    }

    private static final class MyWeakRef<L> extends WeakReference<L> {

        public MyWeakRef(L referent) {
            super(referent);
        }
    }
}