use of net.sourceforge.pmd.lang.dfa.VariableAccess 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.dfa.VariableAccess in project pmd by pmd.
the class DataflowAnomalyAnalysisRule method execute.
public void execute(CurrentPath path) {
if (maxNumberOfViolationsReached()) {
return;
}
Map<String, Usage> usagesByVarName = new HashMap<>();
for (DataFlowNode inode : path) {
if (inode.getVariableAccess() != null) {
// iterate all variables of this node
for (VariableAccess va : inode.getVariableAccess()) {
// get the last usage of the current variable
Usage lastUsage = usagesByVarName.get(va.getVariableName());
if (lastUsage != null) {
// there was a usage to this variable before
checkVariableAccess(inode, va, lastUsage);
}
Usage newUsage = new Usage(va.getAccessType(), inode);
// put the new usage for the variable
usagesByVarName.put(va.getVariableName(), newUsage);
}
}
}
}
use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.
the class VariableAccessVisitor method computeNow.
/*
* SRT public void compute(ASTConstructorDeclaration node) {
* this.computeNow(node); }
*/
private void computeNow(Node node) {
DataFlowNode inode = node.getDataFlowNode();
List<VariableAccess> undefinitions = markUsages(inode);
// all variables are first in state undefinition
DataFlowNode firstINode = inode.getFlow().get(0);
firstINode.setVariableAccess(undefinitions);
// all variables are getting undefined when leaving scope
DataFlowNode lastINode = inode.getFlow().get(inode.getFlow().size() - 1);
lastINode.setVariableAccess(undefinitions);
}
use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.
the class VariableAccessVisitor method addVariableAccess.
/**
* Adds a VariableAccess to a dataflow node.
*
* @param node
* location of the access of a variable
* @param va
* variable access to add
* @param flow
* dataflownodes that can contain the node.
*/
private void addVariableAccess(Node node, VariableAccess va, List<DataFlowNode> flow) {
// backwards to find the right inode (not a method declaration)
for (int i = flow.size() - 1; i > 0; i--) {
DataFlowNode inode = flow.get(i);
if (inode.getNode() == null) {
continue;
}
List<? extends Node> children = inode.getNode().findDescendantsOfType(node.getClass());
for (Node n : children) {
if (node.equals(n)) {
List<VariableAccess> v = new ArrayList<>();
v.add(va);
inode.setVariableAccess(v);
return;
}
}
}
}
use of net.sourceforge.pmd.lang.dfa.VariableAccess 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;
}
Aggregations