Using the com/sun/tools/javac APIs
java.net at twistedprotein.com
Wed Feb 17 23:57:56 PST 2010
I've dabbled in those APIs as well, and it is some the most impenetrable code I've looked at. You'll obviously need the source code as the documentation is almost completely unhelpful, and you'll probably need to look at it in a good IDE to hyperlink back and forward as you untangle the labyrinth.
But I would strongly suggest checking out Kohsuke Kawaguchi's sourcerer project at https://sorcerer.dev.java.net/ (or for the blog that mentions it: http://weblogs.java.net/blog/kohsuke/archive/2006/09/sorcerer_a_bett.html). It seems like he's managed to accomplish exactly what you want to do, so it would be well worth checking it out.
On 18 Feb 2010, at 8:50 , Erik van Zijst wrote:
> Hi folks,
> Not sure if tis is the appropriate mailinglist, but I'm trying to get my head around the various javac API's and I'm having trouble understanding what I can or can not do.
> I'm parsing and traversing a set of source files using the unsupported com.sun.tools.javac.tree API and for every method invocation and type reference, I want to resolve the exact (source) location where that method of type is declared, so that I can render the sources with hyperlinks for full navigation (IDE-style).
> I'm having limited success. Traversing the JCTree.JCCompilationUnit list after the JavacTask.parse() step does not seems to offer name and type resolving (presumably because the attribution step hasn't run yet?).
> Traversing the javax.lang.model.element.Elements list from JavacTask.analyze() gives me class and method declarations with the name of the respective java source file (but not their character offset positions).
> Using the javac APIs, how do I go about matching these sets? Am I overlooking some functionality, or is this a non trivial task?
> Correct resolution is crucial. For instance, it's important that a call to SubType.finalMethodOnSuperClass() properly links to the declartion of finalMethodOnSuperClass() in the BaseType file (assuming SubType extends BaseType).
More information about the compiler-dev