use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix 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.ASTPrimarySuffix 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.ASTPrimarySuffix 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.ASTPrimarySuffix 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;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class ConsecutiveLiteralAppendsRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!isStringBuffer(node)) {
return data;
}
threshold = getProperty(THRESHOLD_DESCRIPTOR);
int concurrentCount = checkConstructor(node, data);
if (hasInitializer(node)) {
concurrentCount += checkInitializerExpressions(node);
}
Node lastBlock = getFirstParentBlock(node);
Node currentBlock = lastBlock;
Map<VariableNameDeclaration, List<NameOccurrence>> decls = node.getScope().getDeclarations(VariableNameDeclaration.class);
Node rootNode = null;
// only want the constructor flagged if it's really containing strings
if (concurrentCount >= 1) {
rootNode = node;
}
for (List<NameOccurrence> decl : decls.values()) {
for (NameOccurrence no : decl) {
JavaNameOccurrence jno = (JavaNameOccurrence) no;
Node n = jno.getLocation();
// variable
if (!node.getImage().equals(jno.getImage())) {
continue;
}
currentBlock = getFirstParentBlock(n);
if (!InefficientStringBufferingRule.isInStringBufferOperation(n, 3, "append")) {
if (!jno.isPartOfQualifiedName()) {
checkForViolation(rootNode, data, concurrentCount);
concurrentCount = 0;
}
continue;
}
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;
}
// see if it changed blocks
if (currentBlock != null && lastBlock != null && !currentBlock.equals(lastBlock) || currentBlock == null ^ lastBlock == null) {
checkForViolation(rootNode, data, concurrentCount);
concurrentCount = 0;
}
// here
if (concurrentCount == 0) {
rootNode = sn;
}
if (isAdditive(sn)) {
concurrentCount = processAdditive(data, concurrentCount, sn, rootNode);
if (concurrentCount != 0) {
rootNode = sn;
}
} else if (!isAppendingStringLiteral(sn)) {
checkForViolation(rootNode, data, concurrentCount);
concurrentCount = 0;
} else {
concurrentCount++;
}
lastBlock = currentBlock;
}
}
}
checkForViolation(rootNode, data, concurrentCount);
return data;
}
Aggregations