⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parsertst.java

📁 检查Java程序漏洞
💻 JAVA
字号:
/** * BSD-style license; for more info see http://pmd.sourceforge.net/license.html*/package test.net.sourceforge.pmd.ast;import junit.framework.TestCase;import net.sourceforge.pmd.TargetJDK1_4;import net.sourceforge.pmd.ast.ASTCompilationUnit;import net.sourceforge.pmd.ast.JavaParser;import net.sourceforge.pmd.ast.JavaParserVisitor;import java.io.StringReader;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.HashSet;import java.util.Set;public class ParserTst extends TestCase {    private class Collector implements InvocationHandler {        private Class clazz = null;        private Set collection = new HashSet();        public Collector(Class clazz) {            this.clazz = clazz;        }        public Set getCollection() {            return collection;        }        public Object invoke(Object proxy, Method method, Object params[]) throws Throwable {            if (method.getName().equals("visit")) {                if (clazz.isInstance(params[0])) {                    collection.add(params[0]);                }            }            Method childrenAccept = params[0].getClass().getMethod("childrenAccept", new Class[]{JavaParserVisitor.class, Object.class});            childrenAccept.invoke(params[0], new Object[]{proxy, null});            return null;        }    }    public Set getNodes(Class clazz, String javaCode) throws Throwable {        Collector coll = new Collector(clazz);        JavaParser parser = (new TargetJDK1_4()).createParser(new StringReader(javaCode));        ASTCompilationUnit cu = parser.CompilationUnit();        JavaParserVisitor jpv = (JavaParserVisitor) Proxy.newProxyInstance(JavaParserVisitor.class.getClassLoader(), new Class[]{JavaParserVisitor.class}, coll);        jpv.visit(cu, null);        return coll.getCollection();    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -