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 lang
The following Code provides you the AST Node of the current selected Code from the CompilationUnitEditor.
ITextEditor editor = (ITextEditor) HandlerUtil.getActiveEditor(event);
ITextSelection sel = (ITextSelection) editor.getSelectionProvider().getSelection();
ITypeRoot typeRoot = JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
ICompilationUnit icu = (ICompilationUnit) typeRoot.getAdapter(ICompilationUnit.class);
CompilationUnit cu = parse(icu);
NodeFinder finder = new NodeFinder(cu, sel.getOffset(), sel.getLength());
ASTNode node = finder.getCoveringNode();
The JavaUI is the entry point to the JDT UI plugin.