Search in sources :

Example 1 with ASTVariableDeclarator

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

the class RedundantFieldInitializerRule method visit.

public Object visit(ASTFieldDeclaration fieldDeclaration, Object data) {
    // Finals can only be initialized once.
    if (fieldDeclaration.isFinal()) {
        return data;
    }
    // VariableDeclarator/VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/Literal
    for (ASTVariableDeclarator variableDeclarator : fieldDeclaration.findChildrenOfType(ASTVariableDeclarator.class)) {
        if (variableDeclarator.jjtGetNumChildren() > 1) {
            final Node variableInitializer = variableDeclarator.jjtGetChild(1);
            if (variableInitializer.jjtGetChild(0) instanceof ASTExpression) {
                final Node expression = variableInitializer.jjtGetChild(0);
                final Node primaryExpression;
                if (expression.jjtGetNumChildren() == 1) {
                    if (expression.jjtGetChild(0) instanceof ASTPrimaryExpression) {
                        primaryExpression = expression.jjtGetChild(0);
                    } else if (expression.jjtGetChild(0) instanceof ASTCastExpression && expression.jjtGetChild(0).jjtGetChild(1) instanceof ASTPrimaryExpression) {
                        primaryExpression = expression.jjtGetChild(0).jjtGetChild(1);
                    } else {
                        continue;
                    }
                } else {
                    continue;
                }
                final Node primaryPrefix = primaryExpression.jjtGetChild(0);
                if (primaryPrefix.jjtGetNumChildren() == 1 && primaryPrefix.jjtGetChild(0) instanceof ASTLiteral) {
                    final ASTLiteral literal = (ASTLiteral) primaryPrefix.jjtGetChild(0);
                    if (isRef(fieldDeclaration, variableDeclarator)) {
                        // Reference type
                        if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTNullLiteral) {
                            addViolation(data, variableDeclarator);
                        }
                    } else {
                        // Primitive type
                        if (literal.jjtGetNumChildren() == 1 && literal.jjtGetChild(0) instanceof ASTBooleanLiteral) {
                            // boolean type
                            ASTBooleanLiteral booleanLiteral = (ASTBooleanLiteral) literal.jjtGetChild(0);
                            if (!booleanLiteral.isTrue()) {
                                addViolation(data, variableDeclarator);
                            }
                        } else if (literal.jjtGetNumChildren() == 0) {
                            // numeric type
                            // Note: Not catching NumberFormatException, as
                            // it shouldn't be happening on valid source
                            // code.
                            Number value = -1;
                            if (literal.isIntLiteral()) {
                                value = literal.getValueAsInt();
                            } else if (literal.isLongLiteral()) {
                                value = literal.getValueAsLong();
                            } else if (literal.isFloatLiteral()) {
                                value = literal.getValueAsFloat();
                            } else if (literal.isDoubleLiteral()) {
                                value = literal.getValueAsDouble();
                            } else if (literal.isCharLiteral()) {
                                value = (int) literal.getImage().charAt(1);
                            }
                            if (value.doubleValue() == 0) {
                                addViolation(data, variableDeclarator);
                            }
                        }
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) ASTBooleanLiteral(net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTNullLiteral(net.sourceforge.pmd.lang.java.ast.ASTNullLiteral) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 2 with ASTVariableDeclarator

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

the class ForLoopCanBeForeachRule method indexStartsAtZero.

/* We only report loops with int initializers starting at zero. */
private boolean indexStartsAtZero(VariableNameDeclaration index) {
    ASTVariableDeclaratorId name = (ASTVariableDeclaratorId) index.getNode();
    ASTVariableDeclarator declarator = name.getFirstParentOfType(ASTVariableDeclarator.class);
    if (declarator == null) {
        return false;
    }
    try {
        List<Node> zeroLiteral = declarator.findChildNodesWithXPath("./VariableInitializer/Expression/PrimaryExpression/PrimaryPrefix/Literal[@Image='0' and " + "@StringLiteral='false']");
        if (!zeroLiteral.isEmpty()) {
            return true;
        }
    } catch (JaxenException je) {
        throw new RuntimeException(je);
    }
    return false;
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) Node(net.sourceforge.pmd.lang.ast.Node) ScopedNode(net.sourceforge.pmd.lang.symboltable.ScopedNode) JaxenException(org.jaxen.JaxenException)

Example 3 with ASTVariableDeclarator

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

the class CheckResultSetRule method visit.

@Override
public Object visit(ASTLocalVariableDeclaration node, Object data) {
    ASTClassOrInterfaceType type = node.getFirstChildOfType(ASTType.class).getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (type != null && (type.getType() != null && "java.sql.ResultSet".equals(type.getType().getName()) || "ResultSet".equals(type.getImage()))) {
        ASTVariableDeclarator declarator = node.getFirstChildOfType(ASTVariableDeclarator.class);
        if (declarator != null) {
            ASTName name = declarator.getFirstDescendantOfType(ASTName.class);
            if (type.getType() != null || name != null && name.getImage().endsWith("executeQuery")) {
                ASTVariableDeclaratorId id = declarator.getFirstChildOfType(ASTVariableDeclaratorId.class);
                resultSetVariables.put(id.getImage(), node);
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 4 with ASTVariableDeclarator

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

the class SingularFieldRule method visit.

@SuppressWarnings("PMD.CompareObjectsWithEquals")
@Override
public Object visit(ASTFieldDeclaration node, Object data) {
    boolean checkInnerClasses = getProperty(CHECK_INNER_CLASSES);
    boolean disallowNotAssignment = getProperty(DISALLOW_NOT_ASSIGNMENT);
    if (node.isPrivate() && !node.isStatic() && !hasClassLombokAnnotation() && !hasLombokAnnotation(node)) {
        for (ASTVariableDeclarator declarator : node.findChildrenOfType(ASTVariableDeclarator.class)) {
            ASTVariableDeclaratorId declaration = (ASTVariableDeclaratorId) declarator.jjtGetChild(0);
            List<NameOccurrence> usages = declaration.getUsages();
            Node decl = null;
            boolean violation = true;
            for (int ix = 0; ix < usages.size(); ix++) {
                NameOccurrence no = usages.get(ix);
                Node location = no.getLocation();
                ASTPrimaryExpression primaryExpressionParent = location.getFirstParentOfType(ASTPrimaryExpression.class);
                if (ix == 0 && !disallowNotAssignment) {
                    if (primaryExpressionParent.getFirstParentOfType(ASTIfStatement.class) != null) {
                        // the first usage is in an if, so it may be skipped
                        // on
                        // later calls to the method. So this might be legit
                        // code
                        // that simply stores an object for later use.
                        violation = false;
                        // Optimization
                        break;
                    }
                    // Is the first usage in an assignment?
                    Node potentialStatement = primaryExpressionParent.jjtGetParent();
                    // Check that the assignment is not to a field inside
                    // the field object
                    boolean assignmentToField = no.getImage().equals(location.getImage());
                    if (!assignmentToField || !isInAssignment(potentialStatement)) {
                        violation = false;
                        // Optimization
                        break;
                    } else {
                        if (usages.size() > ix + 1) {
                            Node secondUsageLocation = usages.get(ix + 1).getLocation();
                            List<ASTStatementExpression> parentStatements = secondUsageLocation.getParentsOfType(ASTStatementExpression.class);
                            for (ASTStatementExpression statementExpression : parentStatements) {
                                if (statementExpression != null && statementExpression.equals(potentialStatement)) {
                                    // The second usage is in the assignment
                                    // of the first usage, which is allowed
                                    violation = false;
                                    // Optimization
                                    break;
                                }
                            }
                        }
                    }
                }
                if (!checkInnerClasses) {
                    // Skip inner classes because the field can be used in
                    // the outer class and checking this is too difficult
                    ASTClassOrInterfaceDeclaration clazz = location.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
                    if (clazz != null && clazz.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) != null) {
                        violation = false;
                        // Optimization
                        break;
                    }
                }
                if (primaryExpressionParent.jjtGetParent() instanceof ASTSynchronizedStatement) {
                    // This usage is directly in an expression of a
                    // synchronized block
                    violation = false;
                    // Optimization
                    break;
                }
                if (location.getFirstParentOfType(ASTLambdaExpression.class) != null) {
                    // This usage is inside a lambda expression
                    violation = false;
                    // Optimization
                    break;
                }
                Node method = location.getFirstParentOfType(ASTMethodDeclaration.class);
                if (method == null) {
                    method = location.getFirstParentOfType(ASTConstructorDeclaration.class);
                    if (method == null) {
                        method = location.getFirstParentOfType(ASTInitializer.class);
                        if (method == null) {
                            continue;
                        }
                    }
                }
                if (decl == null) {
                    decl = method;
                    continue;
                } else if (decl != method && // handle inner classes
                decl.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class) == method.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class)) {
                    violation = false;
                    // Optimization
                    break;
                }
            }
            if (violation && !usages.isEmpty()) {
                addViolation(data, node, new Object[] { declaration.getImage() });
            }
        }
    }
    return data;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) 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) ASTInitializer(net.sourceforge.pmd.lang.java.ast.ASTInitializer) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTLambdaExpression(net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 5 with ASTVariableDeclarator

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

the class StatementAndBraceFinderTest method testVariableDeclaratorParentChildLinks.

@Test
public void testVariableDeclaratorParentChildLinks() {
    ASTVariableDeclarator vd = getOrderedNodes(ASTVariableDeclarator.class, TEST2).get(0);
    ASTMethodDeclaration vdParent = (ASTMethodDeclaration) vd.getDataFlowNode().getParents().get(0).getNode();
    assertEquals(vd, vdParent.getDataFlowNode().getChildren().get(0).getNode());
    assertEquals(vdParent, vd.getDataFlowNode().getParents().get(0).getNode());
}
Also used : ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) ASTVariableDeclarator(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator) Test(org.junit.Test)

Aggregations

ASTVariableDeclarator (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclarator)5 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)3 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)2 ASTBooleanLiteral (net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral)1 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)1 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)1 ASTInitializer (net.sourceforge.pmd.lang.java.ast.ASTInitializer)1 ASTLambdaExpression (net.sourceforge.pmd.lang.java.ast.ASTLambdaExpression)1 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTNullLiteral (net.sourceforge.pmd.lang.java.ast.ASTNullLiteral)1 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)1 ASTSynchronizedStatement (net.sourceforge.pmd.lang.java.ast.ASTSynchronizedStatement)1 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)1