views:

197

answers:

2

I need to get the AST for the current selection in the java editor fo eclipse. Basically I want to convert the selected java code in to some other form(maybe some other language or XML etc..). So I guess, I need to get the AST for the selection. Currently I am able to get the selection as simple text. Is there any way out for such problem? Thanks already!!

+1  A: 

There are a number of handy tools for JDT plugin developers, especially the AST View which does pretty much what you are looking for. So, all you need to do is grab the code for AST View and check how it is done.

The plugin can be installed from the following update site: http://www.eclipse.org/jdt/ui/update-site

Use the plugin spy (read more about it in this article) to start digging into the view classes.

You are traveling into less trivial (and often undocumented) areas of JDT, developing your code digging skills will greatly improve your performance.

zvikico
Thanks, I have just started browsing the code and I am already feeling optimistic. Will let you know.
Suraj Chandran
@zvikico, thanks. Now I am able to get the ASTNode for the selected text. Now I need a ASTVisitor that could give me fully resolved code of the node. i.e. each type name should be fully qualified name so that I can serialize it and do some magic over it.Any idea?? Thanks!
Suraj Chandran
I can get code of form String x;But what I need is java.lang.String x;Any ideas?
Suraj Chandran
You have a node. Nicely done. The key is in the node.resolveBinding() method(s) which returns IBinding implementor. For example, a VariableDeclaration will resolve to IVariableBinding which has a getType() method which return ITypeBinding. Once you get the ITypeBinding, it will give you all the information you need on the fully qualified type.
zvikico
@zvikico thanks again..i got one step further :)
Suraj Chandran
If it worked for you, you might as well mark it as the right answer :-)
zvikico
A: 

IIRC, each node in the Eclipse AST contains an offset. All you need to do is to compute the offsets for the part of the code you are interested in then walk the AST to select the nodes within those offsets.

mansu