View Javadoc

    1   package net.sourceforge.pmd.dfa;
    2
    3   import java.util.Iterator;
    4   import java.util.List;
    5
    6   import net.sourceforge.pmd.PMD;
    7   import net.sourceforge.pmd.lang.dfa.DataFlowNode;
    8   import net.sourceforge.pmd.lang.dfa.VariableAccess;
    9   import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
    10  import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator;
    11  import net.sourceforge.pmd.testframework.ParserTst;
    12
    13  import org.junit.Test;
    14
    15
    16  public class GeneralFiddlingTest extends ParserTst {
    17
    18      @Test
    19      public void test1() throws Throwable {
    20          ASTCompilationUnit acu = buildDFA(TEST1);
    21          ASTMethodDeclarator meth = acu.findDescendantsOfType(ASTMethodDeclarator.class).get(0);
    22          DataFlowNode n = meth.getDataFlowNode();
    23          List f = n.getFlow();
    24          for (Iterator i = f.iterator(); i.hasNext();) {
    25              DataFlowNode dfan = (DataFlowNode) i.next();
    26              System.out.println(dfan);
    27              List va = dfan.getVariableAccess();
    28              for (Iterator j = va.iterator(); j.hasNext();) {
    29                  VariableAccess o = (VariableAccess) j.next();
    30                  System.out.println(o);
    31              }
    32          }
    33      }
    34
    35      private static final String TEST1 =
    36              "class Foo {" + PMD.EOL +
    37              " void bar() {" + PMD.EOL +
    38              "  int x = 2;" + PMD.EOL +
    39              "  foo(x);" + PMD.EOL +
    40              "  x = 3;" + PMD.EOL +
    41              " }" + PMD.EOL +
    42              "}";
    43
    44      public static junit.framework.Test suite() {
    45          return new junit.framework.JUnit4TestAdapter(GeneralFiddlingTest.class);
    46      }
    47  }