/
LazyArgEvaluationCheck.java
380 lines (328 loc) · 12.1 KB
/
LazyArgEvaluationCheck.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* SonarQube Java
* Copyright (C) 2012-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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 3 of the License, or (at your option) any later version.
*
* This program 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 program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.java.checks;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Stream;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.JavaFileScanner;
import org.sonar.plugins.java.api.JavaFileScannerContext;
import org.sonar.plugins.java.api.semantic.MethodMatchers;
import org.sonar.plugins.java.api.semantic.Symbol;
import org.sonar.plugins.java.api.semantic.Type;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.BinaryExpressionTree;
import org.sonar.plugins.java.api.tree.CatchTree;
import org.sonar.plugins.java.api.tree.ExpressionTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.IfStatementTree;
import org.sonar.plugins.java.api.tree.MemberSelectExpressionTree;
import org.sonar.plugins.java.api.tree.MethodInvocationTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.NewClassTree;
import org.sonar.plugins.java.api.tree.Tree;
import static org.sonar.plugins.java.api.semantic.MethodMatchers.ANY;
@Rule(key = "S2629")
public class LazyArgEvaluationCheck extends BaseTreeVisitor implements JavaFileScanner {
private static final String STRING = "java.lang.String";
private static final String OBJECT_ARR = "java.lang.Object[]";
private static class SLF4J {
private static final String[] METHOD_NAMES = {
"trace",
"debug",
"info",
"warn",
"error"
};
private static final String LOGGER = "org.slf4j.Logger";
private static final String MARKER = "org.slf4j.Marker";
private static final MethodMatchers LOG = MethodMatchers.create()
.ofSubTypes(LOGGER)
.names(METHOD_NAMES)
.addParametersMatcher(STRING)
.addParametersMatcher(STRING, ANY)
.addParametersMatcher(STRING, ANY, ANY)
.addParametersMatcher(STRING, OBJECT_ARR)
.addParametersMatcher(MARKER, STRING)
.addParametersMatcher(MARKER, STRING, ANY)
.addParametersMatcher(MARKER, STRING, ANY, ANY)
.addParametersMatcher(MARKER, STRING, OBJECT_ARR)
.build();
private static final MethodMatchers TEST = MethodMatchers.create()
.ofSubTypes(LOGGER)
.names(testMethodNames(METHOD_NAMES))
.addWithoutParametersMatcher()
.build();
}
private static class JUL {
private static final String[] METHOD_NAMES = {
"severe",
"warning",
"info",
"config",
"fine",
"finer",
"finest"
};
private static final String LOGGER = "java.util.logging.Logger";
private static final MethodMatchers LOG = MethodMatchers.or(
MethodMatchers.create()
.ofTypes(LOGGER)
.names(METHOD_NAMES)
.addParametersMatcher(STRING)
.build(),
MethodMatchers.create()
.ofTypes(LOGGER)
.names("log")
.addParametersMatcher("java.util.logging.Level", STRING)
.build());
private static final MethodMatchers TEST = MethodMatchers.create()
.ofTypes(LOGGER)
.names("isLoggable")
.addParametersMatcher("java.util.logging.Level")
.build();
}
private static class LOG4J {
private static final String[] METHOD_NAMES = {
"debug",
"error",
"fatal",
"info",
"trace",
"warn"
};
private static final String LEVEL = "org.apache.logging.log4j.Level";
private static final String LOGGER = "org.apache.logging.log4j.Logger";
private static final String MARKER = "org.apache.logging.log4j.Marker";
private static final Predicate<Type> SUPPLIER = type -> type.isSubtypeOf("org.apache.logging.log4j.util.Supplier") ||
type.isSubtypeOf("org.apache.logging.log4j.util.MessageSupplier");
private static final MethodMatchers LOG = MethodMatchers.or(
MethodMatchers.create()
.ofSubTypes(LOGGER)
.names(METHOD_NAMES)
.withAnyParameters()
.build(),
MethodMatchers.create()
.ofSubTypes(LOGGER)
.names("log")
.withAnyParameters()
.build());
private static final MethodMatchers TEST = MethodMatchers.or(
MethodMatchers.create()
.ofSubTypes(LOGGER)
.names(testMethodNames(METHOD_NAMES))
.withAnyParameters()
.build(),
MethodMatchers.create()
.ofSubTypes(LOGGER)
.names("isEnabled")
.addParametersMatcher(LEVEL)
.addParametersMatcher(LEVEL, MARKER)
.build());
}
private static final MethodMatchers PRECONDITIONS = MethodMatchers.create()
.ofTypes("com.google.common.base.Preconditions")
.names("checkState")
.withAnyParameters()
.build();
private static final MethodMatchers LAZY_ARG_METHODS = MethodMatchers.or(
PRECONDITIONS,
SLF4J.LOG,
JUL.LOG,
LOG4J.LOG);
private static final MethodMatchers LOG_LEVEL_TESTS = MethodMatchers.or(
SLF4J.TEST,
JUL.TEST,
LOG4J.TEST);
private static String[] testMethodNames(String[] lowerCaseNames) {
return Stream.of(lowerCaseNames)
.map(name -> "is" + Character.toUpperCase(name.charAt(0)) + name.substring(1) + "Enabled")
.toArray(String[]::new);
}
private JavaFileScannerContext context;
private Deque<Tree> treeStack = new ArrayDeque<>();
@Override
public void scanFile(JavaFileScannerContext context) {
this.context = context;
if (context.getSemanticModel() == null) {
return;
}
scan(context.getTree());
}
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
if (LAZY_ARG_METHODS.matches(tree) && !insideCatchStatement() && !insideLevelTest() && !argsUsingSuppliers(tree)) {
onMethodInvocationFound(tree);
}
}
private static boolean argsUsingSuppliers(MethodInvocationTree tree) {
return tree.arguments().stream().map(ExpressionTree::symbolType).anyMatch(LOG4J.SUPPLIER);
}
@Override
public void visitIfStatement(IfStatementTree ifTree) {
LevelTestVisitor levelTestVisitor = new LevelTestVisitor();
ifTree.condition().accept(levelTestVisitor);
if (levelTestVisitor.match) {
stackAndContinue(ifTree, super::visitIfStatement);
} else {
super.visitIfStatement(ifTree);
}
}
@Override
public void visitCatch(CatchTree tree) {
stackAndContinue(tree, super::visitCatch);
}
@Override
public void visitMethod(MethodTree tree) {
// we put method trees on stack to be able to detect log statements in anonymous classes
stackAndContinue(tree, super::visitMethod);
}
private boolean insideLevelTest() {
return treeStack.stream().anyMatch(t -> t.is(Tree.Kind.IF_STATEMENT));
}
private boolean insideCatchStatement() {
return treeStack.peek() != null && treeStack.peek().is(Tree.Kind.CATCH);
}
private <T extends Tree> void stackAndContinue(T tree, Consumer<T> visit) {
treeStack.push(tree);
visit.accept(tree);
treeStack.pop();
}
private void onMethodInvocationFound(MethodInvocationTree mit) {
List<JavaFileScannerContext.Location> flow = findStringArg(mit)
.flatMap(LazyArgEvaluationCheck::checkArgument)
.toList();
if (!flow.isEmpty()) {
context.reportIssue(this, flow.get(0).syntaxNode, flow.get(0).msg, flow.subList(1, flow.size()), null);
}
}
private static Stream<JavaFileScannerContext.Location> checkArgument(ExpressionTree stringArgument) {
StringExpressionVisitor visitor = new StringExpressionVisitor();
stringArgument.accept(visitor);
if (visitor.shouldReport) {
return Stream.of(locationFromArg(stringArgument, visitor));
} else {
return Stream.empty();
}
}
private static JavaFileScannerContext.Location locationFromArg(ExpressionTree stringArgument, StringExpressionVisitor visitor) {
StringBuilder msg = new StringBuilder();
if (visitor.hasMethodInvocation) {
msg.append("Invoke method(s) only conditionally. ");
}
if (visitor.hasBinaryExpression) {
msg.append("Use the built-in formatting to construct this argument.");
}
return new JavaFileScannerContext.Location(msg.toString(), stringArgument);
}
private static Stream<ExpressionTree> findStringArg(MethodInvocationTree mit) {
return mit.arguments().stream()
.filter(arg -> arg.symbolType().is(STRING));
}
private static class StringExpressionVisitor extends BaseTreeVisitor {
private boolean hasBinaryExpression;
private boolean shouldReport;
private boolean hasMethodInvocation;
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
if (!isGetter(tree)) {
shouldReport = true;
hasMethodInvocation = true;
}
}
private static boolean isGetter(MethodInvocationTree tree) {
if (isAnnotationMethod(tree)) {
return true;
}
String methodName = tree.methodSymbol().name();
return tree.methodSymbol().parameterTypes().isEmpty()
&& (methodName.startsWith("get") || methodName.startsWith("is") || isGetterMatchingFieldNameAndType(tree.methodSymbol()));
}
private static boolean isGetterMatchingFieldNameAndType(Symbol.MethodSymbol methodSymbol) {
Symbol owner = methodSymbol.owner();
Type getterReturnType = methodSymbol.returnType().type();
// methodSymbol's owner is always not null and TypeSymbol
return ((Symbol.TypeSymbol) owner).memberSymbols()
.stream()
.filter(symbol -> symbol.isVariableSymbol() && symbol.type().equals(getterReturnType))
.anyMatch(symbol -> symbol.name().equals(methodSymbol.name()));
}
private static boolean isAnnotationMethod(MethodInvocationTree tree) {
Symbol owner = tree.methodSymbol().owner();
return owner.isTypeSymbol() && ((Symbol.TypeSymbol) owner).isAnnotation();
}
@Override
public void visitIdentifier(IdentifierTree tree) {
if (hasBinaryExpression) {
shouldReport = true;
}
}
@Override
public void visitNewClass(NewClassTree tree) {
hasMethodInvocation = true;
shouldReport = true;
}
@Override
public void visitBinaryExpression(BinaryExpressionTree tree) {
hasBinaryExpression = true;
if (!isConstant(tree.rightOperand())) {
tree.rightOperand().accept(this);
}
if (!isConstant(tree.leftOperand())) {
tree.leftOperand().accept(this);
}
}
private static boolean isConstant(ExpressionTree operand) {
switch (operand.kind()) {
case BOOLEAN_LITERAL:
case CHAR_LITERAL:
case DOUBLE_LITERAL:
case FLOAT_LITERAL:
case INT_LITERAL:
case LONG_LITERAL:
case STRING_LITERAL:
case NULL_LITERAL:
return true;
case IDENTIFIER:
return isConstant(((IdentifierTree) operand).symbol());
case MEMBER_SELECT:
MemberSelectExpressionTree mset = (MemberSelectExpressionTree) operand;
return isConstant(mset.identifier().symbol());
default:
return false;
}
}
private static boolean isConstant(Symbol symbol) {
return symbol.isStatic() && symbol.isFinal();
}
}
private static class LevelTestVisitor extends BaseTreeVisitor {
boolean match = false;
@Override
public void visitMethodInvocation(MethodInvocationTree mit) {
if (LOG_LEVEL_TESTS.matches(mit)) {
match = true;
}
}
}
}