Search in sources :

Example 1 with ASTConditionalExpression

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

the class UseStringBufferForStringAppendsRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    if (!TypeHelper.isA(node, String.class) || node.isArray()) {
        return data;
    }
    Node parent = node.jjtGetParent().jjtGetParent();
    if (!(parent instanceof ASTLocalVariableDeclaration)) {
        return data;
    }
    for (NameOccurrence no : node.getUsages()) {
        Node name = no.getLocation();
        ASTStatementExpression statement = name.getFirstParentOfType(ASTStatementExpression.class);
        if (statement == null) {
            continue;
        }
        ASTArgumentList argList = name.getFirstParentOfType(ASTArgumentList.class);
        if (argList != null && argList.getFirstParentOfType(ASTStatementExpression.class) == statement) {
            // used in method call
            continue;
        }
        ASTEqualityExpression equality = name.getFirstParentOfType(ASTEqualityExpression.class);
        if (equality != null && equality.getFirstParentOfType(ASTStatementExpression.class) == statement) {
            // used in condition
            continue;
        }
        ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class);
        if (conditional != null) {
            Node thirdParent = name.getNthParent(3);
            Node fourthParent = name.getNthParent(4);
            if ((Objects.equals(thirdParent, conditional) || Objects.equals(fourthParent, conditional)) && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
                // string)
                continue;
            }
        }
        if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
            ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
            if (astName != null) {
                if (astName.equals(name)) {
                    ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
                    if (assignmentOperator != null && assignmentOperator.isCompound()) {
                        addViolation(data, assignmentOperator);
                    }
                } else if (astName.getImage().equals(name.getImage())) {
                    ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
                    if (assignmentOperator != null && !assignmentOperator.isCompound()) {
                        addViolation(data, astName);
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTConditionalExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

Node (net.sourceforge.pmd.lang.ast.Node)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)1 ASTConditionalExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression)1 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)1 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)1 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)1 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)1