use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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.symboltable.NameOccurrence 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<NameDeclaration, List<NameOccurrence>>> variableDeclarations = collectDeclarations(inode);
for (Map<NameDeclaration, List<NameOccurrence>> declarations : variableDeclarations) {
for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : declarations.entrySet()) {
NameDeclaration vnd = entry.getKey();
if (vnd.getNode().jjtGetParent() instanceof ASTFormalParameter) {
// no definition/undefinition/references for parameters
continue;
} else if (vnd.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableOrConstantInitializer.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(occurrence, inode);
}
}
}
return undefinitions;
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence in project pmd by pmd.
the class LocalScope method addNameOccurrence.
@Override
public Set<NameDeclaration> addNameOccurrence(NameOccurrence occ) {
PLSQLNameOccurrence occurrence = (PLSQLNameOccurrence) occ;
Set<NameDeclaration> declarations = findVariableHere(occurrence);
if (!declarations.isEmpty() && !occurrence.isThisOrSuper()) {
for (NameDeclaration decl : declarations) {
List<NameOccurrence> nameOccurrences = getVariableDeclarations().get(decl);
nameOccurrences.add(occurrence);
Node n = occurrence.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 NameFinder method toString.
@Override
public String toString() {
StringBuilder result = new StringBuilder();
for (NameOccurrence occ : names) {
result.append(occ);
result.append(PMD.EOL);
}
return result.toString();
}
use of net.sourceforge.pmd.lang.symboltable.NameOccurrence 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());
}
}
}
}
Aggregations