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

This page was automatically generated by Maven