Search in sources :

Example 1 with ASTLiteral

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

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

the class InsufficientStringBufferDeclarationRule method getInitialLength.

private int getInitialLength(Node node) {
    Node block = node.getFirstParentOfType(ASTBlockStatement.class);
    if (block == null) {
        block = node.getFirstParentOfType(ASTFieldDeclaration.class);
        if (block == null) {
            block = node.getFirstParentOfType(ASTFormalParameter.class);
        }
    }
    List<ASTLiteral> literals = block.findDescendantsOfType(ASTLiteral.class);
    if (literals.size() == 1) {
        ASTLiteral literal = literals.get(0);
        String str = literal.getImage();
        if (str != null && isStringOrCharLiteral(literal)) {
            // take off the quotes
            return str.length() - 2;
        }
    }
    return 0;
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 3 with ASTLiteral

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

the class InsufficientStringBufferDeclarationRule method processNode.

private int processNode(Node sn) {
    int anticipatedLength = 0;
    if (sn != null) {
        ASTPrimaryPrefix xn = sn.getFirstDescendantOfType(ASTPrimaryPrefix.class);
        if (xn != null) {
            if (xn.jjtGetNumChildren() != 0 && xn.jjtGetChild(0) instanceof ASTLiteral) {
                ASTLiteral literal = (ASTLiteral) xn.jjtGetChild(0);
                String str = xn.jjtGetChild(0).getImage();
                if (str != null) {
                    if (literal.isStringLiteral()) {
                        anticipatedLength += str.length() - 2;
                    } else if (literal.isCharLiteral()) {
                        anticipatedLength += 1;
                    } else if (literal.isIntLiteral()) {
                        // but only if we are not inside a cast expression
                        Node parentNode = literal.jjtGetParent().jjtGetParent().jjtGetParent();
                        if (parentNode instanceof ASTCastExpression && ((ASTCastExpression) parentNode).getType() == char.class) {
                            anticipatedLength += 1;
                        } else {
                            // any number, regardless of the base will be converted to base 10
                            // e.g. 0xdeadbeef -> will be converted to a
                            // base 10 integer string: 3735928559
                            anticipatedLength += String.valueOf(literal.getValueAsLong()).length();
                        }
                    } else {
                        anticipatedLength += str.length();
                    }
                }
            }
        }
    }
    return anticipatedLength;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) Node(net.sourceforge.pmd.lang.ast.Node) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 4 with ASTLiteral

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

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

the class ConstructorCallsOverridableMethodRule method getArgumentTypes.

private static List<String> getArgumentTypes(ASTArguments args) {
    List<String> argumentTypes = new ArrayList<>();
    ASTArgumentList argumentList = args.getFirstChildOfType(ASTArgumentList.class);
    if (argumentList != null) {
        for (int a = 0; a < argumentList.jjtGetNumChildren(); a++) {
            Node expression = argumentList.jjtGetChild(a);
            ASTPrimaryPrefix arg = expression.getFirstDescendantOfType(ASTPrimaryPrefix.class);
            String type = "<unknown>";
            if (arg != null && arg.jjtGetNumChildren() > 0) {
                if (arg.jjtGetChild(0) instanceof ASTLiteral) {
                    ASTLiteral lit = (ASTLiteral) arg.jjtGetChild(0);
                    if (lit.isCharLiteral()) {
                        type = "char";
                    } else if (lit.isFloatLiteral()) {
                        type = "float";
                    } else if (lit.isIntLiteral()) {
                        type = "int";
                    } else if (lit.isStringLiteral()) {
                        type = "String";
                    } else if (lit.jjtGetNumChildren() > 0 && lit.jjtGetChild(0) instanceof ASTBooleanLiteral) {
                        type = "boolean";
                    } else if (lit.isDoubleLiteral()) {
                        type = "double";
                    } else if (lit.isLongLiteral()) {
                        type = "long";
                    }
                } else if (arg.jjtGetChild(0) instanceof ASTName) {
                    // ASTName n = (ASTName)arg.jjtGetChild(0);
                    type = "ref";
                }
            }
            argumentTypes.add(type);
        }
    }
    return argumentTypes;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTBooleanLiteral(net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ArrayList(java.util.ArrayList) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Aggregations

ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)18 Node (net.sourceforge.pmd.lang.ast.Node)13 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ASTAdditiveExpression (net.sourceforge.pmd.lang.java.ast.ASTAdditiveExpression)4 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)4 ArrayList (java.util.ArrayList)3 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)3 ASTBooleanLiteral (net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral)3 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)3 TypeNode (net.sourceforge.pmd.lang.java.ast.TypeNode)3 List (java.util.List)2 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTFieldDeclaration (net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)2 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 BigInteger (java.math.BigInteger)1