How to modify a Method without creating an entire AST? [message #251659] |
Thu, 28 February 2008 10:40 |
Eclipse User |
|
|
|
Originally posted by: topgun.in.gmail.com
I want to change modifiers of a method by selecting an option from popup
menu.
The way it has been implemented as of now is :
* From the ISelection object in the method selectionChanged() I type cast it
to IMember and then get its ICompilationUnit.
* I build the AST to get CompilationUnit (the top most ASTNode).
* Then I traverese down to the relevant method and change its modifiers
using ASTRewrite.
I tried creating AST for simply the method but I guess we can't do that.. or
can we? Can someone suggest me how to do it in a better way?
Thanks,
Pushkar
|
|
|
|
Re: How to modify a Method without creating an entire AST? [message #251685 is a reply to message #251664] |
Fri, 29 February 2008 06:47 |
Eclipse User |
|
|
|
Originally posted by: topgun.in.gmail.com
Oliver,
> You can use the getFocalPosition(int) to create the statements only of the
> method you selected. All other methods or fields will only consist of the
> headers. No statements.
I could not locate the getFocalPosition(int) method. The ASTParser has the
setFocalPosition(int position) method. What parameter value do I pass to it?
Code snippet :
public void selectionChanged(IAction action, ISelection selection)
{ ...
IMember member = (IMember)
((IStructuredSelection)selection).getFirstElement();
ICompilationUnit cu = member.getCompilationUnit();
CompilationUnit unit = parse(cu);
...
}
protected CompilationUnit parse (ICompilationUnit unit)
{
ASTParser parser = ASTparser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(unit);
//I believe this is the place where i need to setFocalPosition(int) what
value do I pass??
parser.setFocalPosition(?);
return(CompilationUnit) parser.createAST(null);
}
Thanks,
Pushkar
|
|
|
|
Re: How to modify a Method without creating an entire AST? [message #251832 is a reply to message #251721] |
Mon, 03 March 2008 14:53 |
Eclipse User |
|
|
|
Originally posted by: topgun.in.gmail.com
Oliver,
> Sorry, it was setFocalPosition(int).
> You can pass in the offset position of the IMember getNameRange().
> Let me know if it works.
Yes, it does work.
I set it as follows :
parser.setFocalPosition(member.getNameRange().getOffset());
This one would be efficient as it would avoid unnecessary parsing of the
entire source.
Thanks Oliver :)
Pushkar
|
|
|
Powered by
FUDForum. Page generated in 0.04445 seconds