Search in sources :

Example 1 with ASTMultiplicativeExpression

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

Aggregations

Node (net.sourceforge.pmd.lang.ast.Node)1 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)1 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)1 ASTMultiplicativeExpression (net.sourceforge.pmd.lang.java.ast.ASTMultiplicativeExpression)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1