use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class InsufficientStringBufferDeclarationRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isEither(node.getNameDeclaration(), StringBuffer.class, StringBuilder.class)) {
return data;
}
Node rootNode = node;
int anticipatedLength = 0;
int constructorLength = DEFAULT_BUFFER_SIZE;
constructorLength = getConstructorLength(node, constructorLength);
anticipatedLength = getInitialLength(node);
anticipatedLength += getConstructorAppendsLength(node);
List<NameOccurrence> usage = node.getUsages();
Map<Node, Map<Node, Integer>> blocks = new HashMap<>();
for (NameOccurrence no : usage) {
JavaNameOccurrence jno = (JavaNameOccurrence) no;
Node n = jno.getLocation();
if (!InefficientStringBufferingRule.isInStringBufferOperation(n, 3, "append")) {
if (!jno.isOnLeftHandSide() && !InefficientStringBufferingRule.isInStringBufferOperation(n, 3, "setLength")) {
continue;
}
if (constructorLength != -1 && anticipatedLength > constructorLength) {
anticipatedLength += processBlocks(blocks);
String[] param = { String.valueOf(constructorLength), String.valueOf(anticipatedLength) };
addViolation(data, rootNode, param);
}
constructorLength = getConstructorLength(n, constructorLength);
rootNode = n;
anticipatedLength = getInitialLength(node);
}
ASTPrimaryExpression s = n.getFirstParentOfType(ASTPrimaryExpression.class);
int numChildren = s.jjtGetNumChildren();
for (int jx = 0; jx < numChildren; jx++) {
Node sn = s.jjtGetChild(jx);
if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) {
continue;
}
int thisSize = 0;
Node block = getFirstParentBlock(sn);
if (isAdditive(sn)) {
thisSize = processAdditive(sn);
} else {
thisSize = processNode(sn);
}
if (block != null) {
storeBlockStatistics(blocks, thisSize, block);
} else {
anticipatedLength += thisSize;
}
}
}
anticipatedLength += processBlocks(blocks);
if (constructorLength != -1 && anticipatedLength > constructorLength) {
String[] param = { String.valueOf(constructorLength), String.valueOf(anticipatedLength) };
addViolation(data, rootNode, param);
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class UseStringBufferForStringAppendsRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isA(node, String.class) || node.isArray()) {
return data;
}
Node parent = node.jjtGetParent().jjtGetParent();
if (!(parent instanceof ASTLocalVariableDeclaration)) {
return data;
}
for (NameOccurrence no : node.getUsages()) {
Node name = no.getLocation();
ASTStatementExpression statement = name.getFirstParentOfType(ASTStatementExpression.class);
if (statement == null) {
continue;
}
ASTArgumentList argList = name.getFirstParentOfType(ASTArgumentList.class);
if (argList != null && argList.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// used in method call
continue;
}
ASTEqualityExpression equality = name.getFirstParentOfType(ASTEqualityExpression.class);
if (equality != null && equality.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// used in condition
continue;
}
ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class);
if (conditional != null) {
Node thirdParent = name.getNthParent(3);
Node fourthParent = name.getNthParent(4);
if ((Objects.equals(thirdParent, conditional) || Objects.equals(fourthParent, conditional)) && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
// string)
continue;
}
}
if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
if (astName != null) {
if (astName.equals(name)) {
ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
if (assignmentOperator != null && assignmentOperator.isCompound()) {
addViolation(data, assignmentOperator);
}
} else if (astName.getImage().equals(name.getImage())) {
ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
if (assignmentOperator != null && !assignmentOperator.isCompound()) {
addViolation(data, astName);
}
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class NonThreadSafeSingletonRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
if (checkNonStaticMethods && !node.isStatic() || node.isSynchronized()) {
return super.visit(node, data);
}
List<ASTIfStatement> ifStatements = node.findDescendantsOfType(ASTIfStatement.class);
for (ASTIfStatement ifStatement : ifStatements) {
if (ifStatement.getFirstParentOfType(ASTSynchronizedStatement.class) == null) {
if (!ifStatement.hasDescendantOfType(ASTNullLiteral.class)) {
continue;
}
ASTName n = ifStatement.getFirstDescendantOfType(ASTName.class);
if (n == null || !fieldDecls.containsKey(n.getImage())) {
continue;
}
List<ASTAssignmentOperator> assigmnents = ifStatement.findDescendantsOfType(ASTAssignmentOperator.class);
boolean violation = false;
for (int ix = 0; ix < assigmnents.size(); ix++) {
ASTAssignmentOperator oper = assigmnents.get(ix);
if (!(oper.jjtGetParent() instanceof ASTStatementExpression)) {
continue;
}
ASTStatementExpression expr = (ASTStatementExpression) oper.jjtGetParent();
if (expr.jjtGetChild(0) instanceof ASTPrimaryExpression && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetNumChildren() == 1 && ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0) instanceof ASTPrimaryPrefix) {
ASTPrimaryPrefix pp = (ASTPrimaryPrefix) ((ASTPrimaryExpression) expr.jjtGetChild(0)).jjtGetChild(0);
String name = null;
if (pp.usesThisModifier()) {
ASTPrimarySuffix priSuf = expr.getFirstDescendantOfType(ASTPrimarySuffix.class);
name = priSuf.getImage();
} else {
ASTName astName = (ASTName) pp.jjtGetChild(0);
name = astName.getImage();
}
if (fieldDecls.containsKey(name)) {
violation = true;
}
}
}
if (violation) {
addViolation(data, ifStatement);
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class BooleanInstantiationRule method visit.
@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
if (!customBoolean) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}
if ("Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage()) || "java.lang.Boolean.valueOf".equals(((ASTName) node.jjtGetChild(0)).getImage())) {
ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
ASTPrimarySuffix suffix = parent.getFirstDescendantOfType(ASTPrimarySuffix.class);
if (suffix == null) {
return super.visit(node, data);
}
ASTPrimaryPrefix prefix = suffix.getFirstDescendantOfType(ASTPrimaryPrefix.class);
if (prefix == null) {
return super.visit(node, data);
}
if (prefix.hasDescendantOfType(ASTBooleanLiteral.class)) {
super.addViolation(data, node);
return data;
}
ASTLiteral literal = prefix.getFirstDescendantOfType(ASTLiteral.class);
if (literal != null && ("\"true\"".equals(literal.getImage()) || "\"false\"".equals(literal.getImage()))) {
super.addViolation(data, node);
return data;
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.
the class ConsecutiveLiteralAppendsRule method checkInitializerExpressions.
/**
* Determine if during the variable initializer calls to ".append" are done.
*
* @param node
* @return
*/
private int checkInitializerExpressions(ASTVariableDeclaratorId node) {
ASTVariableInitializer initializer = node.jjtGetParent().getFirstChildOfType(ASTVariableInitializer.class);
ASTPrimaryExpression primary = initializer.getFirstDescendantOfType(ASTPrimaryExpression.class);
int result = 0;
boolean previousWasAppend = false;
for (int i = 0; i < primary.jjtGetNumChildren(); i++) {
Node child = primary.jjtGetChild(i);
if (child.jjtGetNumChildren() > 0 && child.jjtGetChild(0) instanceof ASTAllocationExpression) {
// skip the constructor call, that has already been checked
continue;
}
if (child instanceof ASTPrimarySuffix) {
ASTPrimarySuffix suffix = (ASTPrimarySuffix) child;
if (suffix.jjtGetNumChildren() == 0 && suffix.hasImageEqualTo("append")) {
previousWasAppend = true;
} else if (suffix.jjtGetNumChildren() > 0 && previousWasAppend) {
previousWasAppend = false;
ASTLiteral literal = suffix.getFirstDescendantOfType(ASTLiteral.class);
if (literal != null && literal.isStringLiteral()) {
result++;
} else {
// checking the remainder of the initializer
break;
}
}
}
}
return result;
}
Aggregations