-
Notifications
You must be signed in to change notification settings - Fork 578
/
ThrowingExceptions.java
208 lines (176 loc) · 7.88 KB
/
ThrowingExceptions.java
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
package edu.umd.cs.findbugs.detect;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.OpcodeStack;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.ba.AnalysisContext;
import edu.umd.cs.findbugs.ba.SignatureParser;
import edu.umd.cs.findbugs.ba.XMethod;
import edu.umd.cs.findbugs.bcel.OpcodeStackDetector;
import edu.umd.cs.findbugs.internalAnnotations.DottedClassName;
import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.bcel.Const;
import org.apache.bcel.classfile.Code;
import org.apache.bcel.classfile.ExceptionTable;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.classfile.Method;
import org.apache.commons.lang3.StringUtils;
public class ThrowingExceptions extends OpcodeStackDetector {
private final BugReporter bugReporter;
public ThrowingExceptions(BugReporter bugReporter) {
this.bugReporter = bugReporter;
}
private JavaClass clazz;
private String exceptionThrown = null;
@Override
public void visit(JavaClass obj) {
exceptionThrown = null;
clazz = obj;
}
@Override
public void visit(Method obj) {
exceptionThrown = null;
if (obj.isSynthetic()) {
return;
}
Stream<String> exceptionStream = null;
// If the method is generic or is a method of a generic class, then first check the generic signature to avoid detection
// of generic descendants of Exception or Throwable as Exception or Throwable itself.
String signature = obj.getGenericSignature();
String[] exceptions = null;
if (signature != null) {
exceptions = StringUtils.substringsBetween(signature, "^", ";");
if (exceptions != null) {
exceptionStream = Arrays.stream(exceptions)
.filter(s -> s.charAt(0) == 'L')
.map(s -> s.substring(1).replace('/', '.'));
}
}
// If the method is not generic or it does not throw a generic exception then it has no exception specification in its generic
// signature.
if (signature == null || exceptions == null) {
ExceptionTable exceptionTable = obj.getExceptionTable();
if (exceptionTable != null) {
exceptionStream = Arrays.stream(exceptionTable.getExceptionNames());
}
}
// If the method throws Throwable or Exception because its ancestor throws the same exception then ignore it.
if (exceptionStream != null) {
Optional<String> exception = exceptionStream
.filter(s -> "java.lang.Exception".equals(s) || "java.lang.Throwable".equals(s))
.findAny();
if (exception.isPresent() && !parentThrows(obj, exception.get())) {
exceptionThrown = exception.get();
}
}
// If the method's body is empty then we report the bug immediately.
if (obj.getCode() == null && exceptionThrown != null) {
reportBug("java.lang.Exception".equals(exceptionThrown) ? "THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION"
: "THROWS_METHOD_THROWS_CLAUSE_THROWABLE", getXMethod());
}
}
@Override
public void visitAfter(Code obj) {
// For methods with bodies we report the exception after checking their body.
if (exceptionThrown != null) {
reportBug("java.lang.Exception".equals(exceptionThrown) ? "THROWS_METHOD_THROWS_CLAUSE_BASIC_EXCEPTION"
: "THROWS_METHOD_THROWS_CLAUSE_THROWABLE", getXMethod());
}
}
@Override
public void sawOpcode(int seen) {
if (seen == Const.ATHROW) {
OpcodeStack.Item item = stack.getStackItem(0);
if (item != null) {
if ("Ljava/lang/RuntimeException;".equals(item.getSignature())) {
reportBug("THROWS_METHOD_THROWS_RUNTIMEEXCEPTION", getXMethod());
}
}
} else if (exceptionThrown != null &&
(seen == Const.INVOKEVIRTUAL ||
seen == Const.INVOKEINTERFACE ||
seen == Const.INVOKESTATIC)) {
// If the method throws Throwable or Exception because it invokes another method throwing such
// exceptions then ignore this bug by resetting exceptionThrown to null.
XMethod calledMethod = getXMethodOperand();
if (calledMethod == null) {
return;
}
String[] thrownExceptions = calledMethod.getThrownExceptions();
if (thrownExceptions != null && Arrays.stream(thrownExceptions)
.map(s -> s.replace('/', '.'))
.anyMatch(exceptionThrown::equals)) {
exceptionThrown = null;
}
}
}
private void reportBug(String bugName, XMethod method) {
bugReporter.reportBug(new BugInstance(this, bugName, LOW_PRIORITY).addClass(this).addMethod(method));
}
private boolean parentThrows(@NonNull Method method, @DottedClassName String exception) {
return parentThrows(clazz, method, exception);
}
private boolean parentThrows(@NonNull JavaClass clazz, @NonNull Method method, @DottedClassName String exception) {
JavaClass ancestor;
boolean throwsEx = false;
try {
ancestor = clazz.getSuperClass();
if (ancestor != null) {
Optional<Method> superMethod = Arrays.stream(ancestor.getMethods())
.filter(m -> method.getName().equals(m.getName()) && signatureMatches(method, m))
.findAny();
if (superMethod.isPresent()) {
throwsEx = Arrays.stream(superMethod.get().getExceptionTable().getExceptionNames())
.anyMatch(exception::equals);
} else {
throwsEx = parentThrows(ancestor, method, exception);
}
}
for (JavaClass intf : clazz.getInterfaces()) {
Optional<Method> superMethod = Arrays.stream(intf.getMethods())
.filter(m -> method.getName().equals(m.getName()) && signatureMatches(method, m))
.findAny();
if (superMethod.isPresent()) {
throwsEx |= Arrays.stream(superMethod.get().getExceptionTable().getExceptionNames())
.anyMatch(exception::equals);
} else {
throwsEx |= parentThrows(intf, method, exception);
}
}
} catch (ClassNotFoundException e) {
AnalysisContext.reportMissingClass(e);
}
return throwsEx;
}
private boolean signatureMatches(Method child, Method parent) {
String genSig = parent.getGenericSignature();
if (genSig == null) {
return child.getSignature().equals(parent.getSignature());
}
String sig = child.getSignature();
SignatureParser genSP = new SignatureParser(genSig);
SignatureParser sp = new SignatureParser(sig);
if (genSP.getNumParameters() != sp.getNumParameters()) {
return false;
}
String[] gArgs = genSP.getArguments();
String[] args = sp.getArguments();
for (int i = 0; i < sp.getNumParameters(); ++i) {
if (gArgs[i].charAt(0) == 'T') {
if (args[i].charAt(0) != 'L') {
return false;
}
} else {
if (!gArgs[i].equals(args[i])) {
return false;
}
}
}
String gRet = genSP.getReturnTypeSignature();
String ret = sp.getReturnTypeSignature();
return (gRet.charAt(0) == 'T' && ret.charAt(0) == 'L') ||
gRet.equals(ret);
}
}