protected boolean invokeImpl(
     final Project project, final PsiLocalVariable localVariable, final Editor editor) {
   final PsiElement parent = localVariable.getParent();
   if (!(parent instanceof PsiDeclarationStatement)) {
     String message =
         RefactoringBundle.getCannotRefactorMessage(
             RefactoringBundle.message("error.wrong.caret.position.local.or.expression.name"));
     CommonRefactoringUtil.showErrorHint(project, editor, message, REFACTORING_NAME, getHelpID());
     return false;
   }
   final LocalToFieldHandler localToFieldHandler =
       new LocalToFieldHandler(project, true) {
         @Override
         protected Settings showRefactoringDialog(
             PsiClass aClass,
             PsiLocalVariable local,
             PsiExpression[] occurences,
             boolean isStatic) {
           return IntroduceConstantHandler.this.showRefactoringDialog(
               project,
               editor,
               aClass,
               local.getInitializer(),
               local.getType(),
               occurences,
               local,
               null);
         }
       };
   return localToFieldHandler.convertLocalToField(localVariable, editor);
 }
 public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
   final PsiLocalVariable variable = (PsiLocalVariable) element;
   final PsiReference[] references =
       ReferencesSearch.search(variable, variable.getUseScope(), false)
           .toArray(PsiReference.EMPTY_ARRAY);
   final PsiCodeBlock tightestBlock = MoveDeclarationPredicate.getTightestBlock(references);
   assert tightestBlock != null;
   final PsiDeclarationStatement declaration = (PsiDeclarationStatement) variable.getParent();
   final PsiReference firstReference = references[0];
   final PsiElement referenceElement = firstReference.getElement();
   PsiDeclarationStatement newDeclaration;
   if (tightestBlock.equals(PsiTreeUtil.getParentOfType(referenceElement, PsiCodeBlock.class))) {
     // containing block of first reference is the same as the common
     //  block of all.
     newDeclaration = moveDeclarationToReference(referenceElement, variable, tightestBlock);
   } else {
     // declaration must be moved to common block (first reference block
     // is too deep)
     final PsiElement child =
         MoveDeclarationPredicate.getChildWhichContainsElement(tightestBlock, referenceElement);
     newDeclaration = createNewDeclaration(variable, null);
     newDeclaration = (PsiDeclarationStatement) tightestBlock.addBefore(newDeclaration, child);
   }
   assert declaration != null;
   if (declaration.getDeclaredElements().length == 1) {
     declaration.delete();
   } else {
     variable.delete();
   }
   highlightElement(newDeclaration);
 }
  public boolean convertLocalToField(final PsiLocalVariable local, final Editor editor) {
    boolean tempIsStatic = myIsConstant;
    PsiElement parent = local.getParent();
    List<PsiClass> classes = new ArrayList<>();

    if ("gr".equalsIgnoreCase(local.getContainingFile().getVirtualFile().getExtension())) {
      // this is special ij GW studio check for rule files
      CommonRefactoringUtil.showErrorHint(
          myProject,
          editor,
          "Introduce field is not supported for rule files",
          REFACTORING_NAME,
          HelpID.LOCAL_TO_FIELD);
      return false;
    }

    while (parent != null && parent.getContainingFile() != null) {
      if (parent instanceof PsiClass && !(myIsConstant && parent instanceof PsiAnonymousClass)) {
        classes.add((PsiClass) parent);
      }
      if (parent instanceof PsiFile && JspPsiUtil.isInJspFile(parent)) {
        String message = RefactoringBundle.message("error.not.supported.for.jsp", REFACTORING_NAME);
        CommonRefactoringUtil.showErrorHint(
            myProject, editor, message, REFACTORING_NAME, HelpID.LOCAL_TO_FIELD);
        return false;
      }
      if (parent instanceof PsiModifierListOwner
          && ((PsiModifierListOwner) parent).hasModifierProperty(PsiModifier.STATIC)) {
        tempIsStatic = true;
      }
      parent = parent.getParent();
    }

    if (classes.isEmpty()) {
      return false;
    }
    if (classes.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
      if (convertLocalToField(
          local, classes.get(getChosenClassIndex(classes)), editor, tempIsStatic)) {
        return false;
      }
    } else {
      final boolean isStatic = tempIsStatic;
      NavigationUtil.getPsiElementPopup(
              classes.toArray(new PsiClass[classes.size()]),
              new PsiClassListCellRenderer(),
              "Choose class to introduce " + (myIsConstant ? "constant" : "field"),
              new PsiElementProcessor<PsiClass>() {
                @Override
                public boolean execute(@NotNull PsiClass aClass) {
                  convertLocalToField(local, aClass, editor, isStatic);
                  return false;
                }
              })
          .showInBestPositionFor(editor);
    }

    return true;
  }
  public boolean convertLocalToField(final PsiLocalVariable local, final Editor editor) {
    boolean tempIsStatic = myIsConstant;
    PsiElement parent = local.getParent();
    List<PsiClass> classes = new ArrayList<PsiClass>();
    while (parent != null && parent.getContainingFile() != null) {
      if (parent instanceof PsiClass && !(myIsConstant && parent instanceof PsiAnonymousClass)) {
        classes.add((PsiClass) parent);
      }
      if (parent instanceof PsiFile && FileTypeUtils.isInServerPageFile(parent)) {
        String message = RefactoringBundle.message("error.not.supported.for.jsp", REFACTORING_NAME);
        CommonRefactoringUtil.showErrorHint(
            myProject, editor, message, REFACTORING_NAME, HelpID.LOCAL_TO_FIELD);
        return false;
      }
      if (parent instanceof PsiModifierListOwner
          && ((PsiModifierListOwner) parent).hasModifierProperty(PsiModifier.STATIC)) {
        tempIsStatic = true;
      }
      parent = parent.getParent();
    }

    if (classes.isEmpty()) return false;
    final AbstractInplaceIntroducer activeIntroducer =
        AbstractInplaceIntroducer.getActiveIntroducer(editor);
    final boolean shouldSuggestDialog =
        activeIntroducer instanceof InplaceIntroduceConstantPopup
            && activeIntroducer.startsOnTheSameElement(null, local);
    if (classes.size() == 1
        || ApplicationManager.getApplication().isUnitTestMode()
        || shouldSuggestDialog) {
      if (convertLocalToField(
          local, classes.get(getChosenClassIndex(classes)), editor, tempIsStatic)) return false;
    } else {
      final boolean isStatic = tempIsStatic;
      NavigationUtil.getPsiElementPopup(
              classes.toArray(new PsiClass[classes.size()]),
              new PsiClassListCellRenderer(),
              "Choose class to introduce " + (myIsConstant ? "constant" : "field"),
              new PsiElementProcessor<PsiClass>() {
                @Override
                public boolean execute(@NotNull PsiClass aClass) {
                  convertLocalToField(local, aClass, editor, isStatic);
                  return false;
                }
              })
          .showInBestPositionFor(editor);
    }

    return true;
  }
  protected void performRefactoring(UsageInfo[] usages) {
    try {
      PsiElementFactory factory =
          JavaPsiFacade.getInstance(myManager.getProject()).getElementFactory();
      PsiType initializerType =
          getInitializerType(myForcedType, myParameterInitializer, myLocalVariable);
      setForcedType(initializerType);

      // Converting myParameterInitializer
      if (myParameterInitializer == null) {
        LOG.assertTrue(myLocalVariable != null);
        myParameterInitializer =
            factory.createExpressionFromText(myLocalVariable.getName(), myLocalVariable);
      } else if (myParameterInitializer instanceof PsiArrayInitializerExpression) {
        final PsiExpression newExprArrayInitializer =
            RefactoringUtil.createNewExpressionFromArrayInitializer(
                (PsiArrayInitializerExpression) myParameterInitializer, initializerType);
        myParameterInitializer =
            (PsiExpression) myParameterInitializer.replace(newExprArrayInitializer);
      }

      myInitializerWrapper = new JavaExpressionWrapper(myParameterInitializer);

      // Changing external occurences (the tricky part)

      IntroduceParameterUtil.processUsages(usages, this);

      if (myGenerateDelegate) {
        generateDelegate(myMethodToReplaceIn);
        if (myMethodToReplaceIn != myMethodToSearchFor) {
          final PsiMethod method = generateDelegate(myMethodToSearchFor);
          if (method.getContainingClass().isInterface()) {
            final PsiCodeBlock block = method.getBody();
            if (block != null) {
              block.delete();
            }
          }
        }
      }

      // Changing signature of initial method
      // (signature of myMethodToReplaceIn will be either changed now or have already been changed)
      LOG.assertTrue(initializerType.isValid());
      final FieldConflictsResolver fieldConflictsResolver =
          new FieldConflictsResolver(myParameterName, myMethodToReplaceIn.getBody());
      IntroduceParameterUtil.changeMethodSignatureAndResolveFieldConflicts(
          new UsageInfo(myMethodToReplaceIn), usages, this);
      if (myMethodToSearchFor != myMethodToReplaceIn) {
        IntroduceParameterUtil.changeMethodSignatureAndResolveFieldConflicts(
            new UsageInfo(myMethodToSearchFor), usages, this);
      }
      ChangeContextUtil.clearContextInfo(myParameterInitializer);

      // Replacing expression occurrences
      for (UsageInfo usage : usages) {
        if (usage instanceof ChangedMethodCallInfo) {
          PsiElement element = usage.getElement();

          processChangedMethodCall(element);
        } else if (usage instanceof InternalUsageInfo) {
          PsiElement element = usage.getElement();
          if (element instanceof PsiExpression) {
            element = RefactoringUtil.outermostParenthesizedExpression((PsiExpression) element);
          }
          if (element != null) {
            if (element.getParent() instanceof PsiExpressionStatement) {
              element.getParent().delete();
            } else {
              PsiExpression newExpr = factory.createExpressionFromText(myParameterName, element);
              IntroduceVariableBase.replace((PsiExpression) element, newExpr, myProject);
            }
          }
        }
      }

      if (myLocalVariable != null && myRemoveLocalVariable) {
        myLocalVariable.normalizeDeclaration();
        myLocalVariable.getParent().delete();
      }
      fieldConflictsResolver.fix();
    } catch (IncorrectOperationException ex) {
      LOG.error(ex);
    }
  }
    public void run() {
      try {
        final boolean rebindNeeded2 = !myVariableName.equals(myFieldName) || myRebindNeeded;
        final PsiReference[] refs;
        if (rebindNeeded2) {
          refs =
              ReferencesSearch.search(myLocal, GlobalSearchScope.projectScope(myProject), false)
                  .toArray(new PsiReference[0]);
        } else {
          refs = null;
        }

        final PsiMethod enclosingConstructor =
            BaseExpressionToFieldHandler.getEnclosingConstructor(myDestinationClass, myLocal);
        myField =
            mySettings.isIntroduceEnumConstant()
                ? EnumConstantsUtil.createEnumConstant(myDestinationClass, myLocal, myFieldName)
                : createField(
                    myLocal,
                    mySettings.getForcedType(),
                    myFieldName,
                    myInitializerPlace == IN_FIELD_DECLARATION);
        myField = (PsiField) myDestinationClass.add(myField);
        BaseExpressionToFieldHandler.setModifiers(myField, mySettings);
        if (!mySettings.isIntroduceEnumConstant()) {
          VisibilityUtil.fixVisibility(myOccurences, myField, mySettings.getFieldVisibility());
        }

        myLocal.normalizeDeclaration();
        PsiDeclarationStatement declarationStatement =
            (PsiDeclarationStatement) myLocal.getParent();
        final BaseExpressionToFieldHandler.InitializationPlace finalInitializerPlace;
        if (myLocal.getInitializer() == null) {
          finalInitializerPlace = IN_FIELD_DECLARATION;
        } else {
          finalInitializerPlace = myInitializerPlace;
        }
        final PsiElementFactory factory = JavaPsiFacade.getInstance(myProject).getElementFactory();

        switch (finalInitializerPlace) {
          case IN_FIELD_DECLARATION:
            declarationStatement.delete();
            break;

          case IN_CURRENT_METHOD:
            PsiStatement statement = createAssignment(myLocal, myFieldName, factory);
            myAssignmentStatement = (PsiStatement) declarationStatement.replace(statement);
            break;

          case IN_CONSTRUCTOR:
            myAssignmentStatement =
                addInitializationToConstructors(myLocal, myField, enclosingConstructor, factory);
            break;
          case IN_SETUP_METHOD:
            myAssignmentStatement = addInitializationToSetUp(myLocal, myField, factory);
        }

        if (enclosingConstructor != null && myInitializerPlace == IN_CONSTRUCTOR) {
          PsiStatement statement = createAssignment(myLocal, myFieldName, factory);
          myAssignmentStatement = (PsiStatement) declarationStatement.replace(statement);
        }

        if (rebindNeeded2) {
          for (final PsiReference reference : refs) {
            if (reference != null) {
              // expr = RefactoringUtil.outermostParenthesizedExpression(expr);
              RefactoringUtil.replaceOccurenceWithFieldRef(
                  (PsiExpression) reference, myField, myDestinationClass);
              // replaceOccurenceWithFieldRef((PsiExpression)reference, field, aaClass);
            }
          }
          // RefactoringUtil.renameVariableReferences(local, pPrefix + fieldName,
          // GlobalSearchScope.projectScope(myProject));
        }
      } catch (IncorrectOperationException e) {
        LOG.error(e);
      }
    }