Search in sources :

Example 31 with DataFlowNode

use of net.sourceforge.pmd.lang.dfa.DataFlowNode in project pmd by pmd.

the class StatementAndBraceFinderTest method testWhileStmtHasCorrectTypes.

@Test
public void testWhileStmtHasCorrectTypes() {
    ASTExpression exp = getOrderedNodes(ASTExpression.class, TEST4).get(0);
    DataFlowNode dfn = exp.getDataFlowNode().getFlow().get(2);
    assertTrue(dfn.isType(NodeType.WHILE_EXPR));
    dfn = exp.getDataFlowNode().getFlow().get(3);
    assertTrue(dfn.isType(NodeType.WHILE_LAST_STATEMENT));
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) ASTExpression(net.sourceforge.pmd.lang.plsql.ast.ASTExpression) Test(org.junit.Test)

Example 32 with DataFlowNode

use of net.sourceforge.pmd.lang.dfa.DataFlowNode in project pmd by pmd.

the class StatementAndBraceFinderTest method testIfStmtHasCorrectTypes.

@Test
public void testIfStmtHasCorrectTypes() {
    ASTExpression exp = getOrderedNodes(ASTExpression.class, TEST3).get(0);
    assertEquals(5, exp.getDataFlowNode().getFlow().size());
    DataFlowNode dfn = exp.getDataFlowNode().getFlow().get(2);
    assertTrue(dfn.isType(NodeType.IF_EXPR));
    assertEquals(3, dfn.getLine());
    dfn = exp.getDataFlowNode().getFlow().get(3);
    assertTrue(dfn.isType(NodeType.IF_LAST_STATEMENT_WITHOUT_ELSE));
    assertEquals(3, dfn.getLine());
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) ASTExpression(net.sourceforge.pmd.lang.plsql.ast.ASTExpression) Test(org.junit.Test)

Example 33 with DataFlowNode

use of net.sourceforge.pmd.lang.dfa.DataFlowNode in project pmd by pmd.

the class StatementAndBraceFinderTest method testExpressionParentChildLinks.

/**
 * Java ASTStatementExpression equivalent is inferred as an Expression()
 * which has an UnlabelledStatement as a parent.
 */
@Test
public void testExpressionParentChildLinks() {
    ASTExpression ex = getOrderedNodes(ASTExpression.class, TEST1).get(0);
    DataFlowNode dfn = ex.getDataFlowNode();
    assertEquals(3, dfn.getLine());
    assertTrue(dfn.getNode() instanceof ASTExpression);
    List<DataFlowNode> dfns = dfn.getParents();
    assertEquals(1, dfns.size());
    DataFlowNode parentDfn = dfns.get(0);
    assertEquals(2, parentDfn.getLine());
    assertTrue(parentDfn.getNode() instanceof ASTProgramUnit);
    ASTProgramUnit exParent = (ASTProgramUnit) parentDfn.getNode();
    // Validate the two-way link between Program Unit and Statement
    assertEquals(ex, exParent.getDataFlowNode().getChildren().get(0).getNode());
    assertEquals(exParent, ex.getDataFlowNode().getParents().get(0).getNode());
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) StartOrEndDataFlowNode(net.sourceforge.pmd.lang.dfa.StartOrEndDataFlowNode) ASTProgramUnit(net.sourceforge.pmd.lang.plsql.ast.ASTProgramUnit) ASTExpression(net.sourceforge.pmd.lang.plsql.ast.ASTExpression) Test(org.junit.Test)

Example 34 with DataFlowNode

use of net.sourceforge.pmd.lang.dfa.DataFlowNode in project pmd-eclipse-plugin by pmd.

the class DataflowGraph method findPath.

/**
 * Recursively finds a Path from the starting Node to the ending Node, the
 * visited-List contains Paths, that already have been visited, so the
 * Function does not produce Loops, the List should be a new ArrayList()
 * when calling this Function
 *
 * @param start
 * @param end
 * @param visited
 * @return an List of PathCanvas, that build up the path from Start-Node to
 *         End-Node or null, if there no such path could be found
 */
