Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Annotation insert location
Annotation insert location [message #206531] Mon, 13 June 2005 14:42 Go to next message
Karen Butzke is currently offline Karen ButzkeFriend
Messages: 220
Registered: July 2009
Senior Member
I am programmatically adding an annotation to a FieldDeclaration. I see
inconsisitent behavior depending on whether the field has existing
modifiers. If the field is defined as private String foo then the
annotation is placed on the line above the field like this:

@MyAnnotation
private String foo

If the field is defined as just String foo, then the annotation is placed
like this:

@MyAnnotation String foo


Here is some of my code used to do this:

AST ast = astRewrite.getAST();
NormalAnnotation annotation = ast.newNormalAnnotation();
annotation.setTypeName(ast.newSimpleName("MyAnnotation"));

astRewrite.getListRewrite(bodyDeclaration,
bodyDeclaration.getModifiersProperty()).insertAt(annotation, 0, null);


The same thing occurs with other BodyDeclarations such as a
TypeDeclaration. Is there a better way to go about this or is this a bug?


thanks,
Karen
Re: Annotation insert location [message #206563 is a reply to message #206531] Mon, 13 June 2005 15:47 Go to previous messageGo to next message
Markus KellerFriend
Messages: 294
Registered: July 2009
Senior Member
There was a discussion about annotation placement in bug 82461, and a code
formatter option was introduced. If the current options don't fit your
needs, please file an enhancement request for JDT/Core.

If you think the ASTRewrite does not produce properly formatted code
according to the code formatting options, please file a bug for JDT/Core.

If the behavior is really different depending on existing modifiers, I'd say
that's a bug.

Markus

Karen Moore wrote:
> I am programmatically adding an annotation to a FieldDeclaration. I see
> inconsisitent behavior depending on whether the field has existing
> modifiers. If the field is defined as private String foo then the
> annotation is placed on the line above the field like this:
>
> @MyAnnotation
> private String foo
>
> If the field is defined as just String foo, then the annotation is
> placed like this:
>
> @MyAnnotation String foo
>
>
> Here is some of my code used to do this:
>
> AST ast = astRewrite.getAST();
> NormalAnnotation annotation = ast.newNormalAnnotation();
> annotation.setTypeName(ast.newSimpleName("MyAnnotation"));
>
> astRewrite.getListRewrite(bodyDeclaration,
> bodyDeclaration.getModifiersProperty()).insertAt(annotation, 0, null);
>
>
> The same thing occurs with other BodyDeclarations such as a
> TypeDeclaration. Is there a better way to go about this or is this a bug?
>
>
> thanks,
> Karen
>
Re: Annotation insert location [message #206571 is a reply to message #206563] Mon, 13 June 2005 16:52 Go to previous messageGo to next message
Karen Butzke is currently offline Karen ButzkeFriend
Messages: 220
Registered: July 2009
Senior Member
Thanks for your response, I had not seen the added preference or that
resolved bug. I have entered bug 99729 since the behavior is different
depending on existing modifiers.
Re: Annotation insert location [message #644586 is a reply to message #206531] Mon, 13 December 2010 11:36 Go to previous messageGo to next message
almoayed  is currently offline almoayed Friend
Messages: 1
Registered: December 2010
Junior Member
Hey,
I am trying to add annotations to certain methods of a class. I am using the ASTVisitor to get hold of the MethodDeclaration object. I used the code lines you offered, but the problem, I can see no changes to the MethodDeclaration node and I can get the ASTRewrite to write anything back to the java code. Do you have any idea how to do that?? Do u have any tutorial on how to insert annotations to method and class declarations??

I tried the following but it didn't help

http://www.eclipse.org/articles/article.php?file=Article-Jav aCodeManipulation_AST/index.html
Re: Annotation insert location [message #645273 is a reply to message #644586] Thu, 16 December 2010 11:12 Go to previous message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
The only change from the already given example should be something like this.....
MethodDeclaration methodDecl= .....
ListRewrite listRewrite = astRewrite.getListRewrite(methodDecl, MethodDeclaration.MODIFIERS2_PROPERTY ).insertAt(annotation, 0, null);

Isn't this working?
Previous Topic:Can anyone give me an example where the eclipse JDT creates a CAPTURE binding?
Next Topic:Getting an error when trying to create a java project programmatically
Goto Forum:
  


Current Time: Tue Apr 23 18:03:21 GMT 2024

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

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

Back to the top