Search in sources :

Example 6 with ASTLocalVariableDeclaration

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

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

the class LocalScopeTest method testgetEnclosingMethodScope.

@Test
public void testgetEnclosingMethodScope() {
    parseCode(TEST4);
    ASTLocalVariableDeclaration node = acu.findDescendantsOfType(ASTLocalVariableDeclaration.class).get(0);
    LocalScope scope = (LocalScope) node.getScope();
    MethodScope ms = scope.getEnclosingScope(MethodScope.class);
    assertEquals(2, ms.getDeclarations().size());
}
Also used : ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) Test(org.junit.Test)

Aggregations

ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)7 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 ArrayList (java.util.ArrayList)2 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)2 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)2 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 List (java.util.List)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1 ASTConditionalExpression (net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression)1