Search in sources :

Example 16 with DataFlowNode

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);
        }
    }
}
Also used : HashMap(java.util.HashMap) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess)

Example 17 with DataFlowNode

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);
        }
    }
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Example 18 with DataFlowNode

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;
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 19 with DataFlowNode

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;
}
Also used : StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence) HashSet(java.util.HashSet) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode)

Example 20 with DataFlowNode

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;
            }
        }
    }
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) Node(net.sourceforge.pmd.lang.ast.Node) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) VariableAccess(net.sourceforge.pmd.lang.dfa.VariableAccess) ArrayList(java.util.ArrayList)

Aggregations

DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)35 StartOrEndDataFlowNode (net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode)17 Test (org.junit.Test)15 ArrayList (java.util.ArrayList)7 VariableAccess (net.sourceforge.pmd.lang.dfa.VariableAccess)6 ASTExpression (net.sourceforge.pmd.lang.plsql.ast.ASTExpression)6 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)4 List (java.util.List)3 Node (net.sourceforge.pmd.lang.ast.Node)3 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)3 ASTProgramUnit (net.sourceforge.pmd.lang.plsql.ast.ASTProgramUnit)3 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)2 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)2 PLSQLNode (net.sourceforge.pmd.lang.plsql.ast.PLSQLNode)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2 Point (org.eclipse.swt.graphics.Point)2 DAAPathFinder (net.sourceforge.pmd.lang.dfa.pathfinder.DAAPathFinder)1