Declaration and references for a variable
Jonathan Gibbons
jonathan.gibbons at oracle.com
Tue Nov 13 13:14:48 PST 2012
Antonio,
I'm guessing you're already using the Compiler API (javax.tools.*) and
Tree API (com.sun.source.*) if you've got as far as using TaskListeners.
In which case, there's an easier to do what you want.
Create the CompilationTask, down cast it to a JavacTask, then call
parse() and analyze().
The AST's returned from parse() will now contain all the info you need.
Use a TreePathScanner to visit the nodes in the AST, looking for the
nodes you're interested in. Get yourself a com.sun.source.util.Trees
utility object, with Trees.instance(JavacTask), then use the methods on
Trees to query the info you need, such as element, type, etc for the
nodes you're interested in. Typically, you want to use the methods on
Trees that take a TreePath to identify the node.
>
> Another question: the attribute phase call the 'started' method of the
> task listener, but it doens't call the "finished" method. Why?
What do you mean by "attribute phase"? If you mean the "analyze" call
I mentioned earlier, that would be a bug. If you mean the javac
internal phase "Attr", you won't see the "finished" method called until
"analyze" has completed, which is after the internal javac phase, Flow.
-- Jon
On 11/13/2012 12:21 PM, Antonio Tancredi wrote:
> 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