Add Name-Value Pair to PsiAnnotation

I have looked through the forums and the API, and I have been unable to find
a factory that can be used to add a Name-Value pair to an annotation. For
example:

@SomeAnnotation(someVariable = true)
void someMethodName();

I can create the "SomeAnnotation" annotation through the PsiElementFactory
by using factory.createAnnotationFromText("@SomeAnnotation", method). However,
I have been unable to find a way through the Open API to add in the "someVaraible
= true" name-value pair within the annotation. I know that once you create
the annotation through the PsiElementFactory you can call annotation.getParameterList().getAttributes()
to get all name-value pairs, but this only returns the current defined structure
of those pairs.

Is there some factory that has access to create such Name-Value pairs? Doing
the following does not appear that it will work:

PsiNameValuePair[] nameValuePairs = annotation.getParameterList().getAttributes();
PsiNameValuePair[] newNameValuePairs = new PsiNameValuePair[nameValuePairs.length
+ 1];
System.arraycopy(nameValuePairs, 0, newNameValuePairs, 0, nameValuePairs.length);
newNameValuePairs[nameValuePairs.length] = new PsiNameValuePairImpl();
// Somehow set the name / value in the pair (How?)

Thanks,
Mike


2 comments

You should create a full annotation from text
factory.createAnnotationFromText("@SomeAnnotation(someVariable = true)", context)

0

It is a nasty hack but it worked for me:

PsiAnnotationParameterList list = a.getParameterList ();
PsiElement exp = PsiManager.getInstance (mProject).getElementFactory ().createDisplayText (", " + text);
list.addAfter (exp, attrs[attrs.length-1]);

Misha

0

Please sign in to leave a comment.