/
CheckRelaxingNullnessAnnotation.java
359 lines (328 loc) · 14.6 KB
/
CheckRelaxingNullnessAnnotation.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* FindBugs - Find Bugs in Java programs
* Copyright (C) 2003-2007 University of Maryland
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.umd.cs.findbugs.detect;
import static edu.umd.cs.findbugs.ba.NullnessAnnotation.CHECK_FOR_NULL;
import static edu.umd.cs.findbugs.ba.NullnessAnnotation.NONNULL;
import static edu.umd.cs.findbugs.ba.NullnessAnnotation.NULLABLE;
import static org.objectweb.asm.Opcodes.ACC_STATIC;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.AnnotationNode;
import org.objectweb.asm.tree.LocalVariableNode;
import org.objectweb.asm.tree.MethodNode;
import edu.umd.cs.findbugs.BugInstance;
import edu.umd.cs.findbugs.BugReporter;
import edu.umd.cs.findbugs.LocalVariableAnnotation;
import edu.umd.cs.findbugs.asm.ClassNodeDetector;
import edu.umd.cs.findbugs.ba.AnalysisContext;
import edu.umd.cs.findbugs.ba.Hierarchy;
import edu.umd.cs.findbugs.ba.NullnessAnnotation;
import edu.umd.cs.findbugs.ba.NullnessAnnotation.Parser;
import edu.umd.cs.findbugs.ba.SignatureParser;
import edu.umd.cs.findbugs.ba.XClass;
import edu.umd.cs.findbugs.ba.XMethod;
import edu.umd.cs.findbugs.classfile.CheckedAnalysisException;
import edu.umd.cs.findbugs.classfile.ClassDescriptor;
import edu.umd.cs.findbugs.classfile.Global;
import edu.umd.cs.findbugs.classfile.MethodDescriptor;
import edu.umd.cs.findbugs.classfile.analysis.AnnotationValue;
import edu.umd.cs.findbugs.classfile.engine.asm.FindBugsASM;
import edu.umd.cs.findbugs.internalAnnotations.SlashedClassName;
import edu.umd.cs.findbugs.util.ClassName;
/**
* Checks that overriding methods do not relax {@link Nonnull} (made
* {@link CheckForNull}) on return values or {@link CheckForNull} (made
* {@link Nonnull}) on parameters.
*
* The code accepts also old (deprecated) nullness annotations from
* {@code edu.umd.cs.findbugs.annotations} package.
*
* @author alienisty (Alessandro Nistico)
* @author Andrey Loskutov
*/
public class CheckRelaxingNullnessAnnotation extends ClassNodeDetector {
XClass xclass;
public CheckRelaxingNullnessAnnotation(BugReporter bugReporter) {
super(bugReporter);
}
@Override
public void visitClass(ClassDescriptor classDescriptor) throws CheckedAnalysisException {
xclass = getClassInfo(classDescriptor);
if (xclass != null) {
super.visitClass(classDescriptor);
}
}
@CheckForNull
XClass getClassInfo(ClassDescriptor classDescr) {
if (classDescr == null) {
return null;
}
try {
return Global.getAnalysisCache().getClassAnalysis(XClass.class, classDescr);
} catch (CheckedAnalysisException e) {
bugReporter.reportMissingClass(classDescr);
return null;
}
}
@Override
public MethodVisitor visitMethod(int methodAccess, String methodName, String desc, String methodSignature, String[] exceptions) {
if ((methodAccess & ACC_STATIC) != 0) {
// skip static methods
return null;
}
final XMethod xmethod = xclass.findMethod(methodName, desc, false);
if (xmethod == null) {
// unable to continue the analysis
bugReporter.reportSkippedAnalysis(new MethodDescriptor(xclass.getClassDescriptor().getClassName(), methodName, desc,
false));
return null;
}
return new DetectorNode(methodAccess, methodName, desc, methodSignature, exceptions, xmethod);
}
private final class DetectorNode extends MethodNode {
private final XMethod xmethod;
private Map<Integer, NullnessAnnotation> nonNullParameter;
private boolean relaxedNullReturn;
DetectorNode(int access, String name, String desc, String signature, String[] exceptions, XMethod xmethod) {
super(FindBugsASM.ASM_VERSION, access, name, desc, signature, exceptions);
this.xmethod = xmethod;
}
@Override
public void visitEnd() {
super.visitEnd();
// 1 test if we have suspicious annotations on method or parameters
relaxedNullReturn = containsRelaxedNonNull(visibleAnnotations);
if (!relaxedNullReturn) {
relaxedNullReturn = containsRelaxedNonNull(invisibleAnnotations);
}
boolean needsCheck = relaxedNullReturn;
if (invisibleParameterAnnotations != null || visibleParameterAnnotations != null) {
nonNullParameter = getNonnullOrNullableParams(visibleParameterAnnotations);
Map<Integer, NullnessAnnotation> nnp = getNonnullOrNullableParams(invisibleParameterAnnotations);
if (nnp != null) {
if (nonNullParameter == null) {
nonNullParameter = nnp;
} else {
nonNullParameter.putAll(nnp);
}
}
needsCheck |= !nonNullParameter.isEmpty();
}
if (!needsCheck) {
// we can stop, there is no direct violations due annotations applied on the method.
// However it would be nice to flag Bug2672946B violation too (where the entire class
// relaxes the parent contract by applying default annotations)
return;
// If we continue here, we will flag Bug2672946B violation too on method level,
// but it would be nice to do it on class or even package level (if package-info.java
// has default annotations applied which conflicts with parent class contract)
}
// 2 look in the hierarchy if we have relaxed contract
HierarchyIterator hierarchy = new HierarchyIterator(xclass);
XClass superClass;
boolean done = false;
while (!done && (superClass = hierarchy.next()) != null) {
XMethod method = superClass.findMethod(name, desc, false);
if (method != null) {
done = checkMethod(method);
} else {
for (XMethod superMethod : superClass.getXMethods()) {
if (name.equals(superMethod.getName()) && compatibleParameters(desc, superMethod.getSignature())) {
if (checkMethod(superMethod)) {
done = true;
break;
}
}
}
}
}
}
private final boolean checkMethod(@Nonnull XMethod method) {
boolean foundAny = false;
if (relaxedNullReturn && containsNullness(method.getAnnotations(), NONNULL)) {
BugInstance bug = new BugInstance(CheckRelaxingNullnessAnnotation.this, "NP_METHOD_RETURN_RELAXING_ANNOTATION",
HIGH_PRIORITY);
bug.addClassAndMethod(xmethod);
bugReporter.reportBug(bug);
foundAny = true;
}
if (nonNullParameter != null) {
for (Map.Entry<Integer, NullnessAnnotation> e : nonNullParameter.entrySet()) {
int i = e.getKey();
if (containsNullness(method.getParameterAnnotations(i), CHECK_FOR_NULL)) {
NullnessAnnotation a = e.getValue();
BugInstance bug = new BugInstance(CheckRelaxingNullnessAnnotation.this,
"NP_METHOD_PARAMETER_TIGHTENS_ANNOTATION", a.equals(NONNULL) ? HIGH_PRIORITY : NORMAL_PRIORITY);
bug.addClassAndMethod(xmethod);
LocalVariableAnnotation lva = null;
if (localVariables != null) {
for (LocalVariableNode lvn : localVariables) {
if (lvn.index == i + 1) {
lva = new LocalVariableAnnotation(lvn.name, i + 1, 0);
lva.setDescription(LocalVariableAnnotation.PARAMETER_NAMED_ROLE);
break;
}
}
}
if (lva == null) {
lva = new LocalVariableAnnotation("?", i + 1, 0);
lva.setDescription(LocalVariableAnnotation.PARAMETER_ROLE);
}
bug.add(lva);
bugReporter.reportBug(bug);
foundAny = true;
}
}
}
return foundAny;
}
}
private class HierarchyIterator {
private XClass superclass;
private Queue<ClassDescriptor> interfacesToVisit;
private final Set<ClassDescriptor> visited;
public HierarchyIterator(@Nonnull XClass xclass) {
interfacesToVisit = new LinkedList<>(Arrays.asList(xclass.getInterfaceDescriptorList()));
visited = new HashSet<>();
superclass = getClassInfo(xclass.getSuperclassDescriptor());
}
public XClass next() {
while (!interfacesToVisit.isEmpty()) {
ClassDescriptor interfaceDescr = interfacesToVisit.poll();
if (visited.add(interfaceDescr)) {
XClass xinterface = getClassInfo(interfaceDescr);
if (xinterface != null) {
interfacesToVisit.addAll(Arrays.asList(xinterface.getInterfaceDescriptorList()));
return xinterface;
}
}
}
// no interfaces => check super classes
if (superclass == null) {
return null;
}
XClass currentSuperclass = superclass;
// compute next one
superclass = getClassInfo(superclass.getSuperclassDescriptor());
if (superclass != null) {
interfacesToVisit = new LinkedList<>(Arrays.asList(superclass.getInterfaceDescriptorList()));
}
return currentSuperclass;
}
}
static boolean containsRelaxedNonNull(@CheckForNull List<AnnotationNode> methodAnnotations) {
if (methodAnnotations == null) {
return false;
}
for (AnnotationNode annotation : methodAnnotations) {
NullnessAnnotation nullness = getNullness(annotation.desc);
if (nullness == CHECK_FOR_NULL || nullness == NULLABLE) {
return true;
}
}
return false;
}
@CheckForNull
static Map<Integer, NullnessAnnotation> getNonnullOrNullableParams(@CheckForNull List<AnnotationNode>[] parameterAnnotations) {
if (parameterAnnotations == null) {
return null;
}
Map<Integer, NullnessAnnotation> nonNullParameter = new HashMap<>();
for (int i = 0; i < parameterAnnotations.length; i++) {
List<AnnotationNode> annotations = parameterAnnotations[i];
if (annotations == null) {
continue;
}
for (AnnotationNode annotation : annotations) {
NullnessAnnotation nullness = getNullness(annotation.desc);
if (nullness == null || nullness == CHECK_FOR_NULL) {
continue;
}
nonNullParameter.put(i, nullness);
}
}
return nonNullParameter;
}
@CheckForNull
static NullnessAnnotation getNullness(@SlashedClassName String annotationDesc) {
if (annotationDesc.length() < 2) {
return null;
}
// remove L; from signature
String substring = annotationDesc.substring(1, annotationDesc.length() - 1);
return Parser.parse(ClassName.toDottedClassName(substring));
}
static boolean containsNullness(Collection<AnnotationValue> annotations, NullnessAnnotation nullness) {
for (AnnotationValue annotation : annotations) {
NullnessAnnotation check = Parser.parse(annotation.getAnnotationClass().getDottedClassName());
if (check == nullness) {
return true;
}
}
return false;
}
static boolean compatibleParameters(String signature, String superSignature) {
SignatureParser sig = new SignatureParser(signature);
SignatureParser superSig = new SignatureParser(superSignature);
if (sig.getNumParameters() == superSig.getNumParameters()) {
Iterator<String> params = sig.parameterSignatureIterator();
Iterator<String> superParams = superSig.parameterSignatureIterator();
while (params.hasNext()) {
String param = params.next();
String superParam = superParams.next();
if (!areRelated(param, superParam)) {
return false;
}
}
String retSig = sig.getReturnTypeSignature();
String superRetSig = superSig.getReturnTypeSignature();
if (areRelated(retSig, superRetSig)) {
// it is compatible
return true;
}
}
return false;
}
static boolean areRelated(String sig, String superSig) {
try {
if (sig.equals(superSig)) {
return true;
}
if (sig.charAt(0) == 'L' && superSig.charAt(0) == 'L') {
sig = sig.substring(1, sig.length() - 1);
superSig = superSig.substring(1, superSig.length() - 1);
return Hierarchy.isSubtype(sig, superSig);
}
} catch (ClassNotFoundException e) {
AnalysisContext.reportMissingClass(e);
}
return false;
}
}