Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Adding a method
Adding a method [message #214291] Thu, 08 September 2005 07:40 Go to next message
Eclipse UserFriend
Originally posted by: newsserver_mails.bodden.de

Hi.

Would you mind giving me a short outline of how I can add a method to a
type so that this is compliant with Eclipse's internal structure (I was
told to use ASTRewrite).

So for a type T and a method foo(), how Do I add foo() to T by rewriting
the AST (instead of using IType.addMethod(...), which takes the method in
String form.

Thanks,
Eric

--
Eric Bodden, ICQ: 12656220, http://www.bodden.de, PGP: BB465582
Arithmetische Kodierung - eine umfassende Einführung
http://bodden.de/studies/publications/pub_ac_de/
Re: Adding a method [message #215056 is a reply to message #214291] Sun, 18 September 2005 09:42 Go to previous message
Eclipse UserFriend
"Eric Bodden" <newsserver_mails@bodden.de> schreef in bericht
news:118d8vdu2sqhv$.1vj6tatbjxw37.dlg@40tude.net...
> Hi.
>
> Would you mind giving me a short outline of how I can add a method to a
> type so that this is compliant with Eclipse's internal structure (I was
> told to use ASTRewrite).


See the code snippet in the help for ASTRewrite, the example gives
everything you need.

> So for a type T and a method foo(), how Do I add foo() to T by rewriting
> the AST (instead of using IType.addMethod(...), which takes the method in
> String form.

AST ast = T.getAST();
ASTRewrite rewrite = ASTRewrite.create(ast);
MethodDeclaration methodFoo = ast.newMethodDeclaration();
// set methodFoo properties, such as
methodFoo.setName(ast.newSimpleName("foo"))

for JLS3:
ListRewrite lr = rewrite.getListRewrite(T,
TypeDeclaration.BODY_DECLARATIONS_PROPERTY);
lr.insert{first,last,before,after}(methodFoo);

for JLS2 I'm not sure; I guess:
newNode = (TypeDeclaration) rewrite.createcopytarget(T);
newNode.bodyDeclaration.add(methodFoo);
rewrite.replace(node, newNode);


HTH,
Arno
Previous Topic:Plug-in startup triggered by project nature detection
Next Topic:Modification of the this variable
Goto Forum:
  


Current Time: Sun May 11 13:20:53 EDT 2025

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

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

Back to the top