Repository navigation
Expand file tree
/
Copy pathPrematureDeclarationRule.java
More file actions
107 lines (91 loc) · 4.3 KB
/
Copy pathPrematureDeclarationRule.java
File metadata and controls
107 lines (91 loc) · 4.3 KB
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
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.java.rule.codestyle;
import static java.util.Collections.emptySet;
import static net.sourceforge.pmd.lang.ast.NodeStream.asInstanceOf;
import java.util.Set;
import java.util.stream.Collectors;
import net.sourceforge.pmd.lang.ast.NodeStream;
import net.sourceforge.pmd.lang.java.ast.ASTAssignableExpr.ASTNamedReferenceExpr;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTForInit;
import net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTResource;
import net.sourceforge.pmd.lang.java.ast.ASTReturnStatement;
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
import net.sourceforge.pmd.lang.java.ast.ASTThrowStatement;
import net.sourceforge.pmd.lang.java.ast.ASTVariableId;
import net.sourceforge.pmd.lang.java.ast.internal.JavaAstUtils;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.lang.java.rule.internal.JavaRuleUtil;
import net.sourceforge.pmd.lang.java.symbols.JVariableSymbol;
import net.sourceforge.pmd.reporting.RuleContext;
/**
* Checks for variables in methods that are defined before they are really
* needed. A reference is deemed to be premature if it is created ahead of a
* block of code that doesn't use it that also has the ability to return or
* throw an exception.
*
* @author Brian Remedios
*/
public class PrematureDeclarationRule extends AbstractJavaRulechainRule {
public PrematureDeclarationRule() {
super(ASTLocalVariableDeclaration.class);
}
@Override
public Object visit(ASTLocalVariableDeclaration node, Object data) {
RuleContext ctx = (RuleContext) data;
if (node.getParent() instanceof ASTForInit
|| node.getParent() instanceof ASTResource) {
// those don't count
return null;
}
for (ASTVariableId id : node) {
ASTExpression initializer = id.getInitializer();
if (JavaAstUtils.isNeverUsed(id) // avoid the duplicate with unused variables
|| JavaRuleUtil.cannotBeMoved(initializer)
|| JavaRuleUtil.hasSideEffect(initializer, emptySet())) {
continue;
}
Set<JVariableSymbol> refsInInitializer = getReferencedVars(initializer);
// If there's no initializer, or the initializer doesn't depend on anything (eg, a literal),
// then we don't care about side-effects
boolean hasStatefulInitializer = !refsInInitializer.isEmpty() || JavaRuleUtil.hasSideEffect(initializer, emptySet());
for (ASTStatement stmt : statementsAfter(node)) {
if (JavaRuleUtil.hasReferencesIn(stmt, id)
|| hasStatefulInitializer && JavaRuleUtil.hasSideEffect(stmt, refsInInitializer)) {
break;
}
if (hasExit(stmt)) {
ctx.addViolation(node, id.getName());
break;
}
}
}
return null;
}
/**
* Returns the set of local variables referenced inside the expression.
*/
private static Set<JVariableSymbol> getReferencedVars(ASTExpression term) {
return term == null ? emptySet()
: term.descendantsOrSelf()
.filterIs(ASTNamedReferenceExpr.class)
.filter(it -> it.getReferencedSym() != null)
.collect(Collectors.mapping(ASTNamedReferenceExpr::getReferencedSym, Collectors.toSet()));
}
/**
* Returns whether the block contains a return call or throws an exception.
* Exclude blocks that have these things as part of an inner class.
*/
private static boolean hasExit(ASTStatement block) {
return block.descendants()
.map(asInstanceOf(ASTThrowStatement.class, ASTReturnStatement.class))
.nonEmpty();
}
/** Returns all the statements following the given local var declaration. */
private static NodeStream<ASTStatement> statementsAfter(ASTLocalVariableDeclaration node) {
return node.asStream().followingSiblings().filterIs(ASTStatement.class);
}
}