Adding a method [message #214291] |
Thu, 08 September 2005 07:40  |
Eclipse User |
|
|
|
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  |
Eclipse User |
|
|
|
"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
|
|
|
Powered by
FUDForum. Page generated in 0.03185 seconds