Search in sources :

Example 1 with ASTExpression

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

the class ClassTypeResolverTest method testUnaryLogicalOperators.

@Test
public void testUnaryLogicalOperators() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Operators.class);
    List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'unaryLogicalOperators']]//Expression"), ASTExpression.class);
    int index = 0;
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    assertEquals(Boolean.TYPE, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Example 2 with ASTExpression

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

the class ClassTypeResolverTest method testBinaryStringPromotion.

@Test
public void testBinaryStringPromotion() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(Promotion.class);
    List<ASTExpression> expressions = convertList(acu.findChildNodesWithXPath("//Block[preceding-sibling::MethodDeclarator[@Image = 'binaryStringPromotion']]//Expression"), ASTExpression.class);
    int index = 0;
    assertEquals(String.class, expressions.get(index++).getType());
    assertEquals(String.class, expressions.get(index++).getType());
    assertEquals(String.class, expressions.get(index++).getType());
    assertEquals(String.class, expressions.get(index++).getType());
    assertEquals(String.class, expressions.get(index++).getType());
    // Make sure we got them all.
    assertEquals("All expressions not tested", index, expressions.size());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) Test(org.junit.Test)

Example 3 with ASTExpression

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

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

the class StringInstantiationRule method visit.

@Override
public Object visit(ASTAllocationExpression node, Object data) {
    if (!(node.jjtGetChild(0) instanceof ASTClassOrInterfaceType)) {
        return data;
    }
    if (!TypeHelper.isA((ASTClassOrInterfaceType) node.jjtGetChild(0), String.class)) {
        return data;
    }
    List<ASTExpression> exp = node.findDescendantsOfType(ASTExpression.class);
    if (exp.size() >= 2) {
        return data;
    }
    if (node.hasDescendantOfAnyType(ASTArrayDimsAndInits.class, ASTAdditiveExpression.class)) {
        return data;
    }
    ASTName name = node.getFirstDescendantOfType(ASTName.class);
    // Literal, i.e., new String("foo")
    if (name == null) {
        addViolation(data, node);
        return data;
    }
    NameDeclaration nd = name.getNameDeclaration();
    if (nd == null) {
        return data;
    }
    if (nd instanceof TypedNameDeclaration && TypeHelper.isA((TypedNameDeclaration) nd, String.class)) {
        addViolation(data, node);
    }
    return data;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) TypedNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.TypedNameDeclaration) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 5 with ASTExpression

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

Aggregations

ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)26 Test (org.junit.Test)9 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)8 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)8 Node (net.sourceforge.pmd.lang.ast.Node)7 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)6 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)6 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)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 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)2 ASTForUpdate (net.sourceforge.pmd.lang.java.ast.ASTForUpdate)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 ResolutionFailedException (net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException)2 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2