use of net.sourceforge.pmd.lang.dfa.DataFlowNode 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.DataFlowNode in project pmd-eclipse-plugin by pmd.
the class DataflowGraph method createDataflowGraph.
/**
* Builds the DataflowGraph out of the given SimpleNode
*
* @param node
*/
private void createDataflowGraph(Node node) {
List<DataFlowNode> flow = node.getDataFlowNode().getFlow();
// every Node has children, for which we can build Paths
for (int i = 0; i < flow.size(); i++) {
DataFlowNode inode = flow.get(i);
// create a new Node and add it to the List
Point location = new Point((getSize().x - 2 * nodeRadius) / 2, i * rowHeight + lineLength / 2);
NodeCanvas nod = new NodeCanvas(this, inode, location, nodeRadius);
nodes.add(nod);
// get the Nodes children and build Paths between them
List<DataFlowNode> children = inode.getChildren();
for (DataFlowNode dfNode : children) {
// create a new Path and add it to the List
int x = (getSize().x - 2 * nodeRadius) / 2;
int y1 = inode.getIndex() * rowHeight + lineLength / 2;
int y2 = dfNode.getIndex() * rowHeight + lineLength / 2;
PathCanvas path = new PathCanvas(this, inode.getIndex(), dfNode.getIndex(), x, y1, y2, nodeRadius);
paths.add(path);
}
}
}
use of net.sourceforge.pmd.lang.dfa.DataFlowNode in project pmd-eclipse-plugin by pmd.
the class DataflowGraphViewer method createDataFields.
/**
* Creates an List (#Rows) of List (#Columns) with TableData in it, provides
* the Input for the Table
*
* @param node
* @return the DataflowGraphTable's Input-List
*/
protected List<List<DataflowGraphTableData>> createDataFields(Node node) {
List<DataFlowNode> flow = node.getDataFlowNode().getFlow();
// the whole TableData
List<List<DataflowGraphTableData>> tableData = new ArrayList<List<DataflowGraphTableData>>();
for (DataFlowNode inode : flow) {
// one Data-List for a row
List<DataflowGraphTableData> rowData = new ArrayList<DataflowGraphTableData>();
// 1. The Nodes Line
rowData.add(new DataflowGraphTableData(String.valueOf(inode.getLine()), SWT.CENTER));
// 2. empty, because the Graph is shown in this column
rowData.add(null);
// 3. the Numbers of the next Nodes
String cellContent = nextNodeNumberStringFrom(inode);
rowData.add(new DataflowGraphTableData(cellContent, SWT.LEFT | SWT.WRAP));
// 4. The Dataflow occurrences (definition, undefinition, reference)
// in this Line of Code
cellContent = referenceStringFrom(inode);
if (cellContent != null) {
rowData.add(new DataflowGraphTableData(cellContent, SWT.LEFT | SWT.WRAP));
} else {
rowData.add(null);
}
// 5. The Line of Code itself
if (resourceString != null) {
cellContent = getCodeLine(resourceString, inode.getLine()).trim();
rowData.add(new DataflowGraphTableData(cellContent, SWT.LEFT | SWT.WRAP));
} else {
rowData.add(null);
}
tableData.add(rowData);
}
return tableData;
}
use of net.sourceforge.pmd.lang.dfa.DataFlowNode 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.dfa.DataFlowNode 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;
}
}
}
}
Aggregations