01: /**
02: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
03: */package net.sourceforge.pmd.rules;
04:
05: import net.sourceforge.pmd.AbstractRule;
06: import net.sourceforge.pmd.ast.ASTAssignmentOperator;
07: import net.sourceforge.pmd.ast.ASTExpression;
08: import net.sourceforge.pmd.ast.ASTName;
09: import net.sourceforge.pmd.ast.ASTPrimaryExpression;
10: import net.sourceforge.pmd.ast.ASTPrimarySuffix;
11: import net.sourceforge.pmd.ast.ASTStatementExpression;
12: import net.sourceforge.pmd.ast.Node;
13: import net.sourceforge.pmd.ast.SimpleNode;
14:
15: public class IdempotentOperations extends AbstractRule {
16:
17: public Object visit(ASTStatementExpression node, Object data) {
18: if (node.jjtGetNumChildren() != 3
19: || !(node.jjtGetChild(0) instanceof ASTPrimaryExpression)
20: || !(node.jjtGetChild(1) instanceof ASTAssignmentOperator)
21: || (((ASTAssignmentOperator) (node.jjtGetChild(1)))
22: .isCompound())
23: || !(node.jjtGetChild(2) instanceof ASTExpression)
24: || node.jjtGetChild(0).jjtGetChild(0)
25: .jjtGetNumChildren() == 0
26: || node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0)
27: .jjtGetNumChildren() == 0) {
28: return super .visit(node, data);
29: }
30:
31: SimpleNode lhs = (SimpleNode) node.jjtGetChild(0)
32: .jjtGetChild(0).jjtGetChild(0);
33: if (!(lhs instanceof ASTName)) {
34: return super .visit(node, data);
35: }
36:
37: SimpleNode rhs = (SimpleNode) node.jjtGetChild(2)
38: .jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
39: if (!(rhs instanceof ASTName)) {
40: return super .visit(node, data);
41: }
42:
43: if (!lhs.hasImageEqualTo(rhs.getImage())) {
44: return super .visit(node, data);
45: }
46:
47: if (lhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
48: Node n = lhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
49: if (n instanceof ASTPrimarySuffix
50: && ((ASTPrimarySuffix) n).isArrayDereference()) {
51: return super .visit(node, data);
52: }
53: }
54:
55: if (rhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
56: Node n = rhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
57: if (n instanceof ASTPrimarySuffix
58: && ((ASTPrimarySuffix) n).isArguments()
59: || ((ASTPrimarySuffix) n).isArrayDereference()) {
60: return super .visit(node, data);
61: }
62: }
63:
64: if (lhs.findChildrenOfType(ASTPrimarySuffix.class).size() != rhs
65: .findChildrenOfType(ASTPrimarySuffix.class).size()) {
66: return super.visit(node, data);
67: }
68:
69: addViolation(data, node);
70: return super.visit(node, data);
71: }
72: }
|