Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » How to modify a Method without creating an entire AST?
How to modify a Method without creating an entire AST? [message #251659] Thu, 28 February 2008 10:40 Go to next message
Eclipse UserFriend
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 #251664 is a reply to message #251659] Thu, 28 February 2008 14:21 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
Pushkar a écrit :
> 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?
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.
HTH,
Olivier
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 Go to previous messageGo to next message
Eclipse UserFriend
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 #251721 is a reply to message #251685] Fri, 29 February 2008 16:56 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
Pushkar a écrit :
> 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 :
Sorry, it was setFocalPosition(int).
You can pass in the offset position of the IMember getNameRange().
Let me know if it works.
--
Olivier
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 Go to previous message
Eclipse UserFriend
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
Previous Topic:finding Java library source
Next Topic:Access restrictions
Goto Forum:
  


Current Time: Mon Jul 22 20:50:57 GMT 2024

Powered by FUDForum. Page generated in 0.03476 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top