I'm trying to try out eclipse jdt/ast following this article.
This is the java code as an input:
class Hello
{
int hello()
{
int a = 0, b = 3;
/* hello */
{
b = a * 3;
}
return a;
}
public static void main(String[] args)
{
int z = 0, i = 3;
/* hello */
{
i = z * 3;
}
}
}
With ASTView, it shows that VariableDeclarationFragment
has corresponding binding.
However, in this visitor code for VariableDeclarationFragment node
, I always get null value for 4 local variables (a,b,z,i) as resolveBinding()
return value.
What's wrong with this? I use eclipse indigo.
This is the code to get the AST
private static CompilationUnit createCompilationUnit(String sourceFile) {
String source = readWithStringBuilder(sourceFile);
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(source.toCharArray()); // set source
parser.setResolveBindings(true); // we need bindings later on
return (CompilationUnit) parser.createAST(null /* IProgressMonitor */); // parse
}
Does this mean I have to import source codes I want to run ASTParser on in my current project?
Right now I am using ASTParser incoporating with UIMA framework. In that framework, I could get source code in char[] only. Don't know how to get their corresponding IJavaProject and UnitName, which is needed if I .setSource() to a char[].
This happens because of the following from the setResolveBindings docs:
That means you could use something like that (from your link):
and add the
setProject
call:The second approach (without
setProject
):I tried solving this using the following piece of code