private static boolean annotateElement(@NotNull Project project,
                                         String annotation,
                                         Set<PsiAnnotation.TargetType> targets,
                                         PsiElement element, boolean makeAction) {
    if (!element.isValid()) return false;
    PsiFile psiFile = element.getContainingFile();
    if (psiFile == null) return false;
    PsiModifierListOwner listOwner = ObjectUtils.tryCast(element, PsiModifierListOwner.class);
    if (listOwner == null) return false;
    PsiAnnotation.TargetType[] location = AnnotationTargetUtil.getTargetsForLocation(listOwner.getModifierList());
    Set<PsiAnnotation.TargetType> currentTypes = EnumSet.copyOf(targets);
    currentTypes.retainAll(Arrays.asList(location));
    if (currentTypes.isEmpty()) {
      return false;
    }

    if ((currentTypes.contains(PsiAnnotation.TargetType.TYPE_USE))) {
      List<IntentionAction> actions = List.of();
      if (listOwner instanceof JvmParameter jvmParameter) {
        ChangeTypeRequest changeTypeRequest = createTypeRequest(jvmParameter.getType(), annotation);
        actions = JvmElementActionFactories.createChangeTypeActions(jvmParameter, changeTypeRequest);
      }
      else if (listOwner instanceof JvmField jvmField) {
        ChangeTypeRequest changeTypeRequest = createTypeRequest(jvmField.getType(), annotation);
        actions = JvmElementActionFactories.createChangeTypeActions(jvmField, changeTypeRequest);
      }
      else if (listOwner instanceof JvmMethod jvmMethod) {
        if (UastContextKt.toUElement(listOwner) instanceof UMethod uMethod &&
            uMethod.getSourcePsi() != null &&
            UastContextKt.toUElement(uMethod.getSourcePsi()) instanceof UMethod rereadUMethod &&
            rereadUMethod.getReturnType() != null) {
          //kotlin can change return type already, let's reread
          PsiAnnotation[] annotations = rereadUMethod.getReturnType().getAnnotations();
          if (ContainerUtil.exists(annotations, fromType -> fromType.hasQualifiedName(annotation))) {
            return true;
          }
        }
        JvmType type = jvmMethod.getReturnType();
        if (type == null) return false;
        ChangeTypeRequest changeTypeRequest = createTypeRequest(type, annotation);
        actions = JvmElementActionFactories.createChangeTypeActions(jvmMethod, changeTypeRequest);
      }
      if (actions.size() == 1) {
        if (makeAction) {
          actions.get(0).invoke(project, null, psiFile);
        }
        return true;
      }
    }
    UElement sourceUElement = UastContextKt.toUElement(element);
    if (element instanceof PsiMethod &&
        sourceUElement != null &&
        UastContextKt.toUElement(sourceUElement.getSourcePsi()) instanceof UField uField) {
      element = uField.getJavaPsi();
    }
    JvmModifiersOwner jvmModifiersOwner = ObjectUtils.tryCast(element, JvmModifiersOwner.class);
    if (jvmModifiersOwner == null) return false;
    AnnotationRequest request = AnnotationRequestsKt.annotationRequest(annotation);
    List<IntentionAction> actions = JvmElementActionFactories.createAddAnnotationActions(jvmModifiersOwner, request);
    if (actions.size() == 1) {
      if (makeAction) {
        actions.get(0).invoke(project, null, psiFile);
      }
      return true;
    }
    return false;
  }
private static boolean annotateElement(@NotNull Project project,
                                         String annotation,
                                         Set<PsiAnnotation.TargetType> targets,
                                         PsiElement element, boolean makeAction)