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          return (List<E>) coll.getCollection();
    80      }
    81
    82      public ASTCompilationUnit buildDFA(String javaCode) throws Throwable {
    83          LanguageVersionHandler languageVersionHandler = Language.JAVA.getDefaultVersion().getLanguageVersionHandler();
    84  	ASTCompilationUnit cu = (ASTCompilationUnit)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(javaCode));
    85          JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, new Collector<ASTCompilationUnit>(ASTCompilationUnit.class));
    86          jpv.visit(cu, null);
    87          new SymbolFacade().initializeWith(cu);
    88          new DataFlowFacade().initializeWith(languageVersionHandler.getDataFlowHandler(), cu);
    89          return cu;
    90      }
    91
    92      public ASTCompilationUnit parseJava13(String code) {
    93          return parseJava(LanguageVersion.JAVA_13, code);
    94      }
    95      public ASTCompilationUnit parseJava14(String code) {
    96          return parseJava(LanguageVersion.JAVA_14, code);
    97      }
    98      public ASTCompilationUnit parseJava15(String code) {
    99          return parseJava(LanguageVersion.JAVA_15, code);
    100     }
    101     public ASTCompilationUnit parseJava17(String code) {
    102         return parseJava(LanguageVersion.JAVA_17, code);
    103     }
    104     public ASTCompilationUnit parseJava(LanguageVersion languageVersion, String code) {
    105         LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
    106 	return (ASTCompilationUnit)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
    107     }
    108 }