|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| package net.sourceforge.pmd.rules.optimization; |
|
7 |
| |
|
8 |
| import net.sourceforge.pmd.AbstractRule; |
|
9 |
| import net.sourceforge.pmd.Rule; |
|
10 |
| import net.sourceforge.pmd.ast.ASTAssignmentOperator; |
|
11 |
| import net.sourceforge.pmd.ast.ASTLocalVariableDeclaration; |
|
12 |
| import net.sourceforge.pmd.ast.ASTMethodDeclaration; |
|
13 |
| import net.sourceforge.pmd.ast.ASTName; |
|
14 |
| import net.sourceforge.pmd.ast.ASTPostfixExpression; |
|
15 |
| import net.sourceforge.pmd.ast.ASTPreDecrementExpression; |
|
16 |
| import net.sourceforge.pmd.ast.ASTPreIncrementExpression; |
|
17 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
|
18 |
| import net.sourceforge.pmd.ast.SimpleNode; |
|
19 |
| |
|
20 |
| import java.util.Iterator; |
|
21 |
| import java.util.List; |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| public class AbstractOptimizationRule extends AbstractRule implements Rule { |
|
29 |
| |
|
30 |
11
| protected final boolean isVarWritterInMethod(String varName, ASTMethodDeclaration md) {
|
|
31 |
11
| List assignments = md.findChildrenOfType(ASTAssignmentOperator.class);
|
|
32 |
11
| return (variableAssigned(varName, assignments) || numericWithPrePost(md, varName));
|
|
33 |
| } |
|
34 |
| |
|
35 |
| |
|
36 |
11
| protected final String getVarName(ASTLocalVariableDeclaration node) {
|
|
37 |
11
| List l = node.findChildrenOfType(ASTVariableDeclaratorId.class);
|
|
38 |
11
| if (l != null && l.size() > 0) {
|
|
39 |
11
| ASTVariableDeclaratorId vd = (ASTVariableDeclaratorId) l.get(0);
|
|
40 |
11
| return vd.getImage();
|
|
41 |
| } |
|
42 |
0
| return null;
|
|
43 |
| } |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
6
| private final boolean numericWithPrePost(ASTMethodDeclaration md, String varName) {
|
|
55 |
| |
|
56 |
6
| List preinc = md.findChildrenOfType(ASTPreIncrementExpression.class);
|
|
57 |
6
| if (preinc != null && !preinc.isEmpty()) {
|
|
58 |
1
| for (Iterator it = preinc.iterator(); it.hasNext();) {
|
|
59 |
1
| ASTPreIncrementExpression ie = (ASTPreIncrementExpression) it.next();
|
|
60 |
1
| if (((ASTName) ie.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0)).getImage().equals(varName)) {
|
|
61 |
1
| return true;
|
|
62 |
| } |
|
63 |
| } |
|
64 |
| } |
|
65 |
| |
|
66 |
| |
|
67 |
5
| List predec = md.findChildrenOfType(ASTPreDecrementExpression.class);
|
|
68 |
5
| if (predec != null && !predec.isEmpty()) {
|
|
69 |
0
| for (Iterator it = predec.iterator(); it.hasNext();) {
|
|
70 |
0
| ASTPreDecrementExpression de = (ASTPreDecrementExpression) it.next();
|
|
71 |
0
| if (((ASTName) de.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0)).getImage().equals(varName)) {
|
|
72 |
0
| return true;
|
|
73 |
| } |
|
74 |
| } |
|
75 |
| } |
|
76 |
| |
|
77 |
5
| List pf = md.findChildrenOfType(ASTPostfixExpression.class);
|
|
78 |
5
| if (pf != null && !pf.isEmpty()) {
|
|
79 |
2
| for (Iterator it = pf.iterator(); it.hasNext();) {
|
|
80 |
2
| ASTPostfixExpression pe = (ASTPostfixExpression) it.next();
|
|
81 |
| |
|
82 |
2
| if ((pe.getImage().equals("++") || pe.getImage().equals("--"))) {
|
|
83 |
2
| SimpleNode first = (SimpleNode) pe.jjtGetChild(0);
|
|
84 |
2
| SimpleNode second = (SimpleNode) first.jjtGetChild(0);
|
|
85 |
2
| if (second.jjtGetNumChildren() == 0) {
|
|
86 |
0
| continue;
|
|
87 |
| } |
|
88 |
2
| ASTName name = (ASTName) second.jjtGetChild(0);
|
|
89 |
2
| if (name.getImage().equals(varName)) {
|
|
90 |
1
| return true;
|
|
91 |
| } |
|
92 |
| } |
|
93 |
| } |
|
94 |
| } |
|
95 |
4
| return false;
|
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
11
| private final boolean variableAssigned(final String varName, final List assignments) {
|
|
100 |
11
| if (assignments == null || assignments.isEmpty()) {
|
|
101 |
3
| return false;
|
|
102 |
| } |
|
103 |
8
| for (Iterator it = assignments.iterator(); it.hasNext();) {
|
|
104 |
8
| final ASTAssignmentOperator a = (ASTAssignmentOperator) it.next();
|
|
105 |
| |
|
106 |
8
| SimpleNode firstChild = (SimpleNode) a.jjtGetParent().jjtGetChild(0);
|
|
107 |
8
| SimpleNode otherChild = (SimpleNode) firstChild.jjtGetChild(0);
|
|
108 |
8
| if (otherChild.jjtGetNumChildren() == 0 || !(otherChild.jjtGetChild(0) instanceof ASTName)) {
|
|
109 |
0
| continue;
|
|
110 |
| } |
|
111 |
8
| ASTName n = (ASTName) otherChild.jjtGetChild(0);
|
|
112 |
8
| if (n.getImage().equals(varName)) {
|
|
113 |
5
| return true;
|
|
114 |
| } |
|
115 |
| } |
|
116 |
| |
|
117 |
3
| return false;
|
|
118 |
| } |
|
119 |
| |
|
120 |
| } |