View Javadoc
1 /*** 2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html 3 */ 4 package net.sourceforge.pmd.rules; 5 6 import net.sourceforge.pmd.AbstractRule; 7 import net.sourceforge.pmd.RuleContext; 8 import net.sourceforge.pmd.ast.ASTAssignmentOperator; 9 import net.sourceforge.pmd.ast.ASTExpression; 10 import net.sourceforge.pmd.ast.ASTName; 11 import net.sourceforge.pmd.ast.ASTPrimaryExpression; 12 import net.sourceforge.pmd.ast.ASTStatementExpression; 13 import net.sourceforge.pmd.ast.SimpleNode; 14 15 public class IdempotentOperationsRule 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 || !(node.jjtGetChild(2) instanceof ASTExpression) 22 ) { 23 return super.visit(node, data); 24 } 25 26 SimpleNode lhs = (SimpleNode)node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0); 27 if (!(lhs instanceof ASTName)) { 28 return super.visit(node, data); 29 } 30 31 SimpleNode rhs = (SimpleNode)node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0); 32 if (!(rhs instanceof ASTName)) { 33 return super.visit(node, data); 34 } 35 36 if (!lhs.getImage().equals(rhs.getImage())) { 37 return super.visit(node, data); 38 } 39 40 RuleContext ctx = (RuleContext) data; 41 ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine(), "Avoid idempotent operations")); 42 return data; 43 } 44 }

This page was automatically generated by Maven