Declaration and references for a variable
Antonio Tancredi
nazareno.tancredi at gmail.com
Tue Nov 13 12:21:08 PST 2012
Hello,
I'm trying to collect some information about variables in a Java source
file, using the openjdk 7 compiler.
What I want to do is: save the AST node for the variable's declaration
and keep a list of all references to this variable (a list of JCTree).
If the variable is declared inside the Java file that I'm inspecting
it's easy because I can save the declaration and references in a map
while visiting the AST, using the symbol as key (see my other message of
Sept-2012).
The problem is when the file contains references to a variable declared
somewhere else in the sourcepath.
My idea is: create a new TaskListener (let's call this object "t"), wait
for the TaskEvent.Kind.ANALYZE event and save the
TypeElement/CompilationUnitTree pair.
After this, I have all the compilation units needed by the source file
that I'm inspecting. Every compilation unit is identified by the
TypeElement (it will always be a ClassSymbol).
After the visit of my visitor, I have all symbols/variables with at
least one reference or declaration.
For each symbol "s" that has no declaration I may assume that the
related variable was defined inside another compilation unit, so I can
do this:
// find s inside the right comp. unit
JCVariableDecl decl = finderVisitor.findDecl(s, t.getCompUnit(
s.outermostClass()));
Will this approach work? There's an easier/better way to do this?
Another question: the attribute phase call the 'started' method of the
task listener, but it doens't call the "finished" method. Why?
Thank you so much.
Antonio Tancredi
More information about the compiler-dev
mailing list