Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a Compilation Unit with type bindings

I am working with the AST API in java, and I am trying to create a Compilation Unit with type bindings. I wrote the following code:

private static CompilationUnit parse(ICompilationUnit unit) {
 ASTParser parser = ASTParser.newParser(AST.JLS3);
 parser.setKind(ASTParser.K_COMPILATION_UNIT);
 parser.setSource(unit);
 parser.setResolveBindings(true);
 CompilationUnit compiUnit = (CompilationUnit) parser.createAST(null);
 return compiUnit;
}

Unfortunately, when I run this code in debug mode and inspect compiUnit I find that compiUnit.ast.resolver.isRecoveringBindings is false.
Can anyone think of a reason why it wouldn't be true, as I specified it to be?
Thank you

like image 885
hizki Avatar asked Dec 16 '25 16:12

hizki


1 Answers

You are mixing up two pieces of the API: binding resolving and binding recovery. From the JavaDoc for setBindingsRecovery:

void org.eclipse.jdt.core.dom.ASTParser.setBindingsRecovery(boolean enabled)

Requests that the compiler should perform bindings recovery. When bindings recovery is enabled the compiler returns incomplete bindings.

Default to false.

This should be set to true only if bindings are resolved. It has no effect if there is no binding resolution.

Parameters:
enabled true if incomplete bindings are expected, and false if only complete bindings are expected.

So, yes. It is expected that bindings recovery is set to false because that is the default. However, since you explicitly set bindings to be resolved, that should be set to true in the AST object. You should check the method AST.hasBindingsResolved() to see if you can get your bindings.

To be clear: bindings resolution is about getting the parser/compiler to calculate type bindings as the AST is being created, while bindings recovery is about enabling bindings to be partially computed. Honestly, I'm not exactly sure what bindings recovery helps with, but I am fairly certain that it is not something that you need.

like image 162
Andrew Eisenberg Avatar answered Dec 19 '25 06:12

Andrew Eisenberg