forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartiallyMaskedCatchTest.java
More file actions
107 lines (90 loc) · 3.79 KB
/
PartiallyMaskedCatchTest.java
File metadata and controls
107 lines (90 loc) · 3.79 KB
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
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class PartiallyMaskedCatchTest {
public static void method() {
try(ClosableThing thing = new ClosableThing()){
thing.doThing();
} catch (ExceptionB e) {
// reachable: ExceptionB is thrown by invocation of CloseableThing.doThing()
} catch (ExceptionA e) {
// reachable: ExceptionA is thrown by implicit invocation of CloseableThing.close()
} catch (IOException e) {
// unreachable: only more specific exceptions are thrown and caught by previous catch blocks
}
try(ClosableThing thing = new ClosableThing()){
thing.doThing();
} catch (ExceptionB | NullPointerException e) {
// reachable: ExceptionB is thrown by invocation of CloseableThing.doThing()
} catch (ExceptionA | RuntimeException e) {
// reachable: ExceptionA is thrown by implicit invocation of CloseableThing.close()
} catch (IOException e) {
// unreachable: only more specific exceptions are thrown and caught by previous catch blocks
}
try(ClosableThing thing = new ClosableThing()){
thing.doThing();
} catch (ExceptionB | NullPointerException e) {
// reachable: ExceptionB is thrown by invocation of CloseableThing.doThing()
} catch (ExceptionA | IllegalArgumentException e) {
// reachable: ExceptionA is thrown by implicit invocation of CloseableThing.close()
} catch (IOException | RuntimeException e) {
// unreachable for type IOException: only more specific exceptions are thrown and caught by previous catch blocks
}
try {
throwingMethod();
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("");
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("");
} catch (InvocationTargetException ite) {
throw new IllegalStateException("");
}
try {
Class<?> clazz = Class.forName("", true, null).asSubclass(null);
Constructor<?> constructor = clazz.getConstructor();
constructor.newInstance();
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("");
} catch (NoSuchMethodException | InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("");
} catch (InvocationTargetException ite) {
throw new IllegalStateException("");
}
try(ClosableThing thing = getClosableThing()){
thing.doThing();
} catch (ExceptionB e) {
// reachable: ExceptionB is thrown by invocation of CloseableThing.doThing()
} catch (ExceptionA e) {
// reachable: ExceptionA is thrown by implicit invocation of CloseableThing.close()
} catch (IOException e) {
// reachable: IOException is thrown by getClosableThing()
}
try (ClosableThing thing = new ClosableThing()) {
genericThrowingMethod(IOException.class);
} catch (ExceptionA e) {
// reachable: ExceptionA is thrown by implicit invocation of CloseableThing.close()
} catch (IOException e) {
// reachable: IOException is thrown by invocation of genericThrowingMethod(IOException.class)
}
}
public static ClosableThing getClosableThing() throws IOException {
return new ClosableThing();
}
public static class ClosableThing implements Closeable {
@Override
public void close() throws ExceptionA {}
public void doThing() throws ExceptionB {}
}
public static class ExceptionA extends IOException {
private static final long serialVersionUID = 1L;
}
public static class ExceptionB extends ExceptionA {
private static final long serialVersionUID = 1L;
}
public static void throwingMethod()
throws ClassNotFoundException,
NoSuchMethodException, InstantiationException, IllegalAccessException,
InvocationTargetException {}
public static <E extends Exception> void genericThrowingMethod(Class<E> c) throws E {}
}