Вопрос

I'm building a symbolic evaluation test generator tool for Java programs on top of Java Symbolic PathFinder. As part of this effort, I need to identify all Boolean expressions in a Java source file and record information about them. I was hoping to be able to use an AST framework like Eclipse JDT or Sun's com.sun.source.tree classes, where I could visit the expressions and have the AST tell me the type of the expression.

It appears that Eclipse JDT supports this, but only if you run from within Eclipse. The posts on running JDT outside Eclipse are helpful: IJavaProject without Eclipse Environment in JDT, and Executing Eclipse plugin (jdt/ast) outside eclipse IDE environment. However, these are not quite what I'm looking for; JDT without Eclipse? may be very close, but I don't want to use JDT from the command line, I want to incorporate parts of it into my Java library which will run inside of Java Symbolic Pathfinder.

If I include the following Eclipse libraries in my Ant build script:

<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"/>

then I can get JDT to run with the following code:

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!");
    }
}

But this does not have the right context for the parser to resolve the type bindings; in consulting the Eclipse documentation, I have to use the parser with an 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.

So, any ideas for getting an IJavaProject running JDT as a library inside of another executable? Alternately, any other Java ASTs that have type information? Thank you in advance for your help.

Это было полезно?

Решение

Try adding the following:

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

Also you can get rid of Document and just use

 parser.setSource(source.toCharArray());

See also: StandAloneASTParserTest.java and Bug 206391

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top