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