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;
}
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);
}
}
}
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);
}
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;
}
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;
}
Aggregations