forked from uber/NullAway
/
ErrorBuilder.java
315 lines (296 loc) · 12.8 KB
/
ErrorBuilder.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
/*
* Copyright (c) 2019 Uber Technologies, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.uber.nullaway;
import static com.uber.nullaway.ErrorMessage.MessageTypes.FIELD_NO_INIT;
import static com.uber.nullaway.ErrorMessage.MessageTypes.METHOD_NO_INIT;
import static com.uber.nullaway.NullAway.INITIALIZATION_CHECK_NAME;
import static com.uber.nullaway.NullAway.getTreesInstance;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.errorprone.VisitorState;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.ModifiersTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Symbol;
import java.util.List;
import java.util.Set;
import java.util.stream.StreamSupport;
import javax.annotation.Nullable;
import javax.lang.model.element.Element;
/** A class to construct error message to be displayed after the analysis finds error. */
public class ErrorBuilder {
private final Config config;
/** Checker name that can be used to suppress the warnings. */
private final String suppressionName;
/** Additional identifiers for this check, to be checked for in @SuppressWarnings annotations. */
private final Set<String> allNames;
ErrorBuilder(Config config, String suppressionName, Set<String> allNames) {
this.config = config;
this.suppressionName = suppressionName;
this.allNames = allNames;
}
/**
* create an error description for a nullability warning
*
* @param errorMessage the error message object.
* @param path the TreePath to the error location. Used to compute a suggested fix at the
* enclosing method for the error location
* @param descriptionBuilder the description builder for the error.
* @return the error description
*/
Description createErrorDescription(
ErrorMessage errorMessage, TreePath path, Description.Builder descriptionBuilder) {
Tree enclosingSuppressTree = suppressibleNode(path);
return createErrorDescription(errorMessage, enclosingSuppressTree, descriptionBuilder);
}
/**
* create an error description for a nullability warning
*
* @param errorMessage the error message object.
* @param suggestTree the location at which a fix suggestion should be made
* @param descriptionBuilder the description builder for the error.
* @return the error description
*/
public Description createErrorDescription(
ErrorMessage errorMessage,
@Nullable Tree suggestTree,
Description.Builder descriptionBuilder) {
Description.Builder builder = descriptionBuilder.setMessage(errorMessage.message);
if (config.suggestSuppressions() && suggestTree != null) {
switch (errorMessage.messageType) {
case DEREFERENCE_NULLABLE:
case RETURN_NULLABLE:
case PASS_NULLABLE:
case ASSIGN_FIELD_NULLABLE:
case SWITCH_EXPRESSION_NULLABLE:
if (config.getCastToNonNullMethod() != null) {
builder = addCastToNonNullFix(suggestTree, builder);
} else {
builder = addSuppressWarningsFix(suggestTree, builder, suppressionName);
}
break;
case CAST_TO_NONNULL_ARG_NONNULL:
builder = removeCastToNonNullFix(suggestTree, builder);
break;
case WRONG_OVERRIDE_RETURN:
builder = addSuppressWarningsFix(suggestTree, builder, suppressionName);
break;
case WRONG_OVERRIDE_PARAM:
builder = addSuppressWarningsFix(suggestTree, builder, suppressionName);
break;
case METHOD_NO_INIT:
case FIELD_NO_INIT:
builder = addSuppressWarningsFix(suggestTree, builder, INITIALIZATION_CHECK_NAME);
break;
case ANNOTATION_VALUE_INVALID:
break;
default:
builder = addSuppressWarningsFix(suggestTree, builder, suppressionName);
}
}
// #letbuildersbuild
return builder.build();
}
/**
* create an error description for a generalized @Nullable value to @NonNull location assignment.
*
* <p>This includes: field assignments, method arguments and method returns
*
* @param errorMessage the error message object.
* @param suggestTreeIfCastToNonNull the location at which a fix suggestion should be made if a
* castToNonNull method is available (usually the expression to cast)
* @param suggestTreePathIfSuppression the location at which a fix suggestion should be made if a
* castToNonNull method is not available (usually the enclosing method, or any place
* where @SuppressWarnings can be added).
* @param descriptionBuilder the description builder for the error.
* @return the error description.
*/
Description createErrorDescriptionForNullAssignment(
ErrorMessage errorMessage,
@Nullable Tree suggestTreeIfCastToNonNull,
@Nullable TreePath suggestTreePathIfSuppression,
Description.Builder descriptionBuilder) {
final Tree enclosingSuppressTree = suppressibleNode(suggestTreePathIfSuppression);
if (config.getCastToNonNullMethod() != null) {
return createErrorDescription(errorMessage, suggestTreeIfCastToNonNull, descriptionBuilder);
} else {
return createErrorDescription(errorMessage, enclosingSuppressTree, descriptionBuilder);
}
}
Description.Builder addSuppressWarningsFix(
Tree suggestTree, Description.Builder builder, String suppressionName) {
SuppressWarnings extantSuppressWarnings =
ASTHelpers.getAnnotation(suggestTree, SuppressWarnings.class);
SuggestedFix fix;
if (extantSuppressWarnings == null) {
fix =
SuggestedFix.prefixWith(
suggestTree,
"@SuppressWarnings(\""
+ suppressionName
+ "\") "
+ config.getAutofixSuppressionComment());
} else {
// need to update the existing list of warnings
final List<String> suppressions = Lists.newArrayList(extantSuppressWarnings.value());
suppressions.add(suppressionName);
// find the existing annotation, so we can replace it
final ModifiersTree modifiers =
(suggestTree instanceof MethodTree)
? ((MethodTree) suggestTree).getModifiers()
: ((VariableTree) suggestTree).getModifiers();
final List<? extends AnnotationTree> annotations = modifiers.getAnnotations();
// noinspection ConstantConditions
com.google.common.base.Optional<? extends AnnotationTree> suppressWarningsAnnot =
Iterables.tryFind(
annotations,
annot -> annot.getAnnotationType().toString().endsWith("SuppressWarnings"));
if (!suppressWarningsAnnot.isPresent()) {
throw new AssertionError("something went horribly wrong");
}
final String replacement =
"@SuppressWarnings({"
+ Joiner.on(',').join(Iterables.transform(suppressions, s -> '"' + s + '"'))
+ "}) "
+ config.getAutofixSuppressionComment();
fix = SuggestedFix.replace(suppressWarningsAnnot.get(), replacement);
}
return builder.addFix(fix);
}
/**
* Adapted from {@link com.google.errorprone.fixes.SuggestedFixes}.
*
* <p>TODO: actually use {@link
* com.google.errorprone.fixes.SuggestedFixes#addSuppressWarnings(VisitorState, String)} instead
*/
@Nullable
private Tree suppressibleNode(@Nullable TreePath path) {
if (path == null) {
return null;
}
return StreamSupport.stream(path.spliterator(), false)
.filter(
tree ->
tree instanceof MethodTree
|| (tree instanceof ClassTree
&& ((ClassTree) tree).getSimpleName().length() != 0)
|| tree instanceof VariableTree)
.findFirst()
.orElse(null);
}
private Description.Builder addCastToNonNullFix(Tree suggestTree, Description.Builder builder) {
final String fullMethodName = config.getCastToNonNullMethod();
assert fullMethodName != null;
// Add a call to castToNonNull around suggestTree:
final String[] parts = fullMethodName.split("\\.");
final String shortMethodName = parts[parts.length - 1];
final String replacement = shortMethodName + "(" + suggestTree.toString() + ")";
final SuggestedFix fix =
SuggestedFix.builder()
.replace(suggestTree, replacement)
.addStaticImport(fullMethodName) // ensure castToNonNull static import
.build();
return builder.addFix(fix);
}
private Description.Builder removeCastToNonNullFix(
Tree suggestTree, Description.Builder builder) {
assert suggestTree.getKind() == Tree.Kind.METHOD_INVOCATION;
final MethodInvocationTree invTree = (MethodInvocationTree) suggestTree;
final Symbol.MethodSymbol methodSymbol = ASTHelpers.getSymbol(invTree);
final String qualifiedName =
ASTHelpers.enclosingClass(methodSymbol) + "." + methodSymbol.getSimpleName().toString();
if (!qualifiedName.equals(config.getCastToNonNullMethod())) {
throw new RuntimeException("suggestTree should point to the castToNonNull invocation.");
}
// Remove the call to castToNonNull:
final SuggestedFix fix =
SuggestedFix.builder()
.replace(suggestTree, invTree.getArguments().get(0).toString())
.build();
return builder.addFix(fix);
}
void reportInitializerError(
Symbol.MethodSymbol methodSymbol,
String message,
VisitorState state,
Description.Builder descriptionBuilder) {
if (symbolHasSuppressInitializationWarningsAnnotation(methodSymbol)) {
return;
}
Tree methodTree = getTreesInstance(state).getTree(methodSymbol);
state.reportMatch(
createErrorDescription(
new ErrorMessage(METHOD_NO_INIT, message), methodTree, descriptionBuilder));
}
boolean symbolHasSuppressInitializationWarningsAnnotation(Symbol symbol) {
SuppressWarnings annotation = symbol.getAnnotation(SuppressWarnings.class);
if (annotation != null) {
for (String s : annotation.value()) {
// we need to check for standard suppression here also since we may report initialization
// errors outside the normal ErrorProne match* methods
if (s.equals(INITIALIZATION_CHECK_NAME) || allNames.stream().anyMatch(s::equals)) {
return true;
}
}
}
return false;
}
static String errMsgForInitializer(Set<Element> uninitFields) {
String message = "initializer method does not guarantee @NonNull ";
if (uninitFields.size() == 1) {
message += "field " + uninitFields.iterator().next().toString() + " is initialized";
} else {
message += "fields " + Joiner.on(", ").join(uninitFields) + " are initialized";
}
message += " along all control-flow paths (remember to check for exceptions or early returns).";
return message;
}
void reportInitErrorOnField(Symbol symbol, VisitorState state, Description.Builder builder) {
if (symbolHasSuppressInitializationWarningsAnnotation(symbol)) {
return;
}
Tree tree = getTreesInstance(state).getTree(symbol);
if (symbol.isStatic()) {
state.reportMatch(
createErrorDescription(
new ErrorMessage(
FIELD_NO_INIT, "@NonNull static field " + symbol + " not initialized"),
tree,
builder));
} else {
state.reportMatch(
createErrorDescription(
new ErrorMessage(FIELD_NO_INIT, "@NonNull field " + symbol + " not initialized"),
tree,
builder));
}
}
}