private static void addSuppressAnnotation(
     final Project project, final GrModifierList modifierList, final String id)
     throws IncorrectOperationException {
   PsiAnnotation annotation =
       modifierList.findAnnotation(BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME);
   final GrExpression toAdd =
       GroovyPsiElementFactory.getInstance(project).createExpressionFromText("\"" + id + "\"");
   if (annotation != null) {
     final PsiAnnotationMemberValue value = annotation.findDeclaredAttributeValue(null);
     if (value instanceof GrAnnotationArrayInitializer) {
       value.add(toAdd);
     } else if (value != null) {
       GrAnnotation anno =
           GroovyPsiElementFactory.getInstance(project).createAnnotationFromText("@A([])");
       final GrAnnotationArrayInitializer list =
           (GrAnnotationArrayInitializer) anno.findDeclaredAttributeValue(null);
       list.add(value);
       list.add(toAdd);
       annotation.setDeclaredAttributeValue(null, list);
     }
   } else {
     modifierList
         .addAnnotation(BatchSuppressManager.SUPPRESS_INSPECTIONS_ANNOTATION_NAME)
         .setDeclaredAttributeValue(null, toAdd);
   }
 }
 private static boolean shouldProcessInstanceMembers(
     @NotNull GrTypeDefinition grType, @Nullable PsiElement lastParent) {
   if (lastParent != null) {
     final GrModifierList modifierList = grType.getModifierList();
     if (modifierList != null
         && modifierList.findAnnotation(GroovyCommonClassNames.GROOVY_LANG_CATEGORY) != null) {
       return false;
     }
   }
   return true;
 }
  private PsiElement findSuitableModifier() {
    final GrModifierList list = getModifierList();

    PsiElement modifier = PsiUtil.findModifierInList(list, GrModifier.DEF);
    if (modifier != null) return modifier;

    modifier = PsiUtil.findModifierInList(list, PsiModifier.FINAL);
    if (modifier != null) return modifier;

    for (PsiElement element : list.getModifiers()) {
      if (!(element instanceof GrAnnotation)) {
        return element;
      }
    }

    return null;
  }
  private static PsiClassType doGetSuperClassType(GroovyScriptClass scriptClass) {
    GrVariableDeclaration declaration = findDeclaration(scriptClass.getContainingFile());
    if (declaration != null) {

      GrModifierList modifierList = declaration.getModifierList();
      if (modifierList.findAnnotation(GroovyCommonClassNames.GROOVY_TRANSFORM_BASE_SCRIPT)
          != null) {
        GrTypeElement typeElement = declaration.getTypeElementGroovy();
        if (typeElement != null) {
          PsiType type = typeElement.getType();
          if (type instanceof PsiClassType) {
            return (PsiClassType) type;
          }
        }
      }
    }
    return null;
  }
  private void appendParenthesesIfNeeded() {
    PsiElement first = getFirstChild();
    if (first == null) {
      getNode().addLeaf(GroovyTokenTypes.mLT, "<", null);
    }

    PsiElement last = getLastChild();
    if (last.getNode().getElementType() != GroovyTokenTypes.mGT) {
      getNode().addLeaf(GroovyTokenTypes.mGT, ">", null);
    }

    PsiElement parent = getParent();
    if (parent instanceof GrMethod) {
      GrModifierList list = ((GrMethod) parent).getModifierList();
      PsiElement[] modifiers = list.getModifiers();
      if (modifiers.length == 0) {
        list.setModifierProperty(GrModifier.DEF, true);
      }
    }
  }
  private static void processClassUsage(GrTypeDefinition psiClass, JavaChangeInfo changeInfo) {
    String name = psiClass.getName();

    GrMethod constructor =
        GroovyPsiElementFactory.getInstance(psiClass.getProject())
            .createConstructorFromText(
                name, ArrayUtil.EMPTY_STRING_ARRAY, ArrayUtil.EMPTY_STRING_ARRAY, "{}", null);

    GrModifierList list = constructor.getModifierList();
    if (psiClass.hasModifierProperty(PsiModifier.PRIVATE))
      list.setModifierProperty(PsiModifier.PRIVATE, true);
    if (psiClass.hasModifierProperty(PsiModifier.PROTECTED))
      list.setModifierProperty(PsiModifier.PROTECTED, true);
    if (!list.hasExplicitVisibilityModifiers()) {
      list.setModifierProperty(GrModifier.DEF, true);
    }

    constructor = (GrMethod) psiClass.add(constructor);
    processConstructor(constructor, changeInfo);
  }
  private static void execute(final GrField field, final Collection<PsiElement> fieldUsages) {
    final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(field.getProject());

    final StringBuilder builder = new StringBuilder(field.getTextLength());
    final GrClosableBlock block = (GrClosableBlock) field.getInitializerGroovy();

    final GrModifierList modifierList = field.getModifierList();
    if (modifierList.getModifiers().length > 0 || modifierList.getAnnotations().length > 0) {
      builder.append(modifierList.getText());
    } else {
      builder.append(GrModifier.DEF);
    }
    builder.append(' ').append(field.getName());

    builder.append('(');
    if (block.hasParametersSection()) {
      builder.append(block.getParameterList().getText());
    } else {
      builder.append("def it = null");
    }
    builder.append(") {");

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                block.getParameterList().delete();
                block.getLBrace().delete();
                final PsiElement psiElement =
                    PsiUtil.skipWhitespacesAndComments(block.getFirstChild(), true);
                if (psiElement != null && "->".equals(psiElement.getText())) {
                  psiElement.delete();
                }
                builder.append(block.getText());
                final GrMethod method =
                    GroovyPsiElementFactory.getInstance(field.getProject())
                        .createMethodFromText(builder.toString());
                field.getParent().replace(method);
                for (PsiElement usage : fieldUsages) {
                  if (usage instanceof GrReferenceExpression) {
                    final PsiElement parent = usage.getParent();
                    StringBuilder newRefText = new StringBuilder();
                    if (parent instanceof GrReferenceExpression
                        && usage == ((GrReferenceExpression) parent).getQualifier()
                        && "call".equals(((GrReferenceExpression) parent).getReferenceName())) {
                      newRefText.append(usage.getText());
                      usage = parent;
                    } else {
                      PsiElement qualifier = ((GrReferenceExpression) usage).getQualifier();
                      if (qualifier == null) {
                        if (parent instanceof GrReferenceExpression
                            && ((GrReferenceExpression) parent).getQualifier() != null
                            && usage != ((GrReferenceExpression) parent).getQualifier()) {
                          qualifier = ((GrReferenceExpression) parent).getQualifier();
                          usage = parent;
                        }
                      }

                      if (qualifier != null) {
                        newRefText.append(qualifier.getText()).append('.');
                        ((GrReferenceExpression) usage).setQualifier(null);
                      } else {
                        newRefText.append("this.");
                      }
                      newRefText.append('&').append(usage.getText());
                    }
                    usage.replace(factory.createReferenceExpressionFromText(newRefText.toString()));
                  }
                }
              }
            });
  }
  public static boolean checkModifierProperty(
      @NotNull GrModifierList modifierList,
      @GrModifier.GrModifierConstant @NotNull String modifier) {
    final PsiElement owner = modifierList.getParent();
    if (owner instanceof GrVariableDeclaration
        && owner.getParent() instanceof GrTypeDefinitionBody) {
      PsiElement pParent = owner.getParent().getParent();
      if (!modifierList
          .hasExplicitVisibilityModifiers()) { // properties are backed by private fields
        if (!(pParent instanceof PsiClass) || !((PsiClass) pParent).isInterface()) {
          if (modifier.equals(GrModifier.PRIVATE)) return true;
          if (modifier.equals(GrModifier.PROTECTED)) return false;
          if (modifier.equals(GrModifier.PUBLIC)) return false;
        }
      }

      if (pParent instanceof PsiClass && ((PsiClass) pParent).isInterface()) {
        if (modifier.equals(GrModifier.STATIC)) return true;
        if (modifier.equals(GrModifier.FINAL)) return true;
      }
      if (pParent instanceof GrTypeDefinition) {
        PsiModifierList pModifierList = ((GrTypeDefinition) pParent).getModifierList();
        if (pModifierList != null
            && pModifierList.findAnnotation(ConstructorAnnotationsProcessor.IMMUTABLE) != null) {
          if (modifier.equals(GrModifier.FINAL)) return true;
        }
      }
    }

    // top level classes cannot have private and protected modifiers
    if (owner instanceof GrTypeDefinition
        && ((GrTypeDefinition) owner).getContainingClass() == null) {
      if (modifier.equals(PROTECTED) || modifier.equals(PRIVATE)) return false;
      if (modifier.equals(PACKAGE_LOCAL))
        return modifierList.hasExplicitModifier(PRIVATE)
            || modifierList.hasExplicitModifier(PROTECTED);
    }

    if (modifierList.hasExplicitModifier(modifier)) {
      return true;
    }

    if (modifier.equals(GrModifier.PUBLIC)) {
      if (owner instanceof GrPackageDefinition) return false;
      if (owner instanceof GrVariableDeclaration
              && !(owner.getParent() instanceof GrTypeDefinitionBody)
          || owner instanceof GrVariable) {
        return false;
      }
      // groovy type definitions and methods are public by default
      return !modifierList.hasExplicitModifier(GrModifier.PRIVATE)
          && !modifierList.hasExplicitModifier(GrModifier.PROTECTED);
    }

    if (owner instanceof GrTypeDefinition) {
      if (modifier.equals(GrModifier.STATIC)) {
        final PsiClass containingClass = ((GrTypeDefinition) owner).getContainingClass();
        return containingClass != null && containingClass.isInterface();
      }
      if (modifier.equals(GrModifier.ABSTRACT)) {
        return ((GrTypeDefinition) owner).isInterface();
      }
    }

    return false;
  }
  private static boolean processPrimaryMethodInner(
      JavaChangeInfo changeInfo, GrMethod method, @Nullable PsiMethod baseMethod) {
    if (changeInfo.isNameChanged()) {
      String newName =
          baseMethod == null
              ? changeInfo.getNewName()
              : RefactoringUtil.suggestNewOverriderName(
                  method.getName(), baseMethod.getName(), changeInfo.getNewName());
      if (newName != null && !newName.equals(method.getName())) {
        method.setName(changeInfo.getNewName());
      }
    }

    final GrModifierList modifierList = method.getModifierList();
    if (changeInfo.isVisibilityChanged()) {
      modifierList.setModifierProperty(changeInfo.getNewVisibility(), true);
    }

    PsiSubstitutor substitutor =
        baseMethod != null ? calculateSubstitutor(method, baseMethod) : PsiSubstitutor.EMPTY;

    final PsiMethod context = changeInfo.getMethod();
    GrTypeElement oldReturnTypeElement = method.getReturnTypeElementGroovy();
    if (changeInfo.isReturnTypeChanged()) {
      CanonicalTypes.Type newReturnType = changeInfo.getNewReturnType();
      if (newReturnType == null) {
        if (oldReturnTypeElement != null) {
          oldReturnTypeElement.delete();
          if (modifierList.getModifiers().length == 0) {
            modifierList.setModifierProperty(GrModifier.DEF, true);
          }
        }
      } else {
        PsiType type = newReturnType.getType(context, method.getManager());
        GrReferenceAdjuster.shortenAllReferencesIn(
            method.setReturnType(substitutor.substitute(type)));
        if (oldReturnTypeElement == null) {
          modifierList.setModifierProperty(GrModifier.DEF, false);
        }
      }
    }

    JavaParameterInfo[] newParameters = changeInfo.getNewParameters();
    final GrParameterList parameterList = method.getParameterList();
    GrParameter[] oldParameters = parameterList.getParameters();
    final PsiParameter[] oldBaseParams =
        baseMethod != null ? baseMethod.getParameterList().getParameters() : null;

    Set<GrParameter> toRemove = new HashSet<GrParameter>(oldParameters.length);
    ContainerUtil.addAll(toRemove, oldParameters);

    GrParameter anchor = null;
    final GrDocComment docComment = method.getDocComment();
    final GrDocTag[] tags = docComment == null ? null : docComment.getTags();

    for (JavaParameterInfo newParameter : newParameters) {
      // if old parameter name differs from base method parameter name we don't change it
      final String newName;
      final int oldIndex = newParameter.getOldIndex();
      if (oldIndex >= 0 && oldBaseParams != null) {
        final String oldName = oldParameters[oldIndex].getName();
        if (oldName.equals(oldBaseParams[oldIndex].getName())) {
          newName = newParameter.getName();
        } else {
          newName = oldName;
        }
      } else {
        newName = newParameter.getName();
      }

      final GrParameter oldParameter = oldIndex >= 0 ? oldParameters[oldIndex] : null;

      if (docComment != null && oldParameter != null) {
        final String oldName = oldParameter.getName();
        for (GrDocTag tag : tags) {
          if ("@param".equals(tag.getName())) {
            final GrDocParameterReference parameterReference = tag.getDocParameterReference();
            if (parameterReference != null && oldName.equals(parameterReference.getText())) {
              parameterReference.handleElementRename(newName);
            }
          }
        }
      }

      GrParameter grParameter =
          createNewParameter(substitutor, context, parameterList, newParameter, newName);
      if (oldParameter != null) {
        grParameter.getModifierList().replace(oldParameter.getModifierList());
      }

      if ("def".equals(newParameter.getTypeText())) {
        grParameter.getModifierList().setModifierProperty(GrModifier.DEF, true);
      } else if (StringUtil.isEmpty(newParameter.getTypeText())) {
        grParameter.getModifierList().setModifierProperty(GrModifier.DEF, false);
      }

      anchor = (GrParameter) parameterList.addAfter(grParameter, anchor);
    }

    for (GrParameter oldParameter : toRemove) {
      oldParameter.delete();
    }
    JavaCodeStyleManager.getInstance(parameterList.getProject())
        .shortenClassReferences(parameterList);
    CodeStyleManager.getInstance(parameterList.getProject()).reformat(parameterList);

    if (changeInfo.isExceptionSetOrOrderChanged()) {
      final ThrownExceptionInfo[] infos = changeInfo.getNewExceptions();
      PsiClassType[] exceptionTypes = new PsiClassType[infos.length];
      for (int i = 0; i < infos.length; i++) {
        ThrownExceptionInfo info = infos[i];
        exceptionTypes[i] = (PsiClassType) info.createType(method, method.getManager());
      }

      PsiReferenceList thrownList =
          GroovyPsiElementFactory.getInstance(method.getProject()).createThrownList(exceptionTypes);
      thrownList = (PsiReferenceList) method.getThrowsList().replace(thrownList);
      JavaCodeStyleManager.getInstance(thrownList.getProject()).shortenClassReferences(thrownList);
      CodeStyleManager.getInstance(method.getProject()).reformat(method.getThrowsList());
    }
    return true;
  }