Clover coverage report - PMD - 3.7 Coverage timestamp: Wed May 31 2006 09:25:59 EDT FRAMES NO FRAMES |
file stats: | LOC: | 120 |  | Methods: | 4
| NCLOC: | 84 |  | Classes: | 1
Source file | Conditionals | Statements | Methods | TOTAL
AbstractOptimizationRule.java | 65.6% | 84.1% | 100% | 77.5% |

1 |  | /*
2 |  | * Created on Jan 11, 2005
3 |  | *
4 |  | * $Id: AbstractOptimizationRule.java,v 1.7 2006/02/10 14:26:31 tomcopeland Exp $
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 |  | * Base class with utility methods for optimization rules
25 |  | *
26 |  | * @author mgriffa
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 |  | // TODO - symbol table?
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 |  | * Check constructions like
47 |  | * int i;
48 |  | * ++i;
49 |  | * --i;
50 |  | * i++;
51 |  | * i*=1;
52 |  | * i+=1;
53 |  | */
54 | 6 | private final boolean numericWithPrePost(ASTMethodDeclaration md, String varName) {
55 |  | // ++i
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 |  | // --i
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 |  | // if node is assigned return true
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 |  | }