Search in sources :

Example 1 with ASTAssignmentOperator

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

the class IdempotentOperationsRule method visit.

@Override
public Object visit(ASTStatementExpression node, Object data) {
    if (node.jjtGetNumChildren() != 3 || !(node.jjtGetChild(0) instanceof ASTPrimaryExpression) || !(node.jjtGetChild(1) instanceof ASTAssignmentOperator) || ((ASTAssignmentOperator) node.jjtGetChild(1)).isCompound() || !(node.jjtGetChild(2) instanceof ASTExpression) || node.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0 || node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0) {
        return super.visit(node, data);
    }
    Node lhs = node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
    if (!(lhs instanceof ASTName)) {
        return super.visit(node, data);
    }
    Node rhs = node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
    if (!(rhs instanceof ASTName)) {
        return super.visit(node, data);
    }
    if (!lhs.hasImageEqualTo(rhs.getImage())) {
        return super.visit(node, data);
    }
    if (lhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
        Node n = lhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
        if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArrayDereference()) {
            return super.visit(node, data);
        }
    }
    if (rhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
        Node n = rhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
        if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArguments() || ((ASTPrimarySuffix) n).isArrayDereference()) {
            return super.visit(node, data);
        }
    }
    if (lhs.findDescendantsOfType(ASTPrimarySuffix.class).size() != rhs.findDescendantsOfType(ASTPrimarySuffix.class).size()) {
        return super.visit(node, data);
    }
    List<ASTPrimarySuffix> lhsSuffixes = lhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class);
    List<ASTPrimarySuffix> rhsSuffixes = rhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class);
    if (lhsSuffixes.size() != rhsSuffixes.size()) {
        return super.visit(node, data);
    }
    for (int i = 0; i < lhsSuffixes.size(); i++) {
        ASTPrimarySuffix l = lhsSuffixes.get(i);
        ASTPrimarySuffix r = rhsSuffixes.get(i);
        if (!l.hasImageEqualTo(r.getImage())) {
            return super.visit(node, data);
        }
    }
    addViolation(data, node);
    return super.visit(node, data);
}
Also used : ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 2 with ASTAssignmentOperator

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

the class DoubleCheckedLockingRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (node.getResultType().isVoid()) {
        return super.visit(node, data);
    }
    ASTType typeNode = (ASTType) node.getResultType().jjtGetChild(0);
    if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) {
        return super.visit(node, data);
    }
    List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
    if (rsl.size() != 1) {
        return super.visit(node, data);
    }
    ASTReturnStatement rs = rsl.get(0);
    List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
    ASTPrimaryExpression ape = pel.get(0);
    Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
    String returnVariableName = null;
    if (lastChild instanceof ASTPrimaryPrefix) {
        returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
    }
    // With Java5 and volatile keyword, DCL is no longer an issue
    if (returnVariableName == null || this.volatileFields.contains(returnVariableName)) {
        return super.visit(node, data);
    }
    // field, then it's ok, too
    if (checkLocalVariableUsage(node, returnVariableName)) {
        return super.visit(node, data);
    }
    List<ASTIfStatement> isl = node.findDescendantsOfType(ASTIfStatement.class);
    if (isl.size() == 2) {
        ASTIfStatement is = isl.get(0);
        if (ifVerify(is, returnVariableName)) {
            // find synchronized
            List<ASTSynchronizedStatement> ssl = is.findDescendantsOfType(ASTSynchronizedStatement.class);
            if (ssl.size() == 1) {
                ASTSynchronizedStatement ss = ssl.get(0);
                isl = ss.findDescendantsOfType(ASTIfStatement.class);
                if (isl.size() == 1) {
                    ASTIfStatement is2 = isl.get(0);
                    if (ifVerify(is2, returnVariableName)) {
                        List<ASTStatementExpression> sel = is2.findDescendantsOfType(ASTStatementExpression.class);
                        if (sel.size() == 1) {
                            ASTStatementExpression se = sel.get(0);
                            if (se.jjtGetNumChildren() == 3) {
                                // primaryExpression, AssignmentOperator, Expression
                                if (se.jjtGetChild(0) instanceof ASTPrimaryExpression) {
                                    ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
                                    if (matchName(pe, returnVariableName)) {
                                        if (se.jjtGetChild(1) instanceof ASTAssignmentOperator) {
                                            addViolation(data, node);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) Node(net.sourceforge.pmd.lang.ast.Node) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType)

Example 3 with ASTAssignmentOperator

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

the class DoubleCheckedLockingRule method checkLocalVariableUsage.

private boolean checkLocalVariableUsage(ASTMethodDeclaration node, String returnVariableName) {
    List<ASTLocalVariableDeclaration> locals = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
    ASTVariableInitializer initializer = null;
    for (ASTLocalVariableDeclaration l : locals) {
        ASTVariableDeclaratorId id = l.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
        if (id != null && id.hasImageEqualTo(returnVariableName)) {
            initializer = l.getFirstDescendantOfType(ASTVariableInitializer.class);
            break;
        }
    }
    // the return variable name doesn't seem to be a local variable
    if (initializer == null) {
        return false;
    }
    // verify the value with which the local variable is initialized
    if (initializer.jjtGetNumChildren() > 0 && initializer.jjtGetChild(0) instanceof ASTExpression && initializer.jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimaryExpression && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimaryPrefix && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTName) {
        ASTName name = (ASTName) initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
        if (name == null || !volatileFields.contains(name.getImage())) {
            return false;
        }
    } else {
        // not a simple assignment
        return false;
    }
    // now check every usage/assignment of the variable
    List<ASTName> names = node.findDescendantsOfType(ASTName.class);
    for (ASTName n : names) {
        if (!n.hasImageEqualTo(returnVariableName)) {
            continue;
        }
        Node expression = n.getNthParent(3);
        if (expression instanceof ASTEqualityExpression) {
            continue;
        }
        if (expression instanceof ASTStatementExpression) {
            if (expression.jjtGetNumChildren() > 2 && expression.jjtGetChild(1) instanceof ASTAssignmentOperator) {
                ASTName value = expression.jjtGetChild(2).getFirstDescendantOfType(ASTName.class);
                if (value == null || !volatileFields.contains(value.getImage())) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Example 4 with ASTAssignmentOperator

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

the class SingularFieldRule method isInAssignment.

private boolean isInAssignment(Node potentialStatement) {
    if (potentialStatement instanceof ASTStatementExpression) {
        ASTStatementExpression statement = (ASTStatementExpression) potentialStatement;
        List<ASTAssignmentOperator> assignments = statement.findDescendantsOfType(ASTAssignmentOperator.class);
        return !assignments.isEmpty() && "=".equals(assignments.get(0).getImage());
    } else {
        return false;
    }
}
Also used : ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)

Example 5 with ASTAssignmentOperator

use of net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator 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

ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)6 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)5 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)2 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTSynchronizedStatement (net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTConditionalExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression)1 ASTNullLiteral (net.sourceforge.pmd.lang.java.ast.ASTNullLiteral)1 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)1 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)1 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)1 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)1 ASTVariableInitializer (net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer)1