protected List<PathCanvas> findPath(DataFlowNode start, DataFlowNode end, List<DataFlowNode> visited) {
    // we return the Path from the current Node to the End
    if (start.getChildren().contains(end)) {
        List<PathCanvas> found = new ArrayList<PathCanvas>();
        PathCanvas path = getPath(start.getIndex(), end.getIndex());
        if (path != null) {
            found.add(path);
            return found;
        }
    } else {
        // this is the Search
        for (DataFlowNode node : start.getChildren()) {
            // here we avoid Loops by checking the visited nodes
            if (visited.contains(node)) {
                continue;
            }
            // ... and adding the current Node
            visited.add(node);
            // the Recursion: find the Path from
            // the current Node's children to the End
            List<PathCanvas> isFound = findPath(node, end, visited);
            if (isFound == null) {
                continue;
            } else {
                // if a Path (from child to end) is found
                // we can add the Path from this Node to the child
                PathCanvas path2 = isFound.get(0);
                PathCanvas path1 = getPath(start.getIndex(), path2.getIndex1());
                if (path1 != null) {
                    isFound.add(0, path1);
                    return isFound;
                }
            }
        }
    }
    return null;
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) ArrayList(java.util.ArrayList)

Example 35 with DataFlowNode

use of net.sourceforge.pmd.lang.dfa.DataFlowNode in project pmd-eclipse-plugin by pmd.

the class DataflowGraph method markPath.

/**
 * Marks a Path from the given first line to the second line <br>
 * Given are two Lines in the Text and a Variable, where an anomaly has
 * occurred, the method colors the nodes, that lie at the lines and
 * calculates and also colors _one possible_ Path between them.
 *
 * @param line1
 * @param line2
 * @param varName
 */
public void markPath(int line1, int line2, String varName) {
    if (nodes == null || paths == null) {
        return;
    }
    // twist the Lines if needed
    if (line1 > line2) {
        int temp = line1;
        line1 = line2;
        line2 = temp;
    }
    // an Anomaly can have multiple starting points
    // but - so we say here - only one ending point
    List<DataFlowNode> startNodes = new ArrayList<DataFlowNode>();
    DataFlowNode endNode = null;
    for (NodeCanvas node : nodes) {
        if (!node.containsVariable(varName)) {
            node.mark(false, null);
            continue;
        }
        if (node.getLine() == line1) {
            // if a Node is set at the given
            // first Line we color it and add it
            // as starting Node
            node.mark(true, markColor);
            startNodes.add(node.getINode());
        } else if (node.getLine() == line2) {
            // ... else, if we found a Node at the ending Line, we mark it
            // and set it as ending Node
            node.mark(true, markColor);
            endNode = node.getINode();
        } else {
            node.mark(false, null);
        }
    }
    for (PathCanvas deMarkedPath : paths) {
        // we then clear all Paths
        deMarkedPath.mark(false, null);
    }
    // ... to mark some of them again
    List<PathCanvas> pathsToMark = new ArrayList<PathCanvas>();
    for (DataFlowNode start : startNodes) {
        // from every starting Node we search for a Path to the ending node
        List<PathCanvas> pathList = findPath(start, endNode, new ArrayList<DataFlowNode>());
        if (pathList == null) {
            continue;
        }
        // we get a List of PathCanvas, that build up the searched Path
        for (PathCanvas currentPath : pathList) {
            // set to mark, we don't want to mark them again
            if (!pathsToMark.contains(currentPath)) {
                pathsToMark.add(currentPath);
            }
        }
    }
    // now we have a clear List of Paths that we can color
    for (int m = 0; m < pathsToMark.size(); m++) {
        PathCanvas markedPath = pathsToMark.get(m);
        markedPath.mark(true, markColor);
        // so we can see them as "stopovers" (like 2,3,...,12)
        if (m < pathsToMark.size() - 1) {
            NodeCanvas markedNode = getNode(markedPath.getIndex2());
            if (!markedNode.isMarked()) {
                markedNode.mark(true, markColor2);
            }
        }
    }
    redraw();
    marked = true;
}
Also used : DataFlowNode(net.sourceforge.pmd.lang.dfa.DataFlowNode) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point)

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