View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.testframework;
5   
6   import java.io.StringReader;
7   import java.lang.reflect.InvocationHandler;
8   import java.lang.reflect.Method;
9   import java.lang.reflect.Proxy;
10  import java.util.ArrayList;
11  import java.util.Collection;
12  import java.util.HashSet;
13  import java.util.List;
14  import java.util.Set;
15  
16  import net.sourceforge.pmd.lang.Language;
17  import net.sourceforge.pmd.lang.LanguageVersion;
18  import net.sourceforge.pmd.lang.LanguageVersionHandler;
19  import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
20  import net.sourceforge.pmd.lang.java.ast.JavaParserVisitor;
21  import net.sourceforge.pmd.lang.java.dfa.DataFlowFacade;
22  import net.sourceforge.pmd.lang.java.symboltable.SymbolFacade;
23  
24  public abstract class ParserTst {
25  
26      private class Collector<E> implements InvocationHandler {
27          private Class<E> clazz = null;
28          private Collection<E> collection;
29  
30          public Collector(Class<E> clazz) {
31              this(clazz, new HashSet<E>());
32          }
33  
34          public Collector(Class<E> clazz, Collection<E> coll) {
35              this.clazz = clazz;
36              this.collection = coll;
37          }
38  
39          public Collection<E> getCollection() {
40              return collection;
41          }
42  
43          public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
44              if (method.getName().equals("visit")) {
45                  if (clazz.isInstance(params[0])) {
46                      collection.add((E) params[0]);
47                  }
48              }
49  
50              Method childrenAccept = params[0].getClass().getMethod("childrenAccept", new Class[]{JavaParserVisitor.class, Object.class});
51              childrenAccept.invoke(params[0], new Object[]{proxy, null});
52              return null;
53          }
54      }
55  
56      public <E> Set<E> getNodes(Class<E> clazz, String javaCode) throws Throwable {
57          return getNodes(Language.JAVA.getDefaultVersion(), clazz, javaCode);
58      }
59  
60      public <E> Set<E> getNodes(LanguageVersion languageVersion, Class<E> clazz, String javaCode) throws Throwable {
61          Collector<E> coll = new Collector<E>(clazz);
62          LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
63  	ASTCompilationUnit cu = (ASTCompilationUnit)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(javaCode));
64          JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, coll);
65          jpv.visit(cu, null);
66          return (Set<E>) coll.getCollection();
67      }
68  
69      public <E> List<E> getOrderedNodes(Class<E> clazz, String javaCode) throws Throwable {
70          Collector<E> coll = new Collector<E>(clazz, new ArrayList<E>());
71          LanguageVersionHandler languageVersionHandler = Language.JAVA.getDefaultVersion().getLanguageVersionHandler();
72          ASTCompilationUnit cu = (ASTCompilationUnit)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(javaCode));
73          JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, coll);
74          jpv.visit(cu, null);
75          SymbolFacade sf = new SymbolFacade();
76          sf.initializeWith(cu);
77          DataFlowFacade dff = new DataFlowFacade();
78          dff.initializeWith(languageVersionHandler.getDataFlowHandler(), cu);
79  
80          return (List<E>) coll.getCollection();
81      }
82      
83      public <E> String dumpNodes(List<E> list ) throws Throwable {
84  	    StringBuilder sb = new StringBuilder () ;
85  	    int index = 0;
86  	    for (E item : list) {
87  		    sb.append("\n node[").append(index).append(item.toString());
88  		    index ++;
89  	  }
90  	  return sb.toString();
91      }
92  
93      public ASTCompilationUnit buildDFA(String javaCode) throws Throwable {
94          LanguageVersionHandler languageVersionHandler = Language.JAVA.getDefaultVersion().getLanguageVersionHandler();
95  	ASTCompilationUnit cu = (ASTCompilationUnit)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(javaCode));
96          JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, new Collector<ASTCompilationUnit>(ASTCompilationUnit.class));
97          jpv.visit(cu, null);
98          new SymbolFacade().initializeWith(cu);
99          new DataFlowFacade().initializeWith(languageVersionHandler.getDataFlowHandler(), cu);
100         return cu;
101     }
102     
103     public ASTCompilationUnit parseJava13(String code) {
104         return parseJava(LanguageVersion.JAVA_13, code);
105     }
106     public ASTCompilationUnit parseJava14(String code) {
107         return parseJava(LanguageVersion.JAVA_14, code);
108     }
109     public ASTCompilationUnit parseJava15(String code) {
110         return parseJava(LanguageVersion.JAVA_15, code);
111     }
112     public ASTCompilationUnit parseJava17(String code) {
113         return parseJava(LanguageVersion.JAVA_17, code);
114     }
115     public ASTCompilationUnit parseJava18(String code) {
116         return parseJava(LanguageVersion.JAVA_18, code);
117     }
118     public ASTCompilationUnit parseJava(LanguageVersion languageVersion, String code) {
119         LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
120         ASTCompilationUnit rootNode = (ASTCompilationUnit)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
121         languageVersionHandler.getSymbolFacade().start(rootNode);
122         return rootNode;
123     }
124 }