use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.
the class ForLoopCanBeForeachRule method visit.
@Override
public Object visit(ASTForStatement node, Object data) {
final ASTForInit init = node.getFirstChildOfType(ASTForInit.class);
final ASTForUpdate update = node.getFirstChildOfType(ASTForUpdate.class);
final ASTExpression guardCondition = node.getFirstChildOfType(ASTExpression.class);
if (init == null && update == null || guardCondition == null) {
return data;
}
Entry<VariableNameDeclaration, List<NameOccurrence>> indexDecl = getIndexVarDeclaration(init, update);
if (indexDecl == null) {
return data;
}
List<NameOccurrence> occurrences = indexDecl.getValue();
VariableNameDeclaration index = indexDecl.getKey();
if (TypeHelper.isA(index, Iterator.class)) {
Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = getIterableDeclOfIteratorLoop(index, node.getScope());
if (iterableInfo != null && isReplaceableIteratorLoop(indexDecl, guardCondition, iterableInfo, node)) {
addViolation(data, node);
}
return data;
}
if (occurrences == null || !"int".equals(index.getTypeImage()) || !indexStartsAtZero(index)) {
return data;
}
String itName = index.getName();
String iterableName = getIterableNameOrNullToAbort(guardCondition, itName);
if (!isForUpdateSimpleEnough(update, itName) || iterableName == null) {
return data;
}
Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = findDeclaration(iterableName, node.getScope());
VariableNameDeclaration iterableDeclaration = iterableInfo == null ? null : iterableInfo.getKey();
if (iterableDeclaration == null) {
return data;
}
if (iterableDeclaration.isArray() && isReplaceableArrayLoop(node, occurrences, iterableDeclaration)) {
addViolation(data, node);
} else if (iterableDeclaration.getTypeImage() != null && iterableDeclaration.getTypeImage().matches("List|ArrayList|LinkedList") && isReplaceableListLoop(node, occurrences, iterableDeclaration)) {
addViolation(data, node);
}
return data;
}
use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.
the class ForLoopCanBeForeachRule method getIndexVarDeclaration.
/* Finds the declaration of the index variable and its occurrences, null to abort */
private Entry<VariableNameDeclaration, List<NameOccurrence>> getIndexVarDeclaration(ASTForInit init, ASTForUpdate update) {
if (init == null) {
return guessIndexVarFromUpdate(update);
}
ASTLocalVariableDeclaration decl = init.getFirstChildOfType(ASTLocalVariableDeclaration.class);
if (decl == null) {
return null;
}
int numDeclaredVars = decl.findChildrenOfType(ASTVariableDeclarator.class).size();
if (numDeclaredVars > 1) {
// will abort in the calling function
return null;
}
Map<VariableNameDeclaration, List<NameOccurrence>> decls = init.getScope().getDeclarations(VariableNameDeclaration.class);
Entry<VariableNameDeclaration, List<NameOccurrence>> indexVarAndOccurrences = null;
for (Entry<VariableNameDeclaration, List<NameOccurrence>> e : decls.entrySet()) {
ASTForInit declInit = e.getKey().getNode().getFirstParentOfType(ASTForInit.class);
if (Objects.equals(declInit, init)) {
indexVarAndOccurrences = e;
break;
}
}
return indexVarAndOccurrences;
}
use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.
the class VariableAccessVisitor method collectDeclarations.
private Set<Map<VariableNameDeclaration, List<NameOccurrence>>> collectDeclarations(DataFlowNode inode) {
Set<Map<VariableNameDeclaration, List<NameOccurrence>>> decls = new HashSet<>();
Map<VariableNameDeclaration, List<NameOccurrence>> varDecls;
for (int i = 0; i < inode.getFlow().size(); i++) {
DataFlowNode n = inode.getFlow().get(i);
if (n instanceof StartOrEndDataFlowNode) {
continue;
}
varDecls = ((JavaNode) n.getNode()).getScope().getDeclarations(VariableNameDeclaration.class);
if (!decls.contains(varDecls)) {
decls.add(varDecls);
}
}
return decls;
}
use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.
the class UselessStringValueOfRule method isPrimitive.
private static boolean isPrimitive(Node parent) {
boolean result = false;
if (parent instanceof ASTPrimaryExpression && parent.jjtGetNumChildren() == 1) {
Node child = parent.jjtGetChild(0);
if (child instanceof ASTPrimaryPrefix && child.jjtGetNumChildren() == 1) {
Node gc = child.jjtGetChild(0);
if (gc instanceof ASTName) {
ASTName name = (ASTName) gc;
NameDeclaration nd = name.getNameDeclaration();
if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
result = true;
}
} else if (gc instanceof ASTLiteral) {
result = !((ASTLiteral) gc).isStringLiteral();
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration 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;
}
Aggregations