Search in sources :

Example 6 with ASTStatementExpression

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

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

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

the class ArrayIsStoredDirectlyRule method checkForDirectAssignment.

/**
 * Checks if the variable designed in parameter is written to a field (not
 * local variable) in the statements.
 */
private boolean checkForDirectAssignment(Object ctx, final ASTFormalParameter parameter, final List<ASTBlockStatement> bs) {
    final ASTVariableDeclaratorId vid = parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    final String varName = vid.getImage();
    for (ASTBlockStatement b : bs) {
        if (b.hasDescendantOfType(ASTAssignmentOperator.class)) {
            final ASTStatementExpression se = b.getFirstDescendantOfType(ASTStatementExpression.class);
            if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) {
                continue;
            }
            String assignedVar = getExpressionVarName(se);
            if (assignedVar == null) {
                continue;
            }
            ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
            Node n = pe.getFirstParentOfType(ASTMethodDeclaration.class);
            if (n == null) {
                n = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
                if (n == null) {
                    continue;
                }
            }
            if (!isLocalVariable(assignedVar, n)) {
                // something
                if (se.jjtGetNumChildren() < 3) {
                    continue;
                }
                ASTExpression e = (ASTExpression) se.jjtGetChild(2);
                if (e.hasDescendantOfType(ASTEqualityExpression.class)) {
                    continue;
                }
                String val = getExpressionVarName(e);
                if (val == null) {
                    continue;
                }
                ASTPrimarySuffix foo = e.getFirstDescendantOfType(ASTPrimarySuffix.class);
                if (foo != null && foo.isArrayDereference()) {
                    continue;
                }
                if (val.equals(varName)) {
                    Node md = parameter.getFirstParentOfType(ASTMethodDeclaration.class);
                    if (md == null) {
                        md = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
                    }
                    if (!isLocalVariable(varName, md)) {
                        addViolation(ctx, parameter, varName);
                    }
                }
            }
        }
    }
    return false;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 9 with ASTStatementExpression

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

Example 10 with ASTStatementExpression

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

the class NonThreadSafeSingletonRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    if (checkNonStaticMethods && !node.isStatic() || node.isSynchronized()) {
        return super.visit(node, data);
    }
    List<ASTIfStatement> ifStatements = node.findDescendantsOfType(ASTIfStatement.class);
    for (ASTIfStatement ifStatement : ifStatements) {
        if (ifStatement.getFirstParentOfType(ASTSynchronizedStatement.class) == null) {
            if (!ifStatement.hasDescendantOfType(ASTNullLiteral.class)) {
                continue;
            }
            ASTName n = ifStatement.getFirstDescendantOfType(ASTName.class);
            if (n == null || !fieldDecls.containsKey(n.getImage())) {
                continue;
            }
            List<ASTAssignmentOperator> assigmnents = ifStatement.findDescendantsOfType(ASTAssignmentOperator.class);
            boolean violation = false;
            for (int ix = 0; ix < assigmnents.size(); ix++) {
                ASTAssignmentOperator oper = assigmnents.get(ix);
                if (!(oper.jjtGetParent() instanceof ASTStatementExpression)) {
                    continue;
                }
                ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
                if (expr.jjtGetChild(0) instanceof ASTPrimaryExpression && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetNumChildren() == 1 && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0) instanceof ASTPrimaryPrefix) {
                    ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0);
                    String name = null;
                    if (pp.usesThisModifier()) {
                        ASTPrimarySuffix priSuf = expr.getFirstDescendantOfType(ASTPrimarySuffix.class);
                        name = priSuf.getImage();
                    } else {
                        ASTName astName = (ASTName) pp.jjtGetChild(0);
                        name = astName.getImage();
                    }
                    if (fieldDecls.containsKey(name)) {
                        violation = true;
                    }
                }
            }
            if (violation) {
                addViolation(data, ifStatement);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTSynchronizedStatement(net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName)

Aggregations

ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)12 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)8 Node (net.sourceforge.pmd.lang.ast.Node)7 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)5 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)5 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)5 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)3 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)3 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)3 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)3 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)3 ASTSynchronizedStatement (net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement)3 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)3 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)3 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2