1   /***
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package test.net.sourceforge.pmd.testframework;
5   
6   import junit.framework.TestCase;
7   import net.sourceforge.pmd.TargetJDK1_4;
8   import net.sourceforge.pmd.TargetJDKVersion;
9   import net.sourceforge.pmd.ast.ASTCompilationUnit;
10  import net.sourceforge.pmd.ast.JavaParser;
11  import net.sourceforge.pmd.ast.JavaParserVisitor;
12  import net.sourceforge.pmd.dfa.DataFlowFacade;
13  import net.sourceforge.pmd.symboltable.SymbolFacade;
14  
15  import java.io.StringReader;
16  import java.lang.reflect.InvocationHandler;
17  import java.lang.reflect.Method;
18  import java.lang.reflect.Proxy;
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.HashSet;
22  import java.util.List;
23  import java.util.Set;
24  
25  public class ParserTst extends TestCase {
26  
27      private class Collector implements InvocationHandler {
28          private Class clazz = null;
29          private Collection collection;
30  
31          public Collector(Class clazz) {
32              this(clazz, new HashSet());
33          }
34  
35          public Collector(Class clazz, Collection coll) {
36              this.clazz = clazz;
37              this.collection = coll;
38          }
39  
40          public Collection getCollection() {
41              return collection;
42          }
43  
44          public Object invoke(Object proxy, Method method, Object params[]) throws Throwable {
45              if (method.getName().equals("visit")) {
46                  if (clazz.isInstance(params[0])) {
47                      collection.add(params[0]);
48                  }
49              }
50  
51              Method childrenAccept = params[0].getClass().getMethod("childrenAccept", new Class[]{JavaParserVisitor.class, Object.class});
52              childrenAccept.invoke(params[0], new Object[]{proxy, null});
53              return null;
54          }
55      }
56  
57      public Set getNodes(Class clazz, String javaCode) throws Throwable {
58          return getNodes(new TargetJDK1_4(), clazz, javaCode);
59      }
60  
61      public Set getNodes(TargetJDKVersion jdk, Class clazz, String javaCode) throws Throwable {
62          Collector coll = new Collector(clazz);
63          JavaParser parser = jdk.createParser(new StringReader(javaCode));
64          ASTCompilationUnit cu = parser.CompilationUnit();
65          JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, coll);
66          jpv.visit(cu, null);
67          return (Set) coll.getCollection();
68      }
69  
70      public List getOrderedNodes(Class clazz, String javaCode) throws Throwable {
71          Collector coll = new Collector(clazz, new ArrayList());
72          JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(javaCode));
73          ASTCompilationUnit cu = parser.CompilationUnit();
74          JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, coll);
75          jpv.visit(cu, null);
76          SymbolFacade sf = new SymbolFacade();
77          sf.initializeWith(cu);
78          DataFlowFacade dff = new DataFlowFacade();
79          dff.initializeWith(cu);
80          return (List) coll.getCollection();
81      }
82  
83      public ASTCompilationUnit buildDFA(String javaCode) throws Throwable {
84          JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(javaCode));
85          ASTCompilationUnit cu = parser.CompilationUnit();
86          JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, new Collector(ASTCompilationUnit.class));
87          jpv.visit(cu, null);
88          new SymbolFacade().initializeWith(cu);
89          new DataFlowFacade().initializeWith(cu);
90          return cu;
91      }
92  }