Search in sources :

Example 1 with ASTAdditiveExpression

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

the class InsufficientStringBufferDeclarationRule method processAdditive.

private int processAdditive(Node sn) {
    ASTAdditiveExpression additive = sn.getFirstDescendantOfType(ASTAdditiveExpression.class);
    if (additive == null) {
        return 0;
    }
    int anticipatedLength = 0;
    for (int ix = 0; ix < additive.jjtGetNumChildren(); ix++) {
        Node childNode = additive.jjtGetChild(ix);
        ASTLiteral literal = childNode.getFirstDescendantOfType(ASTLiteral.class);
        if (literal != null && literal.getImage() != null) {
            anticipatedLength += literal.getImage().length() - 2;
        }
    }
    return anticipatedLength;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 2 with ASTAdditiveExpression

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

the class ConsecutiveLiteralAppendsRule method processAdditive.

private int processAdditive(Object data, int concurrentCount, Node sn, Node rootNode) {
    ASTAdditiveExpression additive = sn.getFirstDescendantOfType(ASTAdditiveExpression.class);
    // The additive expression must of be type String to count
    if (additive == null || additive.getType() != null && !TypeHelper.isA(additive, String.class)) {
        return 0;
    }
    // check for at least one string literal
    List<ASTLiteral> literals = additive.findDescendantsOfType(ASTLiteral.class);
    boolean stringLiteralFound = false;
    for (ASTLiteral l : literals) {
        if (l.isCharLiteral() || l.isStringLiteral()) {
            stringLiteralFound = true;
            break;
        }
    }
    if (!stringLiteralFound) {
        return 0;
    }
    int count = concurrentCount;
    boolean found = false;
    for (int ix = 0; ix < additive.jjtGetNumChildren(); ix++) {
        Node childNode = additive.jjtGetChild(ix);
        if (childNode.jjtGetNumChildren() != 1 || childNode.hasDescendantOfType(ASTName.class)) {
            if (!found) {
                checkForViolation(rootNode, data, count);
                found = true;
            }
            count = 0;
        } else {
            count++;
        }
    }
    // string concats, we really only have 1 then
    if (!found) {
        count = 1;
    }
    return count;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 3 with ASTAdditiveExpression

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

the class UselessStringValueOfRule method visit.

@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
    if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
        return super.visit(node, data);
    }
    String image = ((ASTName) node.jjtGetChild(0)).getImage();
    if ("String.valueOf".equals(image)) {
        Node parent = node.jjtGetParent();
        if (parent.jjtGetNumChildren() != 2) {
            return super.visit(node, data);
        }
        // skip String.valueOf(anyarraytype[])
        ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
        if (args != null) {
            ASTName arg = args.getFirstDescendantOfType(ASTName.class);
            if (arg != null) {
                NameDeclaration declaration = arg.getNameDeclaration();
                if (declaration != null) {
                    ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
                    if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) {
                        return super.visit(node, data);
                    }
                }
            }
        }
        Node gp = parent.jjtGetParent();
        if (parent instanceof ASTPrimaryExpression && gp instanceof ASTAdditiveExpression && "+".equals(gp.getImage())) {
            boolean ok = false;
            if (gp.jjtGetChild(0) == parent) {
                ok = !isPrimitive(gp.jjtGetChild(1));
            } else {
                for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
                    ok = !isPrimitive(gp.jjtGetChild(i));
                }
            }
            if (ok) {
                super.addViolation(data, node);
                return data;
            }
        }
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 4 with ASTAdditiveExpression

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

the class InefficientStringBufferingRule method visit.

@Override
public Object visit(ASTAdditiveExpression node, Object data) {
    ASTBlockStatement bs = node.getFirstParentOfType(ASTBlockStatement.class);
    if (bs == null) {
        return data;
    }
    int immediateLiterals = 0;
    int immediateStringLiterals = 0;
    List<ASTLiteral> nodes = node.findDescendantsOfType(ASTLiteral.class);
    for (ASTLiteral literal : nodes) {
        if (literal.getNthParent(3) instanceof ASTAdditiveExpression) {
            immediateLiterals++;
            if (literal.isStringLiteral()) {
                immediateStringLiterals++;
            }
        }
        if (literal.isIntLiteral() || literal.isFloatLiteral() || literal.isDoubleLiteral() || literal.isLongLiteral()) {
            return data;
        }
    }
    if (immediateLiterals > 1) {
        return data;
    }
    // if literal + public static final, return
    List<ASTName> nameNodes = node.findDescendantsOfType(ASTName.class);
    for (ASTName name : nameNodes) {
        if (name.getNameDeclaration() != null && name.getNameDeclaration() instanceof VariableNameDeclaration) {
            VariableNameDeclaration vnd = (VariableNameDeclaration) name.getNameDeclaration();
            AccessNode accessNodeParent = vnd.getAccessNodeParent();
            if (accessNodeParent.isFinal() && accessNodeParent.isStatic()) {
                return data;
            }
        }
    }
    // if literal primitive type and not strings variables, then return
    boolean stringFound = false;
    for (ASTName name : nameNodes) {
        if (!isPrimitiveType(name) && isStringType(name)) {
            stringFound = true;
            break;
        }
    }
    if (!stringFound && immediateStringLiterals == 0) {
        return data;
    }
    if (bs.isAllocation()) {
        for (Iterator<ASTName> iterator = nameNodes.iterator(); iterator.hasNext(); ) {
            ASTName name = iterator.next();
            if (!name.getImage().endsWith("length")) {
                break;
            } else if (!iterator.hasNext()) {
                // All names end with length
                return data;
            }
        }
        if (isAllocatedStringBuffer(node)) {
            addViolation(data, node);
        }
    } else if (isInStringBufferOperation(node, 6, "append")) {
        addViolation(data, node);
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTAdditiveExpression(net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 5 with ASTAdditiveExpression

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

ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)5 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)4 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)4 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTMultiplicativeExpression (net.sourceforge.pmd.lang.java.ast.ASTMultiplicativeExpression)1 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)1 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)1 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)1 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)1 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)1 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)1