use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd by pmd.
the class UselessAssignment method execute.
public void execute(CurrentPath path) {
Map<String, Usage> hash = new HashMap<>();
// System.out.println("path size is " + path.size());
for (Iterator<DataFlowNode> i = path.iterator(); i.hasNext(); ) {
// System.out.println("i = " + i);
DataFlowNode inode = i.next();
if (inode.getVariableAccess() == null) {
continue;
}
for (int j = 0; j < inode.getVariableAccess().size(); j++) {
VariableAccess va = inode.getVariableAccess().get(j);
// System.out.println("inode = " + inode + ", va = " + va);
Usage u = hash.get(va.getVariableName());
if (u != null) {
// FIXME need to check for assignment as well!
if (va.isDefinition() && va.accessTypeMatches(u.accessType)) {
// System.out.println(va.getVariableName() + ":" + u);
addViolation(rc, u.node.getNode(), va.getVariableName());
}
/*
* // UR - ?? else if (last == VariableAccess.UNDEFINITION
* && va.isReference()) {
* //this.rc.getReport().addRuleViolation(
* createRuleViolation(rc, inode.getNode(),
* va.getVariableName(), "UR")); } // DU - variable is
* defined and then goes out of scope // i.e., unused
* parameter else if (last == VariableAccess.DEFINITION &&
* va.isUndefinition()) { if (inode.getNode() != null) {
* this.rc.getReport().addRuleViolation(createRuleViolation(
* rc, tmp, va.getVariableName(), "DU")); } }
*/
}
u = new Usage(va.getAccessType(), inode);
hash.put(va.getVariableName(), u);
}
}
}
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 computeNow.
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 VariableAccessTest method testGetVariableName.
@Test
public void testGetVariableName() {
VariableAccess va = new VariableAccess(VariableAccess.DEFINITION, "foo.bar");
assertEquals("foo", va.getVariableName());
va = new VariableAccess(VariableAccess.DEFINITION, ".foobar");
assertEquals("", va.getVariableName());
va = new VariableAccess(VariableAccess.DEFINITION, "foobar.");
assertEquals("foobar", va.getVariableName());
va = new VariableAccess(VariableAccess.DEFINITION, "foobar");
assertEquals("foobar", va.getVariableName());
}
use of net.sourceforge.pmd.lang.dfa.VariableAccess in project pmd-eclipse-plugin by pmd.
the class DataflowGraphViewer method referenceStringFrom.
private String referenceStringFrom(DataFlowNode dfNode) {
List<VariableAccess> access = dfNode.getVariableAccess();
if (access == null) {
return null;
}
StringBuilder exp = new StringBuilder();
for (int k = 0; k < access.size(); k++) {
if (k > 0) {
exp.append(", ");
}
VariableAccess va = access.get(k);
switch(va.getAccessType()) {
case VariableAccess.DEFINITION:
exp.append("d(");
break;
case VariableAccess.REFERENCING:
exp.append("r(");
break;
case VariableAccess.UNDEFINITION:
exp.append("u(");
break;
default:
exp.append("?(");
}
exp.append(va.getVariableName()).append(')');
}
return exp.toString();
}
Aggregations