Search in sources :

Example 1 with ASTPreDecrementExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPreDecrementExpression in project pmd by pmd.

the class JavaNameOccurrence method isSelfAssignment.

/**
 * Assert it the occurrence is a self assignment such as:
 * <code>i += 3;</code>
 *
 * @return true, if the occurrence is self-assignment, false, otherwise.
 */
@SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop")
public boolean isSelfAssignment() {
    Node l = location;
    while (true) {
        Node p = l.jjtGetParent();
        Node gp = p.jjtGetParent();
        Node node = gp.jjtGetParent();
        if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) {
            return true;
        }
        if (hasAssignmentOperator(gp)) {
            return isCompoundAssignment(gp);
        }
        if (hasAssignmentOperator(node)) {
            return isCompoundAssignment(node);
        }
        // deal with extra parenthesis: "(i)++"
        if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 && gp instanceof ASTPrimaryExpression && gp.jjtGetNumChildren() == 1 && node instanceof ASTExpression && node.jjtGetNumChildren() == 1 && node.jjtGetParent() instanceof ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) {
            l = node;
            continue;
        }
        // catch this.i++ or ++this.i
        return gp instanceof ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression || gp instanceof ASTPostfixExpression;
    }
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTPostfixExpression(net.sourceforge.pmd.lang.java.ast.ASTPostfixExpression) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPreDecrementExpression(net.sourceforge.pmd.lang.java.ast.ASTPreDecrementExpression) ASTPreIncrementExpression(net.sourceforge.pmd.lang.java.ast.ASTPreIncrementExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Aggregations

Node (net.sourceforge.pmd.lang.ast.Node)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTPostfixExpression (net.sourceforge.pmd.lang.java.ast.ASTPostfixExpression)1 ASTPreDecrementExpression (net.sourceforge.pmd.lang.java.ast.ASTPreDecrementExpression)1 ASTPreIncrementExpression (net.sourceforge.pmd.lang.java.ast.ASTPreIncrementExpression)1 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)1