use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.
the class VariableAccessVisitor method markUsages.
private List<VariableAccess> markUsages(DataFlowNode inode) {
// undefinitions was once a field... seems like it works fine as a local
List<VariableAccess> undefinitions = new ArrayList<>();
Set<Map<VariableNameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
for (Map<VariableNameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
VariableNameDeclaration vnd = entry.getKey();
if (vnd.getAccessNodeParent() instanceof ASTFormalParameter) {
// no definition/undefinition/references for parameters
continue;
} else if (vnd.getAccessNodeParent().getFirstDescendantOfType(ASTVariableInitializer.class) != null) {
// add definition for initialized variables
addVariableAccess(vnd.getNode(), new VariableAccess(VariableAccess.DEFINITION, vnd.getImage()), inode.getFlow());
}
undefinitions.add(new VariableAccess(VariableAccess.UNDEFINITION, vnd.getImage()));
for (NameOccurrence occurrence : entry.getValue()) {
addAccess((JavaNameOccurrence) occurrence, inode);
}
}
}
return undefinitions;
}
use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.
the class CheckSkipResultRule method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
ASTType typeNode = node.getTypeNode();
if (typeNode == null || !TypeHelper.isA(typeNode, InputStream.class)) {
return data;
}
for (NameOccurrence occ : node.getUsages()) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
NameOccurrence qualifier = jocc.getNameForWhichThisIsAQualifier();
if (qualifier != null && "skip".equals(qualifier.getImage())) {
Node loc = jocc.getLocation();
if (loc != null) {
ASTPrimaryExpression exp = loc.getFirstParentOfType(ASTPrimaryExpression.class);
while (exp != null) {
if (exp.jjtGetParent() instanceof ASTStatementExpression) {
// if exp is in a bare statement,
// the returned value is not used
addViolation(data, occ.getLocation());
break;
} else if (exp.jjtGetParent() instanceof ASTExpression && exp.jjtGetParent().jjtGetParent() instanceof ASTPrimaryPrefix) {
// if exp is enclosed in a pair of parenthesis
// let's have a look at the enclosing expression
// we'll see if it's in a bare statement
exp = exp.getFirstParentOfType(ASTPrimaryExpression.class);
} else {
// or assignement so the returned value is used
break;
}
}
}
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.
the class ImmutableFieldRule method initializedInConstructor.
private FieldImmutabilityType initializedInConstructor(List<NameOccurrence> usages, Set<ASTConstructorDeclaration> allConstructors) {
FieldImmutabilityType result = FieldImmutabilityType.MUTABLE;
int methodInitCount = 0;
int lambdaUsage = 0;
// set of constructors accessing the field
Set<ASTConstructorDeclaration> consSet = new HashSet<>();
for (NameOccurrence occ : usages) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
if (jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) {
Node node = jocc.getLocation();
ASTConstructorDeclaration constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
if (constructor != null) {
if (inLoopOrTry(node)) {
continue;
}
// in one constructor only
if (node.getFirstParentOfType(ASTIfStatement.class) != null) {
methodInitCount++;
}
if (inAnonymousInnerClass(node)) {
methodInitCount++;
} else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
lambdaUsage++;
} else {
consSet.add(constructor);
}
} else {
if (node.getFirstParentOfType(ASTMethodDeclaration.class) != null) {
methodInitCount++;
} else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
lambdaUsage++;
}
}
}
}
if (usages.isEmpty() || methodInitCount == 0 && lambdaUsage == 0 && consSet.isEmpty()) {
result = FieldImmutabilityType.CHECKDECL;
} else {
allConstructors.removeAll(consSet);
if (allConstructors.isEmpty() && methodInitCount == 0 && lambdaUsage == 0) {
result = FieldImmutabilityType.IMMUTABLE;
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.
the class AvoidReassigningParametersRule method lookForViolation.
private void lookForViolation(Map<VariableNameDeclaration, List<NameOccurrence>> params, Object data) {
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : params.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
List<NameOccurrence> usages = entry.getValue();
for (NameOccurrence occ : usages) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
if ((jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) && jocc.getNameForWhichThisIsAQualifier() == null && !jocc.useThisOrSuper() && !decl.isVarargs() && (!decl.isArray() || jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetNumChildren() == 1)) {
// not an array or no primary suffix to access the array
// values
addViolation(data, decl.getNode(), decl.getImage());
}
}
}
}
use of net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence in project pmd by pmd.
the class AbstractInefficientZeroCheck method visit.
@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
Node nameNode = node.getTypeNameNode();
if (nameNode == null || nameNode instanceof ASTPrimitiveType || !appliesToClassName(node.getNameDeclaration().getTypeImage())) {
return data;
}
List<NameOccurrence> declars = node.getUsages();
for (NameOccurrence occ : declars) {
JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
if (!isTargetMethod(jocc)) {
continue;
}
Node expr = jocc.getLocation().jjtGetParent().jjtGetParent().jjtGetParent();
checkNodeAndReport(data, jocc.getLocation(), expr);
}
return data;
}
Aggregations