【问题标题】:Finding all Boolean expressions in a Java program with AST使用 AST 查找 Java 程序中的所有布尔表达式
【发布时间】:2013-11-06 21:24:16
【问题描述】:

我正在 Java Symbolic PathFinder 之上为 Java 程序构建一个符号评估测试生成器工具。作为这项工作的一部分,我需要识别 Java 源文件中的所有布尔表达式并记录有关它们的信息。我希望能够使用 AST 框架,如 Eclipse JDT 或 Sun 的 com.sun.source.tree 类,在那里我可以访问表达式并让 AST 告诉我表达式的类型。

Eclipse JDT 似乎支持这一点,但前提是您从 Eclipse 中运行。在 Eclipse 之外运行 JDT 的帖子很有帮助: IJavaProject without Eclipse Environment in JDT,和 Executing Eclipse plugin (jdt/ast) outside eclipse IDE environment。但是,这些并不是我想要的。 JDT without Eclipse? 可能非常接近,但我不想从命令行使用 JDT,我想将它的一部分合并到我的 Java 库中,该库将在 Java Symbolic Pathfinder 内部运行。

如果我在我的 Ant 构建脚本中包含以下 Eclipse 库:

<pathelement path="${eclipse-plugin-loc}\org.eclipse.core.contenttype_3.4.200.v20120523-2004.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.core.jobs_3.5.200.v20120521-2346.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.core.resources_3.8.0.v20120522-2034.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.core.runtime_3.8.0.v20120521-2346.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.equinox.common_3.6.100.v20120522-1841.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.equinox.preferences_3.5.0.v20120522-1841.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.jdt.compiler.apt_1.0.500.v20120522-1651.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.jdt.compiler.tool_1.0.101.v20120522-1651.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.jdt.core_3.8.1.v20120531-0637.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.jdt_3.8.0.v201206081400.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.text_3.5.200.v20120523-1310.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.text_3.5.200.v20120523-1310.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.osgi.util_3.2.300.v20120522-1822.jar"/>
<pathelement path="${eclipse-plugin-loc}\org.eclipse.osgi_3.8.0.v20120529-1548.jar"/>

然后我可以使用以下代码让 JDT 运行:

public void processJavaFile(File toVisit) {
    try {
        String source = readFileToString(toVisit);
        Document document = new Document(source);
        ASTParser parser = ASTParser.newParser(AST.JLS4);
        parser.setResolveBindings(true);
        parser.setSource(document.get().toCharArray());
        CompilationUnit unit = (CompilationUnit) parser.createAST(null);
        PreprocessClassVisitorEclipse pp = new PreprocessClassVisitorEclipse(unit);
        unit.accept(pp);

    } catch (IOException io) {
        System.out.println("Something went wrong with the files!");
    }
}

但这没有正确的上下文供解析器解析类型绑定;在查阅 Eclipse 文档时,我必须使用带有 IJavaProject 的解析器:http://publib.boulder.ibm.com/infocenter/rsahelp/v7r0m0/index.jsp?topic=/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/ASTParser.html

那么,对于让 IJavaProject 运行 JDT 作为另一个可执行文件中的库的任何想法?或者,任何其他具有类型信息的 Java AST?提前感谢您的帮助。

【问题讨论】:

  • 您可以在 OpenJDK 编译器及其内部 AST 之上插入,它具有完整的类型信息。

标签: java eclipse abstract-syntax-tree eclipse-jdt typing


【解决方案1】:

尝试添加以下内容:

parser.setEnvironment(null, null, null, true);
parser.setUnitName(toVisit.getName())

你也可以去掉Document,直接使用

 parser.setSource(source.toCharArray());

另请参阅:StandAloneASTParserTest.javaBug 206391

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2020-02-03
    相关资源
    最近更新 更多