How to create annotation attribute from LocalQuickFix implementation

For my purposes i need to create parameter in some annotation. What is correct way of doing this ?

4 comments
Comment actions Permalink

Hello Evgeny,

For my purposes i need to create parameter in some annotation. What is
correct way of doing this ?


We don't have a specific API for that. You'll need to use createAnnotationFromText(),
passing the text of an annotation with parameters, and then pull the necessary
element from the children of the returned PsiAnnotation.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

We don't have a specific API for that. You'll need to use createAnnotationFromText(),
passing the text of an annotation with parameters, and then pull the necessary
element from the children of the returned PsiAnnotation.

Well, this approach ain't fit.
Because in my case i already have annotaion in source code. and only thing i have to to to fix - create attribute(parameter) with certain value.
For example

class xxx {
@CoolAnnotataion(cool = "..............")
public void doSmth() {
.......
}
}

I need to check if one parameter (cool in this case) is present and ensure if other parem is absent. if 2nd is absent i have to provide intention to create it.

0
Comment actions Permalink

Hello Evgeny,

>> We don't have a specific API for that. You'll need to use
>> createAnnotationFromText(), passing the text of an annotation with
>> parameters, and then pull the necessary element from the children of
>> the returned PsiAnnotation.
>>

Well, this approach ain't fit.

Because in my case i already have annotaion in source code. and only
thing i have to to to fix - create attribute(parameter) with certain
value.

For example

class xxx {
@CoolAnnotataion(cool = "..............")
public void doSmth() {
.......
}
}
I need to check if one parameter (cool in this case) is present and
ensure if other parem is absent. if 2nd is absent i have to provide
intention to create it.


So? You create a new dummy annotation from text, take the parameter element
out of it and insert it into the PSI of the real annotation in the code.
That's how all such tasks are actually implemented in IDEA itself.

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


0
Comment actions Permalink

Great !! Thank you very much for help :)
I was just thinking that such appoach is not "cool" :))

0

Please sign in to leave a comment.