| 1 |
// Test case for issue #293: https://github.com/typetools/checker-framework/issues/293 |
| 2 |
// Thanks to Ed Price for the test case. |
| 3 |
|
| 4 |
// Design space: |
| 5 |
// try |
| 6 |
// ... |
| 7 |
// catch: 7 varieties |
| 8 |
// * absent |
| 9 |
// * catch Throwable |
| 10 |
// * catch Exception |
| 11 |
// for each of the "present" types of catch: |
| 12 |
// * no action |
| 13 |
// * call method |
| 14 |
// * assign string |
| 15 |
// finally: 2 varieties |
| 16 |
// * absent |
| 17 |
// * no action |
| 18 |
// Naming indicates which one; overall 14 tests. |
| 19 |
|
| 20 |
// Empty, to give this file its name. |
| 21 |
public class TryFinally {} |
| 22 |
|
| 23 |
class TestCabsentFabsent { |
| 24 |
static String getFoo() { |
| 25 |
return "foo"; |
| 26 |
} |
| 27 |
|
| 28 |
private final String foo; |
| 29 |
|
| 30 |
public TestCabsentFabsent() { |
| 31 |
this.foo = getFoo(); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
class TestCabsentFnoaction { |
| 36 |
static String getFoo() { |
| 37 |
return "foo"; |
| 38 |
} |
| 39 |
|
| 40 |
private final String foo; |
| 41 |
|
| 42 |
public TestCabsentFnoaction() { |
| 43 |
try { |
| 44 |
this.foo = getFoo(); |
| 45 |
} finally { |
| 46 |
// no action in finally clause |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// Not legal in Java: error: variable foo might not have been initialized |
| 52 |
// class TestCtnoactionFabsent { |
| 53 |
// static String getFoo() { return "foo"; } |
| 54 |
// private final String foo; |
| 55 |
// public TestCtnoactionFabsent() { |
| 56 |
// try { |
| 57 |
// this.foo = getFoo(); |
| 58 |
// } catch (Throwable t) { |
| 59 |
// // no action on exception |
| 60 |
// } |
| 61 |
// } |
| 62 |
// } |
| 63 |
|
| 64 |
// Not legal in Java: error: variable foo might not have been initialized |
| 65 |
// class TestCtnoactionFnoaction { |
| 66 |
// static String getFoo() { return "foo"; } |
| 67 |
// private final String foo; |
| 68 |
// public TestCtnoactionFnoaction() { |
| 69 |
// try { |
| 70 |
// this.foo = getFoo(); |
| 71 |
// } catch (Throwable t) { |
| 72 |
// // no action on exception |
| 73 |
// } finally { |
| 74 |
// // no action in finally clause |
| 75 |
// } |
| 76 |
// } |
| 77 |
// } |
| 78 |
|
| 79 |
// Not legal in Java: error: variable foo might already have been assigned |
| 80 |
// class TestCtmethodFabsent { |
| 81 |
// static String getFoo() { return "foo"; } |
| 82 |
// private final String foo; |
| 83 |
// public TestCtmethodFabsent() { |
| 84 |
// try { |
| 85 |
// this.foo = getFoo(); |
| 86 |
// } catch (Throwable t) { |
| 87 |
// this.foo = getFoo(); |
| 88 |
// } |
| 89 |
// } |
| 90 |
// } |
| 91 |
|
| 92 |
// Not legal in Java: error: variable foo might already have been assigned |
| 93 |
// class TestCtmethodFnoaction { |
| 94 |
// static String getFoo() { return "foo"; } |
| 95 |
// private final String foo; |
| 96 |
// public TestCtmethodFnoaction() { |
| 97 |
// try { |
| 98 |
// this.foo = getFoo(); |
| 99 |
// } catch (Throwable t) { |
| 100 |
// this.foo = getFoo(); |
| 101 |
// } finally { |
| 102 |
// // no action in finally clause |
| 103 |
// } |
| 104 |
// } |
| 105 |
// } |
| 106 |
|
| 107 |
// Not legal in Java: error: variable foo might already have been assigned |
| 108 |
// class TestCtstringFabsent { |
| 109 |
// static String getFoo() { return "foo"; } |
| 110 |
// private final String foo; |
| 111 |
// public TestCtstringFabsent() { |
| 112 |
// try { |
| 113 |
// this.foo = getFoo(); |
| 114 |
// } catch (Throwable t) { |
| 115 |
// this.foo = "foo"; |
| 116 |
// } |
| 117 |
// } |
| 118 |
// } |
| 119 |
|
| 120 |
// Not legal in Java: error: variable foo might already have been assigned |
| 121 |
// class TestCtstringFnoaction { |
| 122 |
// static String getFoo() { return "foo"; } |
| 123 |
// private final String foo; |
| 124 |
// public TestCtstringFnoaction() { |
| 125 |
// try { |
| 126 |
// this.foo = getFoo(); |
| 127 |
// } catch (Throwable t) { |
| 128 |
// this.foo = "foo"; |
| 129 |
// } finally { |
| 130 |
// // no action in finally clause |
| 131 |
// } |
| 132 |
// } |
| 133 |
// } |
| 134 |
|
| 135 |
// Not legal in Java: error: variable foo might not have been initialized |
| 136 |
// class TestCenoactionFabsent { |
| 137 |
// static String getFoo() { return "foo"; } |
| 138 |
// private final String foo; |
| 139 |
// public TestCenoactionFabsent() { |
| 140 |
// try { |
| 141 |
// this.foo = getFoo(); |
| 142 |
// } catch (Exception t) { |
| 143 |
// // no action on exception |
| 144 |
// } |
| 145 |
// } |
| 146 |
// } |
| 147 |
|
| 148 |
// Not legal in Java: error: variable foo might not have been initialized |
| 149 |
// class TestCenoactionFnoaction { |
| 150 |
// static String getFoo() { return "foo"; } |
| 151 |
// private final String foo; |
| 152 |
// public TestCenoactionFnoaction() { |
| 153 |
// try { |
| 154 |
// this.foo = getFoo(); |
| 155 |
// } catch (Exception t) { |
| 156 |
// // no action on exception |
| 157 |
// } finally { |
| 158 |
// // no action in finally clause |
| 159 |
// } |
| 160 |
// } |
| 161 |
// } |
| 162 |
|
| 163 |
// Not legal in Java: error: variable foo might already have been assigned |
| 164 |
// class TestCemethodFabsent { |
| 165 |
// static String getFoo() { return "foo"; } |
| 166 |
// private final String foo; |
| 167 |
// public TestCemethodFabsent() { |
| 168 |
// try { |
| 169 |
// this.foo = getFoo(); |
| 170 |
// } catch (Exception t) { |
| 171 |
// this.foo = getFoo(); |
| 172 |
// } |
| 173 |
// } |
| 174 |
// } |
| 175 |
|
| 176 |
// Not legal in Java: error: variable foo might already have been assigned |
| 177 |
// class TestCemethodFnoaction { |
| 178 |
// static String getFoo() { return "foo"; } |
| 179 |
// private final String foo; |
| 180 |
// public TestCemethodFnoaction() { |
| 181 |
// try { |
| 182 |
// this.foo = getFoo(); |
| 183 |
// } catch (Exception t) { |
| 184 |
// this.foo = getFoo(); |
| 185 |
// } finally { |
| 186 |
// // no action in finally clause |
| 187 |
// } |
| 188 |
// } |
| 189 |
// } |
| 190 |
|
| 191 |
// Not legal in Java: error: variable foo might already have been assigned |
| 192 |
// class TestCestringFabsent { |
| 193 |
// static String getFoo() { return "foo"; } |
| 194 |
// private final String foo; |
| 195 |
// public TestCestringFabsent() { |
| 196 |
// try { |
| 197 |
// this.foo = getFoo(); |
| 198 |
// } catch (Exception t) { |
| 199 |
// this.foo = "foo"; |
| 200 |
// } |
| 201 |
// } |
| 202 |
// } |
| 203 |
|
| 204 |
// Not legal in Java: error: variable foo might already have been assigned |
| 205 |
// class TestCestringFnoaction { |
| 206 |
// static String getFoo() { return "foo"; } |
| 207 |
// private final String foo; |
| 208 |
// public TestCestringFnoaction() { |
| 209 |
// try { |
| 210 |
// this.foo = getFoo(); |
| 211 |
// } catch (Exception t) { |
| 212 |
// this.foo = "foo"; |
| 213 |
// } finally { |
| 214 |
// // no action in finally clause |
| 215 |
// } |
| 216 |
// } |
| 217 |
// } |
| 218 |
|
| 219 |
class TestCabsentFabsentNonfinal { |
| 220 |
static String getFoo() { |
| 221 |
return "foo"; |
| 222 |
} |
| 223 |
|
| 224 |
private String foo; |
| 225 |
|
| 226 |
public TestCabsentFabsentNonfinal() { |
| 227 |
this.foo = getFoo(); |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
class TestCabsentFnoactionNonfinal { |
| 232 |
static String getFoo() { |
| 233 |
return "foo"; |
| 234 |
} |
| 235 |
|
| 236 |
private String foo; |
| 237 |
|
| 238 |
public TestCabsentFnoactionNonfinal() { |
| 239 |
try { |
| 240 |
this.foo = getFoo(); |
| 241 |
} finally { |
| 242 |
// no action in finally clause |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
class TestCtnoactionFabsentNonfinal { |
| 248 |
static String getFoo() { |
| 249 |
return "foo"; |
| 250 |
} |
| 251 |
|
| 252 |
private String foo; |
| 253 |
// :: error: initialization.fields.uninitialized |
| 254 |
public TestCtnoactionFabsentNonfinal() { |
| 255 |
try { |
| 256 |
this.foo = getFoo(); |
| 257 |
} catch (Throwable t) { |
| 258 |
// no action on exception |
| 259 |
} |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
class TestCtnoactionFnoactionNonfinal { |
| 264 |
static String getFoo() { |
| 265 |
return "foo"; |
| 266 |
} |
| 267 |
|
| 268 |
private String foo; |
| 269 |
// :: error: initialization.fields.uninitialized |
| 270 |
public TestCtnoactionFnoactionNonfinal() { |
| 271 |
try { |
| 272 |
this.foo = getFoo(); |
| 273 |
} catch (Throwable t) { |
| 274 |
// no action on exception |
| 275 |
} finally { |
| 276 |
// no action in finally clause |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
class TestCtmethodFabsentNonfinal { |
| 282 |
static String getFoo() { |
| 283 |
return "foo"; |
| 284 |
} |
| 285 |
|
| 286 |
private String foo; |
| 287 |
|
| 288 |
public TestCtmethodFabsentNonfinal() { |
| 289 |
try { |
| 290 |
this.foo = getFoo(); |
| 291 |
} catch (Throwable t) { |
| 292 |
this.foo = getFoo(); |
| 293 |
} |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
class TestCtmethodFnoactionNonfinal { |
| 298 |
static String getFoo() { |
| 299 |
return "foo"; |
| 300 |
} |
| 301 |
|
| 302 |
private String foo; |
| 303 |
|
| 304 |
public TestCtmethodFnoactionNonfinal() { |
| 305 |
try { |
| 306 |
this.foo = getFoo(); |
| 307 |
} catch (Throwable t) { |
| 308 |
this.foo = getFoo(); |
| 309 |
} finally { |
| 310 |
// no action in finally clause |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
class TestCtstringFabsentNonfinal { |
| 316 |
static String getFoo() { |
| 317 |
return "foo"; |
| 318 |
} |
| 319 |
|
| 320 |
private String foo; |
| 321 |
|
| 322 |
public TestCtstringFabsentNonfinal() { |
| 323 |
try { |
| 324 |
this.foo = getFoo(); |
| 325 |
} catch (Throwable t) { |
| 326 |
this.foo = "foo"; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
class TestCtstringFnoactionNonfinal { |
| 332 |
static String getFoo() { |
| 333 |
return "foo"; |
| 334 |
} |
| 335 |
|
| 336 |
private String foo; |
| 337 |
|
| 338 |
public TestCtstringFnoactionNonfinal() { |
| 339 |
try { |
| 340 |
this.foo = getFoo(); |
| 341 |
} catch (Throwable t) { |
| 342 |
this.foo = "foo"; |
| 343 |
} finally { |
| 344 |
// no action in finally clause |
| 345 |
} |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
class TestCenoactionFabsentNonfinal { |
| 350 |
static String getFoo() { |
| 351 |
return "foo"; |
| 352 |
} |
| 353 |
|
| 354 |
private String foo; |
| 355 |
// :: error: initialization.fields.uninitialized |
| 356 |
public TestCenoactionFabsentNonfinal() { |
| 357 |
try { |
| 358 |
this.foo = getFoo(); |
| 359 |
} catch (Exception t) { |
| 360 |
// no action on exception |
| 361 |
} |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
class TestCenoactionFnoactionNonfinal { |
| 366 |
static String getFoo() { |
| 367 |
return "foo"; |
| 368 |
} |
| 369 |
|
| 370 |
private String foo; |
| 371 |
// :: error: initialization.fields.uninitialized |
| 372 |
public TestCenoactionFnoactionNonfinal() { |
| 373 |
try { |
| 374 |
this.foo = getFoo(); |
| 375 |
} catch (Exception t) { |
| 376 |
// no action on exception |
| 377 |
} finally { |
| 378 |
// no action in finally clause |
| 379 |
} |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
class TestCemethodFabsentNonfinal { |
| 384 |
static String getFoo() { |
| 385 |
return "foo"; |
| 386 |
} |
| 387 |
|
| 388 |
private String foo; |
| 389 |
|
| 390 |
public TestCemethodFabsentNonfinal() { |
| 391 |
try { |
| 392 |
this.foo = getFoo(); |
| 393 |
} catch (Exception t) { |
| 394 |
this.foo = getFoo(); |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
class TestCemethodFnoactionNonfinal { |
| 400 |
static String getFoo() { |
| 401 |
return "foo"; |
| 402 |
} |
| 403 |
|
| 404 |
private String foo; |
| 405 |
|
| 406 |
public TestCemethodFnoactionNonfinal() { |
| 407 |
try { |
| 408 |
this.foo = getFoo(); |
| 409 |
} catch (Exception t) { |
| 410 |
this.foo = getFoo(); |
| 411 |
} finally { |
| 412 |
// no action in finally clause |
| 413 |
} |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
class TestCestringFabsentNonfinal { |
| 418 |
static String getFoo() { |
| 419 |
return "foo"; |
| 420 |
} |
| 421 |
|
| 422 |
private String foo; |
| 423 |
|
| 424 |
public TestCestringFabsentNonfinal() { |
| 425 |
try { |
| 426 |
this.foo = getFoo(); |
| 427 |
} catch (Exception t) { |
| 428 |
this.foo = "foo"; |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
|
| 433 |
class TestCestringFnoactionNonfinal { |
| 434 |
static String getFoo() { |
| 435 |
return "foo"; |
| 436 |
} |
| 437 |
|
| 438 |
private String foo; |
| 439 |
|
| 440 |
public TestCestringFnoactionNonfinal() { |
| 441 |
try { |
| 442 |
this.foo = getFoo(); |
| 443 |
} catch (Exception t) { |
| 444 |
this.foo = "foo"; |
| 445 |
} finally { |
| 446 |
// no action in finally clause |
| 447 |
} |
| 448 |
} |
| 449 |
} |