Search in sources :

Example 6 with ASTLiteral

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

the class InvalidSlf4jMessageFormatRule method countPlaceholders.

private int countPlaceholders(final AbstractJavaTypeNode node) {
    // zero means, no placeholders, or we could not analyze the message parameter
    int result = 0;
    ASTLiteral stringLiteral = node.getFirstDescendantOfType(ASTLiteral.class);
    if (stringLiteral != null) {
        result = StringUtils.countMatches(stringLiteral.getImage(), "{}");
    }
    return result;
}
Also used : ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral)

Example 7 with ASTLiteral

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

the class AvoidDuplicateLiteralsRule method processResults.

private void processResults(Object data) {
    int threshold = getProperty(THRESHOLD_DESCRIPTOR);
    for (Map.Entry<String, List<ASTLiteral>> entry : literals.entrySet()) {
        List<ASTLiteral> occurrences = entry.getValue();
        if (occurrences.size() >= threshold) {
            ASTLiteral first = occurrences.get(0);
            String rawImage = first.getEscapedStringLiteral();
            Object[] args = new Object[] { rawImage, Integer.valueOf(occurrences.size()), Integer.valueOf(first.getBeginLine()) };
            addViolation(data, first, args);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) HashMap(java.util.HashMap) Map(java.util.Map)

Example 8 with ASTLiteral

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

the class BigIntegerInstantiationRule method visit.

@Override
public Object visit(ASTAllocationExpression node, Object data) {
    Node type = node.jjtGetChild(0);
    if (!(type instanceof ASTClassOrInterfaceType)) {
        return super.visit(node, data);
    }
    boolean jdk15 = ((RuleContext) data).getLanguageVersion().compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5")) >= 0;
    if ((TypeHelper.isA((ASTClassOrInterfaceType) type, BigInteger.class) || jdk15 && TypeHelper.isA((ASTClassOrInterfaceType) type, BigDecimal.class)) && !node.hasDescendantOfType(ASTArrayDimsAndInits.class)) {
        ASTArguments args = node.getFirstChildOfType(ASTArguments.class);
        if (args.getArgumentCount() == 1) {
            ASTLiteral literal = node.getFirstDescendantOfType(ASTLiteral.class);
            if (literal == null || literal.jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent().jjtGetParent() != args) {
                return super.visit(node, data);
            }
            String img = literal.getImage();
            if (literal.isStringLiteral()) {
                img = img.substring(1, img.length() - 1);
            }
            if ("0".equals(img) || "1".equals(img) || jdk15 && "10".equals(img)) {
                addViolation(data, node);
                return data;
            }
        }
    }
    return super.visit(node, data);
}
Also used : RuleContext(net.sourceforge.pmd.RuleContext) Node(net.sourceforge.pmd.lang.ast.Node) BigInteger(java.math.BigInteger) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTLiteral(net.sourceforge.pmd.lang.java.ast.ASTLiteral) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 9 with ASTLiteral

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

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

the class ConsecutiveLiteralAppendsRule method checkConstructor.

/**
 * Determine if the constructor contains (or ends with) a String Literal
 *
 * @param node
 * @return 1 if the constructor contains string argument, else 0
 */
private int checkConstructor(ASTVariableDeclaratorId node, Object data) {
    Node parent = node.jjtGetParent();
    if (parent.jjtGetNumChildren() >= 2) {
        ASTAllocationExpression allocationExpression = parent.jjtGetChild(1).getFirstDescendantOfType(ASTAllocationExpression.class);
        ASTArgumentList list = null;
        if (allocationExpression != null) {
            list = allocationExpression.getFirstDescendantOfType(ASTArgumentList.class);
        }
        if (list != null) {
            ASTLiteral literal = list.getFirstDescendantOfType(ASTLiteral.class);
            if (!isAdditive(list) && literal != null && literal.isStringLiteral()) {
                return 1;
            }
            return processAdditive(data, 0, list, node);
        }
    }
    return 0;
}
Also used : TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) 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