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