use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.
the class LocalScope method addNameOccurrence.
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occurrence) {
JavaNameOccurrence javaOccurrence = (JavaNameOccurrence) occurrence;
Set<NameDeclaration> declarations = findVariableHere(javaOccurrence);
if (!declarations.isEmpty() && !javaOccurrence.isThisOrSuper()) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
nameOccurrences.add(javaOccurrence);
Node n = javaOccurrence.getLocation();
if (n instanceof ASTName) {
((ASTName) n).setNameDeclaration(decl);
}
// TODO what to do with PrimarySuffix case?
}
}
return declarations;
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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.symboltable.NameOccurrence in project pmd by pmd.
the class StringToStringRule method visit.
public Object visit(ASTVariableDeclaratorId node, Object data) {
if (!TypeHelper.isA(node.getNameDeclaration(), String.class) && !TypeHelper.isA(node.getNameDeclaration(), String[].class)) {
return data;
}
boolean isArray = node.isArray();
for (NameOccurrence occ : node.getUsages()) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
if (qualifier != null) {
if (!isArray && isNotAMethodReference(qualifier) && qualifier.getImage().indexOf("toString") != -1) {
addViolation(data, jocc.getLocation());
} else if (isArray && isNotAName(qualifier) && qualifier.getImage().equals("toString")) {
addViolation(data, jocc.getLocation());
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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.symboltable.NameOccurrence in project pmd by pmd.
the class UnsynchronizedStaticDateFormatterRule method visit.
@Override
public Object visit(ASTFieldDeclaration node, Object data) {
if (!node.isStatic()) {
return data;
}
ASTClassOrInterfaceType cit = node.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
if (cit == null || !targets.contains(cit.getImage())) {
return data;
}
ASTVariableDeclaratorId var = node.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
for (NameOccurrence occ : var.getUsages()) {
Node n = occ.getLocation();
if (n.getFirstParentOfType(ASTSynchronizedStatement.class) != null) {
continue;
}
// ignore usages, that don't call a method.
if (!n.getImage().contains(".")) {
continue;
}
ASTMethodDeclaration method = n.getFirstParentOfType(ASTMethodDeclaration.class);
if (method != null && !method.isSynchronized()) {
addViolation(data, n);
}
}
return data;
}
Aggregations