Search in sources :

Example 6 with ASTAllocationExpression

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

the class InefficientStringBufferingRule method isAllocatedStringBuffer.

private boolean isAllocatedStringBuffer(ASTAdditiveExpression node) {
    ASTAllocationExpression ao = node.getFirstParentOfType(ASTAllocationExpression.class);
    if (ao == null) {
        return false;
    }
    // note that the child can be an ArrayDimsAndInits, for example, from
    // java.lang.FloatingDecimal: t = new int[ nWords+wordcount+1 ];
    ASTClassOrInterfaceType an = ao.getFirstChildOfType(ASTClassOrInterfaceType.class);
    return an != null && TypeHelper.isEither(an, StringBuffer.class, StringBuilder.class);
}
Also used : ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 7 with ASTAllocationExpression

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

the class InsufficientStringBufferDeclarationRule method getConstructorLength.

private int getConstructorLength(Node node, int constructorLength) {
    int iConstructorLength = constructorLength;
    Node block = node.getFirstParentOfType(ASTBlockStatement.class);
    if (block == null) {
        block = node.getFirstParentOfType(ASTFieldDeclaration.class);
    }
    if (block == null) {
        block = node.getFirstParentOfType(ASTFormalParameter.class);
        if (block != null) {
            iConstructorLength = -1;
        } else {
            return DEFAULT_BUFFER_SIZE;
        }
    }
    // if there is any addition/subtraction going on then just use the
    // default.
    ASTAdditiveExpression exp = block.getFirstDescendantOfType(ASTAdditiveExpression.class);
    if (exp != null) {
        return DEFAULT_BUFFER_SIZE;
    }
    ASTMultiplicativeExpression mult = block.getFirstDescendantOfType(ASTMultiplicativeExpression.class);
    if (mult != null) {
        return DEFAULT_BUFFER_SIZE;
    }
    List<ASTLiteral> literals;
    ASTAllocationExpression constructorCall = block.getFirstDescendantOfType(ASTAllocationExpression.class);
    if (constructorCall != null) {
        // if this is a constructor call, only consider the literals within
        // it.
        literals = constructorCall.findDescendantsOfType(ASTLiteral.class);
    } else {
        // otherwise it might be a setLength call...
        literals = block.findDescendantsOfType(ASTLiteral.class);
    }
    if (literals.isEmpty()) {
        List<ASTName> name = block.findDescendantsOfType(ASTName.class);
        if (!name.isEmpty()) {
            iConstructorLength = -1;
        }
    } else if (literals.size() == 1) {
        ASTLiteral literal = literals.get(0);
        String str = literal.getImage();
        if (str == null) {
            iConstructorLength = 0;
        } else if (isStringOrCharLiteral(literal)) {
            // since it's not taken into account
            // anywhere. only count the extra 16
            // characters
            // don't add the constructor's length
            iConstructorLength = 14 + str.length();
        } else if (literal.isIntLiteral()) {
            iConstructorLength = literal.getValueAsInt();
        }
    } else {
        iConstructorLength = -1;
    }
    if (iConstructorLength == 0) {
        if (constructorLength == -1) {
            iConstructorLength = DEFAULT_BUFFER_SIZE;
        } else {
            iConstructorLength = constructorLength;
        }
    }
    return iConstructorLength;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTMultiplicativeExpression(net.sourceforge.pmd.lang.java.ast.ASTMultiplicativeExpression) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 8 with ASTAllocationExpression

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

the class ConsecutiveLiteralAppendsRule method checkInitializerExpressions.

/**
 * Determine if during the variable initializer calls to ".append" are done.
 *
 * @param node
 * @return
 */
private int checkInitializerExpressions(ASTVariableDeclaratorId node) {
    ASTVariableInitializer initializer = node.jjtGetParent().getFirstChildOfType(ASTVariableInitializer.class);
    ASTPrimaryExpression primary = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
    int result = 0;
    boolean previousWasAppend = false;
    for (int i = 0; i < primary.jjtGetNumChildren(); i++) {
        Node child = primary.jjtGetChild(i);
        if (child.jjtGetNumChildren() > 0 && child.jjtGetChild(0) instanceof ASTAllocationExpression) {
            // skip the constructor call, that has already been checked
            continue;
        }
        if (child instanceof ASTPrimarySuffix) {
            ASTPrimarySuffix suffix = (ASTPrimarySuffix) child;
            if (suffix.jjtGetNumChildren() == 0 && suffix.hasImageEqualTo("append")) {
                previousWasAppend = true;
            } else if (suffix.jjtGetNumChildren() > 0 && previousWasAppend) {
                previousWasAppend = false;
                ASTLiteral literal = suffix.getFirstDescendantOfType(ASTLiteral.class);
                if (literal != null && literal.isStringLiteral()) {
                    result++;
                } else {
                    // checking the remainder of the initializer
                    break;
                }
            }
        }
    }
    return result;
}
Also used : ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)

