001: /**
002: * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
003: */package test.net.sourceforge.pmd.testframework;
004:
005: import net.sourceforge.pmd.TargetJDKVersion;
006: import net.sourceforge.pmd.ast.ASTCompilationUnit;
007: import net.sourceforge.pmd.ast.JavaParser;
008: import net.sourceforge.pmd.ast.JavaParserVisitor;
009: import net.sourceforge.pmd.dfa.DataFlowFacade;
010: import net.sourceforge.pmd.symboltable.SymbolFacade;
011:
012: import java.io.StringReader;
013: import java.lang.reflect.InvocationHandler;
014: import java.lang.reflect.Method;
015: import java.lang.reflect.Proxy;
016: import java.util.ArrayList;
017: import java.util.Collection;
018: import java.util.HashSet;
019: import java.util.List;
020: import java.util.Set;
021:
022: public abstract class ParserTst {
023:
024: private class Collector<E> implements InvocationHandler {
025: private Class<E> clazz = null;
026: private Collection<E> collection;
027:
028: public Collector(Class<E> clazz) {
029: this (clazz, new HashSet<E>());
030: }
031:
032: public Collector(Class<E> clazz, Collection<E> coll) {
033: this .clazz = clazz;
034: this .collection = coll;
035: }
036:
037: public Collection<E> getCollection() {
038: return collection;
039: }
040:
041: public Object invoke(Object proxy, Method method,
042: Object params[]) throws Throwable {
043: if (method.getName().equals("visit")) {
044: if (clazz.isInstance(params[0])) {
045: collection.add((E) params[0]);
046: }
047: }
048:
049: Method childrenAccept = params[0].getClass()
050: .getMethod(
051: "childrenAccept",
052: new Class[] { JavaParserVisitor.class,
053: Object.class });
054: childrenAccept.invoke(params[0],
055: new Object[] { proxy, null });
056: return null;
057: }
058: }
059:
060: public <E> Set<E> getNodes(Class<E> clazz, String javaCode)
061: throws Throwable {
062: return getNodes(TargetJDKVersion.DEFAULT_JDK_VERSION, clazz,
063: javaCode);
064: }
065:
066: public <E> Set<E> getNodes(TargetJDKVersion jdk, Class<E> clazz,
067: String javaCode) throws Throwable {
068: Collector<E> coll = new Collector<E>(clazz);
069: JavaParser parser = jdk
070: .createParser(new StringReader(javaCode));
071: ASTCompilationUnit cu = parser.CompilationUnit();
072: JavaParserVisitor jpv = (JavaParserVisitor) Proxy
073: .newProxyInstance(JavaParserVisitor.class
074: .getClassLoader(),
075: new Class[] { JavaParserVisitor.class }, coll);
076: jpv.visit(cu, null);
077: return (Set<E>) coll.getCollection();
078: }
079:
080: public <E> List<E> getOrderedNodes(Class<E> clazz, String javaCode)
081: throws Throwable {
082: Collector<E> coll = new Collector<E>(clazz, new ArrayList<E>());
083: JavaParser parser = TargetJDKVersion.DEFAULT_JDK_VERSION
084: .createParser(new StringReader(javaCode));
085: ASTCompilationUnit cu = parser.CompilationUnit();
086: JavaParserVisitor jpv = (JavaParserVisitor) Proxy
087: .newProxyInstance(JavaParserVisitor.class
088: .getClassLoader(),
089: new Class[] { JavaParserVisitor.class }, coll);
090: jpv.visit(cu, null);
091: SymbolFacade sf = new SymbolFacade();
092: sf.initializeWith(cu);
093: DataFlowFacade dff = new DataFlowFacade();
094: dff.initializeWith(cu);
095: return (List<E>) coll.getCollection();
096: }
097:
098: public ASTCompilationUnit buildDFA(String javaCode)
099: throws Throwable {
100: JavaParser parser = TargetJDKVersion.DEFAULT_JDK_VERSION
101: .createParser(new StringReader(javaCode));
102: ASTCompilationUnit cu = parser.CompilationUnit();
103: JavaParserVisitor jpv = (JavaParserVisitor) Proxy
104: .newProxyInstance(JavaParserVisitor.class
105: .getClassLoader(),
106: new Class[] { JavaParserVisitor.class },
107: new Collector<ASTCompilationUnit>(
108: ASTCompilationUnit.class));
109: jpv.visit(cu, null);
110: new SymbolFacade().initializeWith(cu);
111: new DataFlowFacade().initializeWith(cu);
112: return cu;
113: }
114: }
|