Source Level not being set in Compiler Options [message #1737392] |
Thu, 07 July 2016 17:35 |
Edward McNealy Messages: 3 Registered: July 2016 |
Junior Member |
|
|
I'm trying to parse a Java project and use bindings for method declarations. This is not in an Eclipse plugin, just a standalone project. I'm getting the CompilationUnits fine and visiting them works, but whenever I try to resolve bindings on a node, they are null. I keep getting problems (CompilationUnit.getProblems()) that are saying:
"Got # problems compiling the source file:"
"Syntax error, ... are only available if source level is 1.5 or greater"
I don't really understand this, since when I set my parser's compiler options, I am specifying java version 1.7. Here's my parser setup:
@SuppressWarnings("unchecked")
Hashtable<String, String> options = JavaCore.getDefaultOptions();
options.put(JavaCore.COMPILER_SOURCE, JavaCore.VERSION_1_7);
options.put(JavaCore.COMPILER_COMPLIANCE, JavaCore.VERSION_1_7);
options.put(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, JavaCore.VERSION_1_7);
mAstParser.setCompilerOptions(options);
String[] sources = new String[] {
project.getPath() + "/src",
project.getPath() + "/src/main/java",
project.getPath() + "/src/test/java"
};
String[] classPaths = ASTUtils.getClassPathEntries(project.getPath());
mAstParser.setEnvironment(classPaths, sources, new String[] {"UTF-8", "UTF-8", "UTF-8"}, true);
mAstParser.setKind(ASTParser.K_COMPILATION_UNIT);
mAstParser.setBindingsRecovery(true);
mAstParser.setResolveBindings(true);
mAstParser.setStatementsRecovery(true);
project.getPath() is just returning the location of the project that I am trying to analyze. The ASTUtils.getClassPathEntries() takes the project path and looks through "lib" and "target" folders to find any *.jar or *.class files.
What am I doing wrong?
|
|
|
|
|
Re: Source Level not being set in Compiler Options [message #1737465 is a reply to message #1737399] |
Fri, 08 July 2016 12:55 |
Edward McNealy Messages: 3 Registered: July 2016 |
Junior Member |
|
|
Well I finally figured this out. I think the bindings were not being resolved because at the time I was visiting the compilation units, I didn't have all of the source files converted into compilation units. I was looping through the all the files and using the parser to create the ASTs and then visit the units one by one. Now what I've done is add all of the source file paths in the project into a String[], then use parser.createASTs() method to create the trees for a batch of compilation units. Once I've done that, I can iterate through all of the CompilationUnits that are created and accept my visitors for each of those, and then when I resolveBindings() on my MethodDeclarations, I have bindings! So what I've done is something like this:
List<CompilationUnit> units = new ArrayList<>();
private void build() {
String[] sourceFiles = // Add all source file paths to an array here
parser.createASTs(sourceFiles, null, new String[0], this, null); // 'this', the class needs to extend FileASTRequestor
for (CompilationUnit unit : units) {
unit.accept(new ClassVisitor();
}
}
@Override
public void acceptAST(String sourceFilePath, CompilationUnit compilationUnit) {
units.add(compilationUnit);
}
|
|
|
Powered by
FUDForum. Page generated in 0.05127 seconds