View Javadoc
1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.plsql;
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.LanguageRegistry;
17  import net.sourceforge.pmd.lang.LanguageVersion;
18  import net.sourceforge.pmd.lang.LanguageVersionHandler;
19  import net.sourceforge.pmd.lang.ast.Node;
20  // Root Production comprising PLSQL definitions, and SQL*PLus, DDL, GRANTS etc.
21  import net.sourceforge.pmd.lang.plsql.ast.ASTInput;
22  //Covers all executbale code units, such as package and object type bodies, standalone procedures and functions, and triggers 
23  import net.sourceforge.pmd.lang.plsql.ast.PLSQLParserVisitor;
24  import net.sourceforge.pmd.lang.plsql.dfa.DataFlowFacade;
25  import net.sourceforge.pmd.lang.plsql.symboltable.SymbolFacade;
26  
27  public abstract class AbstractPLSQLParserTst {
28  
29      private class Collector<E> implements InvocationHandler {
30          private Class<E> clazz = null;
31          private Collection<E> collection;
32  
33          public Collector(Class<E> clazz) {
34              this(clazz, new HashSet<E>());
35          }
36  
37          public Collector(Class<E> clazz, Collection<E> coll) {
38              this.clazz = clazz;
39              this.collection = coll;
40          }
41  
42          public Collection<E> getCollection() {
43              return collection;
44          }
45  
46          public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
47              if (method.getName().equals("visit")) {
48                  if (clazz.isInstance(params[0])) {
49                      collection.add((E) params[0]);
50                  }
51              }
52  
53              Method childrenAccept = params[0].getClass().getMethod("childrenAccept", new Class[]{PLSQLParserVisitor.class, Object.class});
54              childrenAccept.invoke(params[0], new Object[]{proxy, null});
55              return null;
56          }
57      }
58  
59      public <E> Set<E> getNodes(Class<E> clazz, String plsqlCode) throws Throwable {
60          return getNodes(LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME).getDefaultVersion(), clazz, plsqlCode);
61      }
62  
63      public <E> Set<E> getNodes(LanguageVersion languageVersion, Class<E> clazz, String plsqlCode) throws Throwable {
64          Collector<E> coll = new Collector<E>(clazz);
65          LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
66  	ASTInput cu = (ASTInput)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(plsqlCode));
67          PLSQLParserVisitor jpv = (PLSQLParserVisitor) Proxy.newProxyInstance(PLSQLParserVisitor.class.getClassLoader(), new Class[]{PLSQLParserVisitor.class}, coll);
68          jpv.visit(cu, null);
69          return (Set<E>) coll.getCollection();
70      }
71  
72      public <E> List<E> getOrderedNodes(Class<E> clazz, String plsqlCode) throws Throwable {
73          Collector<E> coll = new Collector<E>(clazz, new ArrayList<E>());
74          LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
75          ASTInput cu = (ASTInput)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(plsqlCode));
76          PLSQLParserVisitor jpv = (PLSQLParserVisitor) Proxy.newProxyInstance(PLSQLParserVisitor.class.getClassLoader(), new Class[]{PLSQLParserVisitor.class}, coll);
77          jpv.visit(cu, null);
78          SymbolFacade sf = new SymbolFacade();
79          sf.initializeWith(cu);
80          DataFlowFacade dff = new DataFlowFacade();
81          dff.initializeWith(languageVersionHandler.getDataFlowHandler(), cu);
82          return (List<E>) coll.getCollection();
83      }
84  
85      public <E> String dumpNodes(List<E> list ) throws Throwable {
86  	    StringBuilder sb = new StringBuilder () ;
87  	    int index = 0;
88  	    for (E item : list) {
89  		    sb.append("\n node[").append(index).append(item.toString());
90  		    index ++;
91  	  }
92  	  return sb.toString();
93      }
94         
95      public ASTInput buildDFA(String plsqlCode) throws Throwable {
96          LanguageVersionHandler languageVersionHandler = LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME).getDefaultVersion().getLanguageVersionHandler();
97  	ASTInput cu = (ASTInput)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(plsqlCode));
98          PLSQLParserVisitor jpv = (PLSQLParserVisitor) Proxy.newProxyInstance(PLSQLParserVisitor.class.getClassLoader(), new Class[]{PLSQLParserVisitor.class}, new Collector<ASTInput>(ASTInput.class));
99          jpv.visit(cu, null);
100         new SymbolFacade().initializeWith(cu);
101         new DataFlowFacade().initializeWith(languageVersionHandler.getDataFlowHandler(), cu);
102         return cu;
103     }
104     
105     public ASTInput parsePLSQL(LanguageVersion languageVersion, String code) {
106         LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
107 	return (ASTInput)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
108     }
109     
110     public ASTInput parsePLSQL(String code) {
111     	return parsePLSQL(LanguageRegistry.getLanguage(PLSQLLanguageModule.NAME).getDefaultVersion(), code);
112     }
113     
114     public Node parseLanguage(LanguageVersion languageVersion, String code) {
115         LanguageVersionHandler languageVersionHandler = languageVersion.getLanguageVersionHandler();
116 	return (Node)languageVersionHandler.getParser(languageVersionHandler.getDefaultParserOptions()).parse(null, new StringReader(code));
117     }
118 }