1 /***
2 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3 */
4 package test.net.sourceforge.pmd.ast;
5
6 import junit.framework.TestCase;
7 import net.sourceforge.pmd.ast.ASTCompilationUnit;
8 import net.sourceforge.pmd.ast.JavaParser;
9 import net.sourceforge.pmd.ast.JavaParserVisitor;
10
11 import java.io.StringReader;
12 import java.lang.reflect.InvocationHandler;
13 import java.lang.reflect.Method;
14 import java.lang.reflect.Proxy;
15 import java.util.HashSet;
16 import java.util.Set;
17
18 public class ParserTst extends TestCase {
19
20 private class Collector implements InvocationHandler {
21 private Class clazz = null;
22 private Set collection = new HashSet();
23
24 public Collector(Class clazz) {
25 this.clazz = clazz;
26 }
27
28 public Set getCollection() {
29 return collection;
30 }
31
32 public Object invoke(Object proxy, Method method, Object params[]) throws Throwable {
33 if (method.getName().equals("visit")) {
34 if (clazz.isInstance(params[0])) {
35 collection.add(params[0]);
36 }
37 }
38
39 Method childrenAccept = params[0].getClass().getMethod("childrenAccept", new Class[]{JavaParserVisitor.class, Object.class});
40 childrenAccept.invoke(params[0], new Object[]{proxy, null});
41 return null;
42 }
43 }
44
45 public Set getNodes(Class clazz, String javaCode) throws Throwable {
46 Collector coll = new Collector(clazz);
47 JavaParser parser = new JavaParser(new StringReader(javaCode));
48 ASTCompilationUnit cu = parser.CompilationUnit();
49 JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, coll);
50 jpv.visit(cu, null);
51 return coll.getCollection();
52 }
53 }
This page was automatically generated by Maven