Example 9 with ASTAllocationExpression

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

the class PreserveStackTraceRule method visit.

@Override
public Object visit(ASTCatchStatement catchStmt, Object data) {
    String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage();
    // Inspect all the throw stmt inside the catch stmt
    List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class);
    for (ASTThrowStatement throwStatement : lstThrowStatements) {
        Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
        if (n instanceof ASTCastExpression) {
            ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
            if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
                RuleContext ctx = (RuleContext) data;
                addViolation(ctx, throwStatement);
            }
            continue;
        }
        // Retrieve all argument for the throw exception (to see if the
        // original exception is preserved)
        ASTArgumentList args = throwStatement.getFirstDescendantOfType(ASTArgumentList.class);
        if (args != null) {
            Node parent = args.jjtGetParent().jjtGetParent();
            if (parent instanceof ASTAllocationExpression) {
                // maybe it is used inside a anonymous class
                ck(data, target, throwStatement, parent);
            } else {
                // Check all arguments used in the throw statement
                ck(data, target, throwStatement, throwStatement);
            }
        } else {
            Node child = throwStatement.jjtGetChild(0);
            while (child != null && child.jjtGetNumChildren() > 0 && !(child instanceof ASTName)) {
                child = child.jjtGetChild(0);
            }
            if (child != null) {
                if (child instanceof ASTName && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
                    Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope().getDeclarations(VariableNameDeclaration.class);
                    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
                        VariableNameDeclaration decl = entry.getKey();
                        List<NameOccurrence> occurrences = entry.getValue();
                        if (decl.getImage().equals(child.getImage())) {
                            if (!isInitCauseCalled(target, occurrences)) {
                                // Check how the variable is initialized
                                ASTVariableInitializer initializer = decl.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableInitializer.class);
                                if (initializer != null) {
                                    args = initializer.getFirstDescendantOfType(ASTArgumentList.class);
                                    if (args != null) {
                                        // constructor with args?
                                        ck(data, target, throwStatement, args);
                                    } else if (!isFillInStackTraceCalled(target, initializer)) {
                                        addViolation(data, throwStatement);
                                    }
                                }
                            }
                        }
                    }
                } else if (child instanceof ASTClassOrInterfaceType) {
                    addViolation(data, throwStatement);
                }
            }
        }
    }
    return super.visit(catchStmt, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) RuleContext(net.sourceforge.pmd.RuleContext) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ASTThrowStatement(net.sourceforge.pmd.lang.java.ast.ASTThrowStatement) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 10 with ASTAllocationExpression

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

the class ClassTypeResolverTest method testNestedAllocationExpressions.

@Test
public void testNestedAllocationExpressions() {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(NestedAllocationExpressions.class);
    List<ASTAllocationExpression> allocs = acu.findDescendantsOfType(ASTAllocationExpression.class);
    assertFalse(allocs.get(0).isAnonymousClass());
    assertEquals(Thread.class, allocs.get(0).getType());
    assertTrue(allocs.get(1).isAnonymousClass());
    // FUTURE 1.8 use Class.getTypeName() instead of toString
    assertTrue(allocs.get(1).getType().toString().endsWith("NestedAllocationExpressions$1"));
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) Test(org.junit.Test)

Aggregations

ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)11 Node (net.sourceforge.pmd.lang.ast.Node)9 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)6 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)4 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)4 Test (org.junit.Test)4 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTVariableInitializer (net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer)2 Comparator (java.util.Comparator)1 Map (java.util.Map)1 RuleContext (net.sourceforge.pmd.RuleContext)1 QualifiableNode (net.sourceforge.pmd.lang.ast.QualifiableNode)1 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)1