Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Source Level not being set in Compiler Options
Source Level not being set in Compiler Options [message #1737392] Thu, 07 July 2016 17:35 Go to next message
Edward McNealy is currently offline Edward McNealyFriend
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 #1737398 is a reply to message #1737392] Thu, 07 July 2016 18:26 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
looks bogus, I agree.

That project, does it have .project and .classpath files?

How do you setSource(..)? If that's called after setCompilerOptions() it would overwrite the compiler options with those found in the enclosing project.
Re: Source Level not being set in Compiler Options [message #1737399 is a reply to message #1737398] Thu, 07 July 2016 18:37 Go to previous messageGo to next message
Edward McNealy is currently offline Edward McNealyFriend
Messages: 3
Registered: July 2016
Junior Member
Yes, the test project has .project and .classpath files. I have a list of all the .java files in the project, then loop through those and do the setSource() -> parser.createAST(null) -> compilationUnit.accept(MyVisitor) in that loop. So I was only setting up the options once before that loop. I changed this so now it's calling setSource(), then setting up the options, then createAST(). With this I get no more problems, but all of the bindings from my MethodDeclaration node.resolveBindings() are still null.

Thanks for the help.
Re: Source Level not being set in Compiler Options [message #1737465 is a reply to message #1737399] Fri, 08 July 2016 12:55 Go to previous message
Edward McNealy is currently offline Edward McNealyFriend
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);
}

Previous Topic:BX tool
Next Topic:[NEON] JUnit view goes missing ...
Goto Forum:
  


Current Time: Sun Dec 22 05:18:08 GMT 2024

Powered by FUDForum. Page generated in 0.05127 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top