-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
UnnecessaryImportRule.java
406 lines (343 loc) · 17.2 KB
/
UnnecessaryImportRule.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.codestyle;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.sourceforge.pmd.lang.java.ast.ASTAmbiguousName;
import net.sourceforge.pmd.lang.java.ast.ASTClassType;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTImportDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTMethodCall;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLike;
import net.sourceforge.pmd.lang.java.ast.ASTVariableAccess;
import net.sourceforge.pmd.lang.java.ast.JavaComment;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.JavadocComment;
import net.sourceforge.pmd.lang.java.ast.internal.PrettyPrintingUtil;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
import net.sourceforge.pmd.lang.java.symbols.JAccessibleElementSymbol;
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
import net.sourceforge.pmd.lang.java.symbols.JExecutableSymbol;
import net.sourceforge.pmd.lang.java.symbols.JFieldSymbol;
import net.sourceforge.pmd.lang.java.symbols.JVariableSymbol;
import net.sourceforge.pmd.lang.java.symbols.table.ScopeInfo;
import net.sourceforge.pmd.lang.java.symbols.table.coreimpl.ShadowChainIterator;
import net.sourceforge.pmd.lang.java.types.JClassType;
import net.sourceforge.pmd.lang.java.types.JMethodSig;
import net.sourceforge.pmd.lang.java.types.JTypeMirror;
import net.sourceforge.pmd.lang.java.types.JVariableSig;
import net.sourceforge.pmd.lang.java.types.OverloadSelectionResult;
import net.sourceforge.pmd.lang.java.types.TypeSystem;
import net.sourceforge.pmd.lang.java.types.TypeTestUtil;
import net.sourceforge.pmd.util.CollectionUtil;
/**
* Detects unnecessary imports.
*
* <p>For PMD 7 I had hoped this rule could be rewritten to use the
* symbol table implementation directly instead of reimplementing a
* symbol table (with less care). This would be good for performance
* and correctness. Modifying the symbol table chain to track which
* import is used is hard though, mostly because the API to expose
* is unclear (we wouldn't want symbol tables to expose a mutable API).
*/
public class UnnecessaryImportRule extends AbstractJavaRule {
private static final String UNUSED_IMPORT_MESSAGE = "Unused import ''{0}''";
private static final String UNUSED_STATIC_IMPORT_MESSAGE = "Unused static import ''{0}''";
private static final String DUPLICATE_IMPORT_MESSAGE = "Duplicate import ''{0}''";
private static final String IMPORT_FROM_SAME_PACKAGE_MESSAGE = "Unnecessary import from the current package ''{0}''";
private static final String IMPORT_FROM_JAVA_LANG_MESSAGE = "Unnecessary import from the java.lang package ''{0}''";
private static final Logger LOG = LoggerFactory.getLogger(UnnecessaryImportRule.class);
private final Set<ImportWrapper> allSingleNameImports = new HashSet<>();
private final Set<ImportWrapper> allImportsOnDemand = new HashSet<>();
private final Set<ImportWrapper> unnecessaryJavaLangImports = new HashSet<>();
private final Set<ImportWrapper> unnecessaryImportsFromSamePackage = new HashSet<>();
/*
* Patterns to match the following constructs:
*
* @see package.class#member(param, param) label
* {@linkplain package.class#member(param, param) label}
* {@link package.class#member(param, param) label}
* {@link package.class#field}
* {@value package.class#field}
*
* @throws package.class label
* @exception package.class label
*/
/* package.class#member(param, param) */
private static final String TYPE_PART_GROUP = "((?:\\p{Alpha}\\w*\\.)*(?:\\p{Alpha}\\w*))?(?:#\\w*(?:\\(([.\\w\\s,\\[\\]]*)\\))?)?";
private static final Pattern SEE_PATTERN = Pattern.compile("@see\\s+" + TYPE_PART_GROUP);
private static final Pattern LINK_PATTERNS = Pattern.compile("\\{@link(?:plain)?\\s+" + TYPE_PART_GROUP + "[\\s\\}]");
private static final Pattern VALUE_PATTERN = Pattern.compile("\\{@value\\s+(\\p{Alpha}\\w*)[\\s#\\}]");
private static final Pattern THROWS_PATTERN = Pattern.compile("@throws\\s+(\\p{Alpha}\\w*)");
private static final Pattern EXCEPTION_PATTERN = Pattern.compile("@exception\\s+(\\p{Alpha}\\w*)");
/* // @link substring="a" target="package.class#member(param, param)" */
private static final Pattern LINK_IN_SNIPPET = Pattern
.compile("//\\s*@link\\s+(?:.*?)?target=[\"']?" + TYPE_PART_GROUP + "[\"']?");
private static final Pattern[] PATTERNS = { SEE_PATTERN, LINK_PATTERNS, VALUE_PATTERN, THROWS_PATTERN, EXCEPTION_PATTERN, LINK_IN_SNIPPET };
@Override
public Object visit(ASTCompilationUnit node, Object data) {
this.allSingleNameImports.clear();
this.allImportsOnDemand.clear();
this.unnecessaryJavaLangImports.clear();
this.unnecessaryImportsFromSamePackage.clear();
String packageName = node.getPackageName();
for (ASTImportDeclaration importDecl : node.children(ASTImportDeclaration.class)) {
visitImport(importDecl, data, packageName);
}
for (ImportWrapper wrapper : allSingleNameImports) {
if ("java.lang".equals(wrapper.node.getPackageName())) {
if (!isJavaLangImportNecessary(node, wrapper)) {
// the import is not shadowing something
unnecessaryJavaLangImports.add(wrapper);
}
}
}
super.visit(node, data);
visitComments(node);
doReporting(data);
return data;
}
private void doReporting(Object data) {
for (ImportWrapper wrapper : allSingleNameImports) {
String message = wrapper.isStatic() ? UNUSED_STATIC_IMPORT_MESSAGE : UNUSED_IMPORT_MESSAGE;
reportWithMessage(wrapper.node, data, message);
}
for (ImportWrapper wrapper : allImportsOnDemand) {
String message = wrapper.isStatic() ? UNUSED_STATIC_IMPORT_MESSAGE : UNUSED_IMPORT_MESSAGE;
reportWithMessage(wrapper.node, data, message);
}
// remove unused ones, they have already been reported
unnecessaryJavaLangImports.removeAll(allSingleNameImports);
unnecessaryJavaLangImports.removeAll(allImportsOnDemand);
unnecessaryImportsFromSamePackage.removeAll(allSingleNameImports);
unnecessaryImportsFromSamePackage.removeAll(allImportsOnDemand);
for (ImportWrapper wrapper : unnecessaryJavaLangImports) {
reportWithMessage(wrapper.node, data, IMPORT_FROM_JAVA_LANG_MESSAGE);
}
for (ImportWrapper wrapper : unnecessaryImportsFromSamePackage) {
reportWithMessage(wrapper.node, data, IMPORT_FROM_SAME_PACKAGE_MESSAGE);
}
}
private boolean isJavaLangImportNecessary(ASTCompilationUnit node, ImportWrapper wrapper) {
ShadowChainIterator<JTypeMirror, ScopeInfo> iter =
node.getSymbolTable().types().iterateResults(wrapper.node.getImportedSimpleName());
if (iter.hasNext()) {
iter.next();
if (iter.getScopeTag() == ScopeInfo.SINGLE_IMPORT) {
if (iter.hasNext()) {
iter.next();
// the import is shadowing something else
return iter.getScopeTag() != ScopeInfo.JAVA_LANG;
}
}
}
return false;
}
private void visitComments(ASTCompilationUnit node) {
// todo improve that when we have a javadoc parser
for (JavaComment comment : node.getComments()) {
if (!(comment instanceof JavadocComment)) {
continue;
}
for (Pattern p : PATTERNS) {
Matcher m = p.matcher(comment.getText());
while (m.find()) {
String fullname = m.group(1);
if (fullname != null) { // may be null for "@see #" and "@link #"
removeReferenceSingleImport(fullname);
}
if (m.groupCount() > 1) {
fullname = m.group(2);
if (fullname != null) {
for (String param : fullname.split("\\s*,\\s*")) {
removeReferenceSingleImport(param);
}
}
}
if (allSingleNameImports.isEmpty()) {
return;
}
}
}
}
}
private void visitImport(ASTImportDeclaration node, Object data, String thisPackageName) {
if (thisPackageName.equals(node.getPackageName())) {
unnecessaryImportsFromSamePackage.add(new ImportWrapper(node));
}
Set<ImportWrapper> container =
node.isImportOnDemand() ? allImportsOnDemand
: allSingleNameImports;
if (!container.add(new ImportWrapper(node))) {
// duplicate
reportWithMessage(node, data, DUPLICATE_IMPORT_MESSAGE);
}
}
private void reportWithMessage(ASTImportDeclaration node, Object data, String message) {
asCtx(data).addViolationWithMessage(node, message, PrettyPrintingUtil.prettyImport(node));
}
@Override
public Object visit(ASTClassType node, Object data) {
if (node.getQualifier() == null
&& !node.isFullyQualified()
&& node.getTypeMirror().isClassOrInterface()) {
JClassSymbol symbol = ((JClassType) node.getTypeMirror()).getSymbol();
ShadowChainIterator<JTypeMirror, ScopeInfo> scopeIter =
node.getSymbolTable().types().iterateResults(node.getSimpleName());
checkScopeChain(false, symbol, scopeIter, ts -> true, false);
}
return super.visit(node, data);
}
@Override
public Object visit(ASTAmbiguousName node, Object data) {
// ambiguous name means the symbol table could not resolve the first name
// only consider static imports
boolean onlyStatic = !(node.getParent() instanceof ASTClassType);
recordFailedTypeResWithName(node, node.getFirstToken().getImage(), onlyStatic);
return null;
}
private void recordFailedTypeResWithName(JavaNode location, String name, boolean onlyStatics) {
String target = onlyStatics ? "static " : "";
LOG.debug("UnnecessaryImport: Failed type res for {} will cause all {}imports named {} to be marked as used", location, target, name);
boolean foundNamedImport = allSingleNameImports.removeIf(
decl -> (!onlyStatics || decl.isStatic())
&& name.equals(decl.node.getImportedSimpleName()));
if (!foundNamedImport) {
LOG.debug("+ Since no such named import can be found, all {}on-demand-imports will be marked as used", target);
allImportsOnDemand.removeIf(it -> !onlyStatics || it.isStatic());
}
}
@Override
public Object visit(ASTMethodCall node, Object data) {
if (node.getQualifier() == null) {
OverloadSelectionResult overload = node.getOverloadSelectionInfo();
if (overload.isFailed()) {
// don't try further, but still visit all ASTClassType nodes in the AST.
recordFailedTypeResWithName(node, node.getMethodName(), true);
return super.visit(node, data); // todo we're erring towards FPs
}
ShadowChainIterator<JMethodSig, ScopeInfo> scopeIter =
node.getSymbolTable().methods().iterateResults(node.getMethodName());
JExecutableSymbol symbol = overload.getMethodType().getSymbol();
checkScopeChain(true,
symbol,
scopeIter,
methods -> CollectionUtil.any(methods, m -> m.getSymbol().equals(symbol)),
true);
}
return super.visit(node, data);
}
@Override
public Object visit(ASTVariableAccess node, Object data) {
JVariableSymbol sym = node.getReferencedSym();
if (sym != null
&& sym.isField()
&& ((JFieldSymbol) sym).isStatic()) {
if (node.getParent() instanceof ASTSwitchLabel
&& node.ancestors(ASTSwitchLike.class).take(1).any(ASTSwitchLike::isEnumSwitch)) {
// special scoping rules, see JSymbolTable#variables doc
return null;
}
ShadowChainIterator<JVariableSig, ScopeInfo> scopeIter = node.getSymbolTable().variables().iterateResults(node.getName());
checkScopeChain(false, (JFieldSymbol) sym, scopeIter, ts -> true, true);
}
if (sym == null) {
recordFailedTypeResWithName(node, node.getName(), true);
}
return null;
}
private <T> void checkScopeChain(boolean recursive,
JAccessibleElementSymbol symbol,
ShadowChainIterator<T, ScopeInfo> scopeIter,
Predicate<List<T>> containsTarget,
boolean onlyStatic) {
while (scopeIter.hasNext()) {
scopeIter.next();
// must be the first result
// todo make sure new Outer().new Inner() does not mark Inner as used
if (containsTarget.test(scopeIter.getResults())) {
// We found the declaration bringing the symbol in scope
// If it's an import, then it's used. However, maybe it's from java.lang.
if (scopeIter.getScopeTag() == ScopeInfo.SINGLE_IMPORT) {
allSingleNameImports.removeIf(
it -> (it.isStatic() || !onlyStatic)
&& symbol.getSimpleName().equals(it.node.getImportedSimpleName())
);
} else if (scopeIter.getScopeTag() == ScopeInfo.IMPORT_ON_DEMAND) {
allImportsOnDemand.removeIf(it -> {
if (!it.isStatic() && onlyStatic) {
return false;
}
// This is the class that contains the symbol
// we're looking for.
// We have to test whether this symbol is contained
// by the imported type or package.
JClassSymbol symbolOwner = symbol.getEnclosingClass();
if (symbolOwner == null) {
// package import on demand
return it.node.getImportedName().equals(symbol.getPackageName());
} else {
if (it.node.getImportedName().equals(symbolOwner.getCanonicalName())) {
// importing the container directly
return it.isStatic() == symbol.isStatic();
}
// maybe we're importing a subclass of the container.
TypeSystem ts = symbolOwner.getTypeSystem();
JClassSymbol importedContainer = ts.getClassSymbol(it.node.getImportedName());
return importedContainer == null // insufficient classpath, err towards FNs
|| TypeTestUtil.isA(ts.rawType(symbolOwner), ts.rawType(importedContainer));
}
});
}
return;
}
if (!recursive) {
break;
}
}
// unknown reference
}
/** We found a reference to the type given by the name. */
private void removeReferenceSingleImport(String referenceName) {
String expectedImport = StringUtils.substringBefore(referenceName, ".");
allSingleNameImports.removeIf(it -> expectedImport.equals(it.node.getImportedSimpleName()));
}
/** Override the equal behaviour of ASTImportDeclaration to put it into a set. */
private static final class ImportWrapper {
private final ASTImportDeclaration node;
private ImportWrapper(ASTImportDeclaration node) {
this.node = node;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (getClass() != o.getClass()) {
return false;
}
ImportWrapper that = (ImportWrapper) o;
return node.getImportedName().equals(that.node.getImportedName())
&& node.isImportOnDemand() == that.node.isImportOnDemand()
&& this.isStatic() == that.isStatic();
}
@Override
public int hashCode() {
return node.getImportedName().hashCode() * 31
+ Boolean.hashCode(node.isStatic())
+ 37 * Boolean.hashCode(node.isImportOnDemand());
}
private boolean isStatic() {
return this.node.isStatic();
}
}
}