private static PsiNameValuePair createNameValuePair(
     @NotNull PsiAnnotationMemberValue value,
     @NotNull String namePrefix,
     @NotNull PairFunction<Project, String, PsiAnnotation> annotationCreator) {
   return annotationCreator
       .fun(value.getProject(), "@A(" + namePrefix + value.getText() + ")")
       .getParameterList()
       .getAttributes()[0];
 }
  @Nullable
  public static PsiAnnotationMemberValue setDeclaredAttributeValue(
      @NotNull PsiAnnotation psiAnnotation,
      @Nullable String attributeName,
      @Nullable PsiAnnotationMemberValue value,
      @NotNull PairFunction<Project, String, PsiAnnotation> annotationCreator) {
    PsiAnnotationMemberValue existing = psiAnnotation.findDeclaredAttributeValue(attributeName);
    if (value == null) {
      if (existing == null) {
        return null;
      }
      existing.getParent().delete();
    } else {
      if (existing != null) {
        ((PsiNameValuePair) existing.getParent()).setValue(value);
      } else {
        PsiNameValuePair[] attributes = psiAnnotation.getParameterList().getAttributes();
        if (attributes.length == 1) {
          PsiNameValuePair attribute = attributes[0];
          if (attribute.getName() == null) {
            PsiAnnotationMemberValue defValue = attribute.getValue();
            assert defValue != null : attribute;
            attribute.replace(
                createNameValuePair(
                    defValue,
                    PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME + "=",
                    annotationCreator));
          }
        }

        boolean allowNoName =
            attributes.length == 0 && ("value".equals(attributeName) || null == attributeName);
        final String namePrefix;
        if (allowNoName) {
          namePrefix = "";
        } else {
          namePrefix = attributeName + "=";
        }
        psiAnnotation
            .getParameterList()
            .addBefore(createNameValuePair(value, namePrefix, annotationCreator), null);
      }
    }
    return psiAnnotation.findDeclaredAttributeValue(attributeName);
  }