how to extract scope information from Eclipse JDT core batch compiler
What I want to achieve is extract language structure of source code, for
example
class Test {
String name; // (1)
Test(String name) { // (2)
this.name = name; // (3) (4)
}
String toString() {
return name; // (5)
}
}
I want to figure out what is each "name" refering to. Like (3) and (5) are
refering to (1) and (4) is refering to (2). another example is
Test t = new Test("t");
System.out.println(t);
I want to know t1 is type Test and where is it declared.
I use JDT batch compiler (ecjsrc-4.3) downloaded from Eclipse 4.3 release
for this task. The reason I choose this instead of whole JDT framework is
because there is an existed JDTCompilerAdapter of this compiler for Ant,
which is what I need in the future.
So far I add a custom AstVisitor at line 540 in
org.eclipse.jdt.internal.compiler.Compiler, where compiler already
generate AstTree and scope information for all files.
My problem is, I don't know how to use the scope in each visit method in
AstVisitor to get those information. Like in the first example, in line
this.name = name;. on the LHS, it's a FieldReference for this.name and a
ThisReference for this. There is no AstNode for name on the LHS, therefore
I have no idea how to get which field is it referring to. On the RHS, it's
a SingleNameReference. However if I first invoke node.resolve(scope) then
try to print out 'node.resolvedType and node.binding, what I'll get is
null and java.lang.String name. In second example, I don't know how to get
the type information of t either, printing out binding will only get null
for resolveType and <no type> t for binding.
Any one have done this before?
No comments:
Post a Comment