File: Issue1797.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 (258 lines) | stat: -rw-r--r-- 6,411 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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
// Test case for Issue 1797:
// https://github.com/typetools/checker-framework/issues/1797

import org.checkerframework.checker.nullness.qual.Nullable;

class Issue1797 {
    void fooReturn(@Nullable Object o) {
        try {
            return;
        } finally {
            if (o != null) {
                o.toString();
            }
        }
    }

    void fooWhileReturn(@Nullable Object o) {
        while (this.hashCode() < 5) {
            try {
                return;
            } finally {
                if (o != null) {
                    o.toString();
                }
            }
        }
    }

    void fooReturnNested(@Nullable Object o) {
        while (this.hashCode() < 10) {
            while (this.hashCode() < 5) {
                try {
                    return;
                } finally {
                    if (o != null) {
                        o.toString();
                    }
                    // go to exit, not either loop
                }
            }
        }
    }

    void fooBreak(@Nullable Object o) {
        while (this.hashCode() < 5) {
            try {
                break;
            } finally {
                if (o != null) {
                    o.toString();
                }
            }
        }
    }

    void fooBreakLabel(@Nullable Object o) {
        outer:
        while (this.hashCode() < 10) {
            while (this.hashCode() < 5) {
                try {
                    break outer;
                } finally {
                    if (o != null) {
                        o.toString();
                    }
                    // continue after outer
                }
            }
        }
    }

    void fooBreakLabel2(@Nullable Object o) {
        outer:
        while (this.hashCode() < 10) {
            try {
                inner:
                while (this.hashCode() < 5) {
                    if (this.hashCode() < 2) {
                        break outer;
                        // go to finally
                    } else {
                        break inner;
                        // do not go to finally
                    }
                }
            } finally {
                if (o != null) {
                    o.toString();
                }
                // continue either at outer or after outer
            }
        }
    }

    void fooBreakNoLabel(@Nullable Object o) {
        outer:
        while (this.hashCode() < 10) {
            inner:
            while (this.hashCode() < 5) {
                try {
                    break;
                } finally {
                    if (o != null) {
                        o.toString();
                    }
                    // continue at outer
                }
            }
        }
    }

    void fooContinue(@Nullable Object o) {
        while (this.hashCode() < 5) {
            try {
                continue;
            } finally {
                if (o != null) {
                    o.toString();
                }
            }
        }
    }

    void fooContinueLabel(@Nullable Object o) {
        outer:
        while (this.hashCode() < 10) {
            while (this.hashCode() < 5) {
                try {
                    continue outer;
                } finally {
                    if (o != null) {
                        o.toString();
                    }
                }
            }
        }
    }

    void fooSwitch(@Nullable Object o) {
        switch (this.hashCode()) {
            case 1:
                try {
                    break;
                } finally {
                    if (o != null) {
                        o.toString();
                    }
                }
            default:
        }
    }

    // A few tests to make sure also return with expression works.

    int barReturn(@Nullable Object o) {
        try {
            return 5;
        } finally {
            if (o != null) {
                o.toString();
            }
        }
    }

    int barReturnInFinally(@Nullable Object o) {
        try {
            return 5;
        } finally {
            if (o != null) {
                o.toString();
            }
            return 10;
        }
    }

    int barReturnNested(@Nullable Object o) {
        while (this.hashCode() < 10) {
            while (this.hashCode() < 5) {
                try {
                    return 5;
                } finally {
                    if (o != null) {
                        o.toString();
                    }
                    // goes to return 5, not either loop!
                }
            }
        }
        return 10;
    }

    @FunctionalInterface
    interface NullableParamFunction {
        String takeVal(@Nullable Object x);
    }

    void testLambda() {
        NullableParamFunction n1 = (@Nullable Object x) -> (x == null) ? "null" : x.toString();
        try {
            NullableParamFunction n2 = (@Nullable Object x) -> (x == null) ? "null" : x.toString();
        } finally {
            NullableParamFunction n3 = (x) -> (x == null) ? "null" : x.toString();
        }
    }

    boolean nestedCFGConstructionTest(@Nullable Object o) {
        boolean result = true;
        java.io.BufferedWriter out = null;
        try {
            try {
            } finally {
                out = new java.io.BufferedWriter(new java.io.OutputStreamWriter(System.err));
            }
            if (o != null) {
                o.toString();
                out.write(' ');
            }
        } catch (Exception e) {
        } finally {
        }
        return result;
    }

    void nestedTryFinally() {
        try {
            try {
            } finally {
            }
        } finally {
        }
    }

    boolean nestedCFGConstructionTest2() throws java.io.IOException {
        java.io.BufferedWriter out =
                new java.io.BufferedWriter(new java.io.OutputStreamWriter(System.err));
        try {
            try {
                return true;
            } finally {
            }
        } finally {
            out.write(' ');
            out.close();
        }
    }

    void nestedTryFinally2(java.io.BufferedWriter out) throws java.io.IOException {
        try {
            try {
                return;
            } finally {
            }
        } finally {
            out.write(' ');
            out.close();
        }
    }
}