Search in sources :

Example 1 with ASTEqualityExpression

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

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

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

the class BrokenNullCheckRule method checkForViolations.

private void checkForViolations(ASTIfStatement node, Object data, Node conditionalExpression) {
    ASTEqualityExpression equalityExpression = conditionalExpression.getFirstChildOfType(ASTEqualityExpression.class);
    if (equalityExpression == null) {
        return;
    }
    if (conditionalExpression instanceof ASTConditionalAndExpression && !"==".equals(equalityExpression.getImage())) {
        return;
    }
    if (conditionalExpression instanceof ASTConditionalOrExpression && !"!=".equals(equalityExpression.getImage())) {
        return;
    }
    ASTNullLiteral nullLiteral = equalityExpression.getFirstDescendantOfType(ASTNullLiteral.class);
    if (nullLiteral == null) {
        // No null check
        return;
    }
    // because things get too complex
    if (conditionalExpression.hasDescendantOfType(ASTAssignmentOperator.class)) {
        return;
    }
    // Find the expression used in the null compare
    ASTPrimaryExpression nullCompareExpression = findNullCompareExpression(equalityExpression);
    if (nullCompareExpression == null) {
        // No good null check
        return;
    }
    // Now we find the expression to compare to and do the comparison
    for (int i = 0; i < conditionalExpression.jjtGetNumChildren(); i++) {
        Node conditionalSubnode = conditionalExpression.jjtGetChild(i);
        // We skip the null compare branch
        ASTEqualityExpression nullEqualityExpression = nullLiteral.getFirstParentOfType(ASTEqualityExpression.class);
        if (conditionalSubnode.equals(nullEqualityExpression)) {
            continue;
        }
        ASTPrimaryExpression conditionalPrimaryExpression;
        if (conditionalSubnode instanceof ASTPrimaryExpression) {
            conditionalPrimaryExpression = (ASTPrimaryExpression) conditionalSubnode;
        } else {
            // The ASTPrimaryExpression is hidden (in a negation, braces or
            // EqualityExpression)
            conditionalPrimaryExpression = conditionalSubnode.getFirstDescendantOfType(ASTPrimaryExpression.class);
        }
        if (primaryExpressionsAreEqual(nullCompareExpression, conditionalPrimaryExpression)) {
            // We have a match
            addViolation(data, node);
        }
    }
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTConditionalOrExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTConditionalAndExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression)

Example 4 with ASTEqualityExpression

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

the class AcceptanceTest method testEq.

@Test
public void testEq() {
    parseCode(TEST_EQ);
    ASTEqualityExpression e = acu.findDescendantsOfType(ASTEqualityExpression.class).get(0);
    ASTMethodDeclaration method = e.getFirstParentOfType(ASTMethodDeclaration.class);
    Scope s = method.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
    assertEquals(2, m.size());
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
        NameDeclaration vnd = entry.getKey();
        List<NameOccurrence> usages = entry.getValue();
        if (vnd.getImage().equals("a") || vnd.getImage().equals("b")) {
            assertEquals(1, usages.size());
            assertEquals(3, usages.get(0).getLocation().getBeginLine());
        } else {
            fail("Unkown variable " + vnd);
        }
    }
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Scope(net.sourceforge.pmd.lang.symboltable.Scope) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) Test(org.junit.Test)

Aggregations

ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)4 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)3 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)2 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2 List (java.util.List)1 Map (java.util.Map)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTConditionalAndExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression)1 ASTConditionalExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression)1 ASTConditionalOrExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 ASTNullLiteral (net.sourceforge.pmd.lang.java.ast.ASTNullLiteral)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)1 ASTVariableInitializer (net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer)1