ModCommand (@NotNull ActionContext context, @NotNull PsiAnnotation annotation) { PsiClass origAnnoType = annotation.resolveAnnotationType(); if (origAnnoType == null) return ModCommand.nop(); return ModCommand.psiUpdate(origAnnoType, annotationType -> { PsiModifierList modifierList = annotationType.getModifierList(); if (modifierList == null) return; PsiAnnotation targetAnnotation = modifierList.findAnnotation(CommonClassNames.JAVA_LANG_ANNOTATION_TARGET); String targetText = "java.lang.annotation.ElementType." + myTarget; Project project = context.project(); PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); if (targetAnnotation == null) { targetAnnotation = modifierList.addAnnotation(CommonClassNames.JAVA_LANG_ANNOTATION_TARGET); } PsiExpression expression = factory.createExpressionFromText(targetText, targetAnnotation); PsiNameValuePair attribute = AnnotationUtil.findDeclaredAttribute(targetAnnotation, null); PsiAnnotationMemberValue value = attribute == null ? null : attribute.getValue(); if (value == null) { targetAnnotation.setDeclaredAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME, expression); } else { if (!(value instanceof PsiArrayInitializerMemberValue)) { PsiAnnotation dummyAnnotation = factory.createAnnotationFromText("@A({" + value.getText() + "})", null); PsiAnnotationMemberValue dummyValue = dummyAnnotation.getParameterList().getAttributes()[0].getValue(); value = targetAnnotation.setDeclaredAttributeValue(PsiAnnotation.DEFAULT_REFERENCED_METHOD_NAME, dummyValue); } value.addAfter(expression, value.getFirstChild()); } JavaCodeStyleManager.getInstance(project).shortenClassReferences(targetAnnotation); }); }