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);
 }
  private static PsiField createField(
      PsiLocalVariable local, PsiType forcedType, String fieldName, boolean includeInitializer) {
    @NonNls StringBuilder pattern = new StringBuilder();
    pattern.append("private int ");
    pattern.append(fieldName);
    if (local.getInitializer() == null) {
      includeInitializer = false;
    }
    if (includeInitializer) {
      pattern.append("=0");
    }
    pattern.append(";");
    final Project project = local.getProject();
    PsiElementFactory factory = JavaPsiFacade.getInstance(project).getElementFactory();
    try {
      PsiField field = factory.createFieldFromText(pattern.toString(), null);

      field.getTypeElement().replace(factory.createTypeElement(forcedType));
      if (includeInitializer) {
        PsiExpression initializer =
            RefactoringUtil.convertInitializerToNormalExpression(
                local.getInitializer(), forcedType);
        field.getInitializer().replace(initializer);
      }

      for (PsiAnnotation annotation : local.getModifierList().getAnnotations()) {
        field.getModifierList().add(annotation.copy());
      }
      return field;
    } catch (IncorrectOperationException e) {
      LOG.error(e);
      return null;
    }
  }
 private static void initialize(final PsiLocalVariable variable)
     throws IncorrectOperationException {
   PsiType type = variable.getType();
   String init = PsiTypesUtil.getDefaultValueOfType(type);
   PsiElementFactory factory =
       JavaPsiFacade.getInstance(variable.getProject()).getElementFactory();
   PsiExpression initializer = factory.createExpressionFromText(init, variable);
   variable.setInitializer(initializer);
 }
  private static PsiStatement addInitializationToConstructors(
      PsiLocalVariable local,
      PsiField field,
      PsiMethod enclosingConstructor,
      PsiElementFactory factory)
      throws IncorrectOperationException {
    PsiClass aClass = field.getContainingClass();
    PsiMethod[] constructors = aClass.getConstructors();
    PsiStatement assignment = createAssignment(local, field.getName(), factory);
    boolean added = false;
    for (PsiMethod constructor : constructors) {
      if (constructor == enclosingConstructor) continue;
      PsiCodeBlock body = constructor.getBody();
      if (body == null) continue;
      PsiStatement[] statements = body.getStatements();
      if (statements.length > 0) {
        PsiStatement first = statements[0];
        if (first instanceof PsiExpressionStatement) {
          PsiExpression expression = ((PsiExpressionStatement) first).getExpression();
          if (expression instanceof PsiMethodCallExpression) {
            @NonNls
            String text = ((PsiMethodCallExpression) expression).getMethodExpression().getText();
            if ("this".equals(text)) {
              continue;
            }
            if ("super".equals(text)
                && enclosingConstructor == null
                && PsiTreeUtil.isAncestor(constructor, local, false)) {
              local.delete();
              return (PsiStatement) body.addAfter(assignment, first);
            }
          }
        }
        if (enclosingConstructor == null && PsiTreeUtil.isAncestor(constructor, local, false)) {
          local.delete();
          return (PsiStatement) body.addBefore(assignment, first);
        }
      }

      assignment = (PsiStatement) body.add(assignment);
      added = true;
    }
    if (!added && enclosingConstructor == null) {
      if (aClass instanceof PsiAnonymousClass) {
        final PsiClassInitializer classInitializer =
            (PsiClassInitializer) aClass.addAfter(factory.createClassInitializer(), field);
        assignment = (PsiStatement) classInitializer.getBody().add(assignment);
      } else {
        PsiMethod constructor = (PsiMethod) aClass.add(factory.createConstructor());
        assignment = (PsiStatement) constructor.getBody().add(assignment);
      }
    }

    if (enclosingConstructor == null) local.delete();
    return assignment;
  }
 @Override
 protected boolean accept(ElementToWorkOn elementToWorkOn) {
   final PsiExpression expr = elementToWorkOn.getExpression();
   if (expr != null) {
     return isStaticFinalInitializer(expr) == null;
   }
   final PsiLocalVariable localVariable = elementToWorkOn.getLocalVariable();
   final PsiExpression initializer = localVariable.getInitializer();
   return initializer != null && isStaticFinalInitializer(initializer) == null;
 }
 @Override
 public void visitReferenceExpression(PsiReferenceExpression expression) {
   if (argumentsContainCatchParameter || !visited.add(expression)) {
     return;
   }
   super.visitReferenceExpression(expression);
   final PsiElement target = expression.resolve();
   if (!parameter.equals(target)) {
     if (target instanceof PsiLocalVariable) {
       final PsiLocalVariable variable = (PsiLocalVariable) target;
       final Query<PsiReference> query =
           ReferencesSearch.search(variable, variable.getUseScope(), false);
       query.forEach(
           reference -> {
             final PsiElement element = reference.getElement();
             final PsiElement parent =
                 PsiTreeUtil.skipParentsOfType(element, PsiParenthesizedExpression.class);
             if (!(parent instanceof PsiReferenceExpression)) {
               return true;
             }
             final PsiElement grandParent = parent.getParent();
             if (!(grandParent instanceof PsiMethodCallExpression)) {
               return true;
             }
             final PsiMethodCallExpression methodCallExpression =
                 (PsiMethodCallExpression) grandParent;
             final PsiExpressionList argumentList = methodCallExpression.getArgumentList();
             final PsiExpression[] arguments = argumentList.getExpressions();
             for (PsiExpression argument : arguments) {
               argument.accept(ReferenceFinder.this);
             }
             return true;
           });
       final PsiExpression initializer = variable.getInitializer();
       if (initializer != null) {
         initializer.accept(this);
       }
     }
     return;
   }
   if (ignoreGetMessage) {
     argumentsContainCatchParameter = true;
   } else {
     final PsiElement parent = expression.getParent();
     if (parent instanceof PsiReferenceExpression) {
       final PsiElement grandParent = parent.getParent();
       if (grandParent instanceof PsiMethodCallExpression) {
         return;
       }
     }
     argumentsContainCatchParameter = true;
   }
 }
 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 IntroduceFieldRunnable(
     boolean rebindNeeded,
     PsiLocalVariable local,
     PsiClass aClass,
     BaseExpressionToFieldHandler.Settings settings,
     boolean isStatic,
     PsiExpression[] occurrences) {
   myVariableName = local.getName();
   myFieldName = settings.getFieldName();
   myRebindNeeded = rebindNeeded;
   myLocal = local;
   myProject = local.getProject();
   myDestinationClass = aClass;
   mySettings = settings;
   myInitializerPlace = settings.getInitializerPlace();
   myOccurences = occurrences;
 }
 @Override
 public void visitForStatement(@NotNull PsiForStatement statement) {
   super.visitForStatement(statement);
   final PsiStatement initialization = statement.getInitialization();
   if (!(initialization instanceof PsiDeclarationStatement)) {
     return;
   }
   final PsiDeclarationStatement declaration = (PsiDeclarationStatement) initialization;
   final PsiElement[] declaredElements = declaration.getDeclaredElements();
   if (declaredElements.length != 1) {
     return;
   }
   final PsiElement declaredElement = declaredElements[0];
   if (!(declaredElement instanceof PsiLocalVariable)) {
     return;
   }
   final PsiLocalVariable variable = (PsiLocalVariable) declaredElement;
   final PsiExpression initialValue = variable.getInitializer();
   if (initialValue == null) {
     return;
   }
   final PsiExpression condition = statement.getCondition();
   if (!ExpressionUtils.isVariableLessThanComparison(condition, variable)) {
     return;
   }
   final PsiStatement update = statement.getUpdate();
   if (!VariableAccessUtils.variableIsIncremented(variable, update)) {
     return;
   }
   final PsiArrayAccessExpression arrayAccessExpression = getArrayAccessExpression(statement);
   if (arrayAccessExpression == null) {
     return;
   }
   final PsiExpression arrayExpression = arrayAccessExpression.getArrayExpression();
   final PsiType type = arrayExpression.getType();
   if (!(type instanceof PsiArrayType)
       || type.getDeepComponentType() instanceof PsiPrimitiveType) {
     return;
   }
   final PsiStatement body = statement.getBody();
   if (!bodyIsArrayToCollectionCopy(body, variable, true)) {
     return;
   }
   registerStatementError(statement);
 }
示例#10
0
 @Nullable
 private static String getExprText(PsiExpression expr, PsiLocalVariable localVariable) {
   final String exprText = expr != null ? expr.getText() : null;
   if (localVariable != null) {
     final PsiExpression initializer = localVariable.getInitializer();
     return initializer != null ? initializer.getText() : exprText;
   } else {
     return exprText;
   }
 }
 public static boolean containsConflictingDeclarations(
     PsiCodeBlock block, PsiCodeBlock parentBlock) {
   final PsiStatement[] statements = block.getStatements();
   if (statements.length == 0) {
     return false;
   }
   final List<PsiCodeBlock> followingBlocks = new ArrayList<>();
   collectFollowingBlocks(block.getParent().getNextSibling(), followingBlocks);
   final Project project = block.getProject();
   final JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
   final PsiResolveHelper resolveHelper = facade.getResolveHelper();
   for (final PsiStatement statement : statements) {
     if (!(statement instanceof PsiDeclarationStatement)) {
       continue;
     }
     final PsiDeclarationStatement declaration = (PsiDeclarationStatement) statement;
     final PsiElement[] variables = declaration.getDeclaredElements();
     for (PsiElement variable : variables) {
       if (!(variable instanceof PsiLocalVariable)) {
         continue;
       }
       final PsiLocalVariable localVariable = (PsiLocalVariable) variable;
       final String variableName = localVariable.getName();
       if (variableName == null) {
         continue;
       }
       final PsiVariable target =
           resolveHelper.resolveAccessibleReferencedVariable(variableName, parentBlock);
       if (target instanceof PsiLocalVariable) {
         return true;
       }
       for (PsiCodeBlock codeBlock : followingBlocks) {
         final PsiVariable target1 =
             resolveHelper.resolveAccessibleReferencedVariable(variableName, codeBlock);
         if (target1 instanceof PsiLocalVariable) {
           return true;
         }
       }
     }
   }
   return false;
 }
 @Override
 public void visitReferenceExpression(@NotNull PsiReferenceExpression ref) {
   if (used) {
     return;
   }
   super.visitReferenceExpression(ref);
   final PsiElement resolvedElement = ref.resolve();
   if (variable.equals(resolvedElement)) {
     used = true;
   }
 }
  private static PsiStatement createAssignment(
      PsiLocalVariable local, String fieldname, PsiElementFactory factory) {
    try {
      String pattern = fieldname + "=0;";
      PsiExpressionStatement statement =
          (PsiExpressionStatement) factory.createStatementFromText(pattern, null);
      statement =
          (PsiExpressionStatement)
              CodeStyleManager.getInstance(local.getProject()).reformat(statement);

      PsiAssignmentExpression expr = (PsiAssignmentExpression) statement.getExpression();
      final PsiExpression initializer =
          RefactoringUtil.convertInitializerToNormalExpression(
              local.getInitializer(), local.getType());
      expr.getRExpression().replace(initializer);

      return statement;
    } catch (IncorrectOperationException e) {
      LOG.error(e);
      return null;
    }
  }
  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;
  }
 @Nullable
 private static String buildFromOffsetText(PsiExpression expression, PsiLocalVariable variable)
     throws IncorrectOperationException {
   expression = ParenthesesUtils.stripParentheses(expression);
   if (expression == null) {
     return null;
   }
   final String expressionText = expression.getText();
   final String variableName = variable.getName();
   if (expressionText.equals(variableName)) {
     final PsiExpression initialValue = variable.getInitializer();
     if (initialValue == null) {
       return null;
     }
     return initialValue.getText();
   }
   if (expression instanceof PsiBinaryExpression) {
     final PsiBinaryExpression binaryExpression = (PsiBinaryExpression) expression;
     final PsiExpression lhs = binaryExpression.getLOperand();
     final PsiExpression rhs = binaryExpression.getROperand();
     final String rhsText = buildFromOffsetText(rhs, variable);
     final PsiJavaToken sign = binaryExpression.getOperationSign();
     final IElementType tokenType = sign.getTokenType();
     if (ExpressionUtils.isZero(lhs)) {
       if (tokenType.equals(JavaTokenType.MINUS)) {
         return '-' + rhsText;
       }
       return rhsText;
     }
     final String lhsText = buildFromOffsetText(lhs, variable);
     if (ExpressionUtils.isZero(rhs)) {
       return lhsText;
     }
     return collapseConstant(lhsText + sign.getText() + rhsText, variable);
   }
   return collapseConstant(expression.getText(), variable);
 }
 static PsiType getInitializerType(
     PsiType forcedType, PsiExpression parameterInitializer, PsiLocalVariable localVariable) {
   final PsiType initializerType;
   if (forcedType == null) {
     if (parameterInitializer == null) {
       if (localVariable != null) {
         initializerType = localVariable.getType();
       } else {
         LOG.assertTrue(false);
         initializerType = null;
       }
     } else {
       if (localVariable == null) {
         initializerType =
             RefactoringUtil.getTypeByExpressionWithExpectedType(parameterInitializer);
       } else {
         initializerType = localVariable.getType();
       }
     }
   } else {
     initializerType = forcedType;
   }
   return initializerType;
 }
 @Override
 public void visitLocalVariable(@NotNull PsiLocalVariable variable) {
   super.visitLocalVariable(variable);
   final PsiCodeBlock codeBlock = PsiTreeUtil.getParentOfType(variable, PsiCodeBlock.class);
   if (!shouldCheckVariable(variable, codeBlock)) {
     return;
   }
   final ArrayReadWriteVisitor visitor =
       new ArrayReadWriteVisitor(variable, !isSimpleArrayExpression(variable.getInitializer()));
   codeBlock.accept(visitor);
   final boolean written = visitor.isWritten();
   if (!visitor.isReferenced() || written == visitor.isRead()) {
     return;
   }
   registerVariableError(variable, Boolean.valueOf(written));
 }
 private static PsiStatement addInitializationToSetUp(
     final PsiLocalVariable local, final PsiField field, final PsiElementFactory factory)
     throws IncorrectOperationException {
   PsiMethod inClass =
       TestFrameworks.getInstance().findOrCreateSetUpMethod(field.getContainingClass());
   assert inClass != null;
   PsiStatement assignment = createAssignment(local, field.getName(), factory);
   final PsiCodeBlock body = inClass.getBody();
   assert body != null;
   if (PsiTreeUtil.isAncestor(body, local, false)) {
     assignment =
         (PsiStatement)
             body.addBefore(assignment, PsiTreeUtil.getParentOfType(local, PsiStatement.class));
   } else {
     assignment = (PsiStatement) body.add(assignment);
   }
   local.delete();
   return assignment;
 }
示例#19
0
  protected void doAction() {
    final JavaRefactoringSettings settings = JavaRefactoringSettings.getInstance();
    settings.INTRODUCE_PARAMETER_REPLACE_FIELDS_WITH_GETTERS =
        myPanel.getReplaceFieldsWithGetters();
    if (myCbDeclareFinal != null && myCbDeclareFinal.isEnabled()) {
      settings.INTRODUCE_PARAMETER_CREATE_FINALS = Boolean.valueOf(myCbDeclareFinal.isSelected());
    }

    myPanel.saveSettings(settings);

    myNameSuggestionsManager.nameSelected();

    boolean isDeleteLocalVariable = false;

    PsiExpression parameterInitializer = myExpression;
    if (myLocalVar != null) {
      if (myPanel.isUseInitializer()) {
        parameterInitializer = myLocalVar.getInitializer();
      }
      isDeleteLocalVariable = myPanel.isDeleteLocalVariable();
    }

    final IntroduceParameterProcessor processor =
        new IntroduceParameterProcessor(
            myProject,
            myMethodToReplaceIn,
            myMethodToSearchFor,
            parameterInitializer,
            myExpression,
            myLocalVar,
            isDeleteLocalVariable,
            getParameterName(),
            myPanel.isReplaceAllOccurences(),
            myPanel.getReplaceFieldsWithGetters(),
            isDeclareFinal(),
            myPanel.isGenerateDelegate(),
            getSelectedType(),
            myPanel.getParametersToRemove());
    invokeRefactoring(processor);
    myParameterNameField.requestFocusInWindow();
  }
 private static boolean localVariableAreEquivalent(
     @NotNull PsiLocalVariable var1, @NotNull PsiLocalVariable var2) {
   final PsiType type1 = var1.getType();
   final PsiType type2 = var2.getType();
   if (!typesAreEquivalent(type1, type2)) {
     return false;
   }
   final String name1 = var1.getName();
   final String name2 = var2.getName();
   if (!name1.equals(name2)) {
     return false;
   }
   final PsiExpression initializer1 = var1.getInitializer();
   final PsiExpression initializer2 = var2.getInitializer();
   return expressionsAreEquivalent(initializer1, initializer2);
 }
 private static PsiDeclarationStatement createNewDeclaration(
     @NotNull PsiLocalVariable variable, PsiExpression initializer)
     throws IncorrectOperationException {
   final PsiManager manager = variable.getManager();
   final PsiElementFactory factory =
       JavaPsiFacade.getInstance(manager.getProject()).getElementFactory();
   final PsiDeclarationStatement newDeclaration =
       factory.createVariableDeclarationStatement(
           variable.getName(), variable.getType(), initializer);
   if (variable.hasModifierProperty(PsiModifier.FINAL)) {
     final PsiLocalVariable newVariable =
         (PsiLocalVariable) newDeclaration.getDeclaredElements()[0];
     final PsiModifierList modifierList = newVariable.getModifierList();
     modifierList.setModifierProperty(PsiModifier.FINAL, true);
   }
   return newDeclaration;
 }
示例#22
0
  public static boolean isAcceptable(
      PsiLambdaExpression lambdaExpression, final PsiType leftType, boolean checkReturnType) {
    if (leftType instanceof PsiIntersectionType) {
      for (PsiType conjunctType : ((PsiIntersectionType) leftType).getConjuncts()) {
        if (isAcceptable(lambdaExpression, conjunctType, checkReturnType)) return true;
      }
      return false;
    }
    final PsiClassType.ClassResolveResult resolveResult =
        PsiUtil.resolveGenericsClassInType(GenericsUtil.eliminateWildcards(leftType));
    final PsiClass psiClass = resolveResult.getElement();
    if (psiClass instanceof PsiAnonymousClass) {
      return isAcceptable(
          lambdaExpression, ((PsiAnonymousClass) psiClass).getBaseClassType(), checkReturnType);
    }
    final MethodSignature methodSignature = getFunction(psiClass);
    if (methodSignature == null) return false;
    final PsiParameter[] lambdaParameters = lambdaExpression.getParameterList().getParameters();
    final PsiType[] parameterTypes = methodSignature.getParameterTypes();
    if (lambdaParameters.length != parameterTypes.length) return false;
    for (int lambdaParamIdx = 0, length = lambdaParameters.length;
        lambdaParamIdx < length;
        lambdaParamIdx++) {
      PsiParameter parameter = lambdaParameters[lambdaParamIdx];
      final PsiTypeElement typeElement = parameter.getTypeElement();
      if (typeElement != null) {
        final PsiType lambdaFormalType = typeElement.getType();
        final PsiType methodParameterType = parameterTypes[lambdaParamIdx];
        if (lambdaFormalType instanceof PsiPrimitiveType) {
          if (methodParameterType instanceof PsiPrimitiveType)
            return methodParameterType.equals(lambdaFormalType);
          return false;
        }

        if (!TypeConversionUtil.erasure(lambdaFormalType)
            .isAssignableFrom(
                TypeConversionUtil.erasure(
                    GenericsUtil.eliminateWildcards(
                        resolveResult
                            .getSubstitutor()
                            .substitute(
                                methodSignature
                                    .getSubstitutor()
                                    .substitute(methodParameterType)))))) {
          return false;
        }
      }
    }
    if (checkReturnType) {
      final String uniqueVarName =
          JavaCodeStyleManager.getInstance(lambdaExpression.getProject())
              .suggestUniqueVariableName("l", lambdaExpression, true);
      String canonicalText = leftType.getCanonicalText();
      if (leftType instanceof PsiEllipsisType) {
        canonicalText = ((PsiEllipsisType) leftType).toArrayType().getCanonicalText();
      }
      final PsiStatement assignmentFromText =
          JavaPsiFacade.getElementFactory(lambdaExpression.getProject())
              .createStatementFromText(
                  canonicalText + " " + uniqueVarName + " = " + lambdaExpression.getText(),
                  lambdaExpression);
      final PsiLocalVariable localVariable =
          (PsiLocalVariable)
              ((PsiDeclarationStatement) assignmentFromText).getDeclaredElements()[0];
      LOG.assertTrue(psiClass != null);
      PsiType methodReturnType = getReturnType(psiClass, methodSignature);
      if (methodReturnType != null) {
        methodReturnType =
            resolveResult
                .getSubstitutor()
                .substitute(methodSignature.getSubstitutor().substitute(methodReturnType));
        return LambdaHighlightingUtil.checkReturnTypeCompatible(
                (PsiLambdaExpression) localVariable.getInitializer(), methodReturnType)
            == null;
      }
    }
    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 invoke(@NotNull Project project, Editor editor, PsiFile file)
      throws IncorrectOperationException {
    LOG.assertTrue(myOutOfScopeVariable != null);
    PsiManager manager = file.getManager();
    myOutOfScopeVariable.normalizeDeclaration();
    PsiUtil.setModifierProperty(myOutOfScopeVariable, PsiModifier.FINAL, false);
    PsiElement commonParent =
        PsiTreeUtil.findCommonParent(myOutOfScopeVariable, myUnresolvedReference);
    LOG.assertTrue(commonParent != null);
    PsiElement child =
        myOutOfScopeVariable.getTextRange().getStartOffset()
                < myUnresolvedReference.getTextRange().getStartOffset()
            ? myOutOfScopeVariable
            : myUnresolvedReference;

    while (child.getParent() != commonParent) child = child.getParent();
    PsiDeclarationStatement newDeclaration =
        (PsiDeclarationStatement)
            JavaPsiFacade.getInstance(manager.getProject())
                .getElementFactory()
                .createStatementFromText("int i = 0", null);
    PsiVariable variable =
        (PsiVariable) newDeclaration.getDeclaredElements()[0].replace(myOutOfScopeVariable);
    if (variable.getInitializer() != null) {
      variable.getInitializer().delete();
    }

    while (!(child instanceof PsiStatement) || !(child.getParent() instanceof PsiCodeBlock)) {
      child = child.getParent();
      commonParent = commonParent.getParent();
    }
    LOG.assertTrue(commonParent != null);
    PsiDeclarationStatement added =
        (PsiDeclarationStatement) commonParent.addBefore(newDeclaration, child);
    PsiLocalVariable addedVar = (PsiLocalVariable) added.getDeclaredElements()[0];
    manager.getCodeStyleManager().reformat(commonParent);

    // Leave initializer assignment
    PsiExpression initializer = myOutOfScopeVariable.getInitializer();
    if (initializer != null) {
      PsiExpressionStatement assignment =
          (PsiExpressionStatement)
              JavaPsiFacade.getInstance(manager.getProject())
                  .getElementFactory()
                  .createStatementFromText(myOutOfScopeVariable.getName() + "= e;", null);
      ((PsiAssignmentExpression) assignment.getExpression()).getRExpression().replace(initializer);
      assignment = (PsiExpressionStatement) manager.getCodeStyleManager().reformat(assignment);
      PsiDeclarationStatement declStatement =
          PsiTreeUtil.getParentOfType(myOutOfScopeVariable, PsiDeclarationStatement.class);
      LOG.assertTrue(declStatement != null);
      PsiElement parent = declStatement.getParent();
      if (parent instanceof PsiForStatement) {
        declStatement.replace(assignment);
      } else {
        parent.addAfter(assignment, declStatement);
      }
    }

    if (myOutOfScopeVariable.isValid()) {
      myOutOfScopeVariable.delete();
    }

    if (HighlightControlFlowUtil.checkVariableInitializedBeforeUsage(
            myUnresolvedReference,
            addedVar,
            new THashMap<PsiElement, Collection<PsiReferenceExpression>>())
        != null) {
      initialize(addedVar);
    }

    DaemonCodeAnalyzer.getInstance(project).updateVisibleHighlighters(editor);
  }
  protected Settings showRefactoringDialog(
      Project project,
      final Editor editor,
      PsiClass parentClass,
      PsiExpression expr,
      PsiType type,
      PsiExpression[] occurrences,
      PsiElement anchorElement,
      PsiElement anchorElementIfAll) {
    final PsiMethod containingMethod =
        PsiTreeUtil.getParentOfType(expr != null ? expr : anchorElement, PsiMethod.class);

    PsiLocalVariable localVariable = null;
    if (expr instanceof PsiReferenceExpression) {
      PsiElement ref = ((PsiReferenceExpression) expr).resolve();
      if (ref instanceof PsiLocalVariable) {
        localVariable = (PsiLocalVariable) ref;
      }
    } else if (anchorElement instanceof PsiLocalVariable) {
      localVariable = (PsiLocalVariable) anchorElement;
    }

    String enteredName = null;
    boolean replaceAllOccurrences = true;

    final AbstractInplaceIntroducer activeIntroducer =
        AbstractInplaceIntroducer.getActiveIntroducer(editor);
    if (activeIntroducer != null) {
      activeIntroducer.stopIntroduce(editor);
      expr = (PsiExpression) activeIntroducer.getExpr();
      localVariable = (PsiLocalVariable) activeIntroducer.getLocalVariable();
      occurrences = (PsiExpression[]) activeIntroducer.getOccurrences();
      enteredName = activeIntroducer.getInputName();
      replaceAllOccurrences = activeIntroducer.isReplaceAllOccurrences();
      type = ((InplaceIntroduceConstantPopup) activeIntroducer).getType();
    }

    for (PsiExpression occurrence : occurrences) {
      if (RefactoringUtil.isAssignmentLHS(occurrence)) {
        String message =
            RefactoringBundle.getCannotRefactorMessage("Selected expression is used for write");
        CommonRefactoringUtil.showErrorHint(
            project, editor, message, REFACTORING_NAME, getHelpID());
        highlightError(project, editor, occurrence);
        return null;
      }
    }

    if (localVariable == null) {
      final PsiElement errorElement = isStaticFinalInitializer(expr);
      if (errorElement != null) {
        String message =
            RefactoringBundle.getCannotRefactorMessage(
                RefactoringBundle.message("selected.expression.cannot.be.a.constant.initializer"));
        CommonRefactoringUtil.showErrorHint(
            project, editor, message, REFACTORING_NAME, getHelpID());
        highlightError(project, editor, errorElement);
        return null;
      }
    } else {
      final PsiExpression initializer = localVariable.getInitializer();
      if (initializer == null) {
        String message =
            RefactoringBundle.getCannotRefactorMessage(
                RefactoringBundle.message(
                    "variable.does.not.have.an.initializer", localVariable.getName()));
        CommonRefactoringUtil.showErrorHint(
            project, editor, message, REFACTORING_NAME, getHelpID());
        return null;
      }
      final PsiElement errorElement = isStaticFinalInitializer(initializer);
      if (errorElement != null) {
        String message =
            RefactoringBundle.getCannotRefactorMessage(
                RefactoringBundle.message(
                    "initializer.for.variable.cannot.be.a.constant.initializer",
                    localVariable.getName()));
        CommonRefactoringUtil.showErrorHint(
            project, editor, message, REFACTORING_NAME, getHelpID());
        highlightError(project, editor, errorElement);
        return null;
      }
    }

    final TypeSelectorManagerImpl typeSelectorManager =
        new TypeSelectorManagerImpl(project, type, containingMethod, expr, occurrences);
    if (editor != null
        && editor.getSettings().isVariableInplaceRenameEnabled()
        && (expr == null || expr.isPhysical())
        && activeIntroducer == null) {
      myInplaceIntroduceConstantPopup =
          new InplaceIntroduceConstantPopup(
              project,
              editor,
              parentClass,
              expr,
              localVariable,
              occurrences,
              typeSelectorManager,
              anchorElement,
              anchorElementIfAll,
              expr != null ? createOccurrenceManager(expr, parentClass) : null);
      if (myInplaceIntroduceConstantPopup.startInplaceIntroduceTemplate()) {
        return null;
      }
    }

    final IntroduceConstantDialog dialog =
        new IntroduceConstantDialog(
            project,
            parentClass,
            expr,
            localVariable,
            localVariable != null,
            occurrences,
            getParentClass(),
            typeSelectorManager,
            enteredName);
    dialog.setReplaceAllOccurrences(replaceAllOccurrences);
    if (!dialog.showAndGet()) {
      if (occurrences.length > 1) {
        WindowManager.getInstance()
            .getStatusBar(project)
            .setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting"));
      }
      return null;
    }
    return new Settings(
        dialog.getEnteredName(),
        expr,
        occurrences,
        dialog.isReplaceAllOccurrences(),
        true,
        true,
        InitializationPlace.IN_FIELD_DECLARATION,
        dialog.getFieldVisibility(),
        localVariable,
        dialog.getSelectedType(),
        dialog.isDeleteVariable(),
        dialog.getDestinationClass(),
        dialog.isAnnotateAsNonNls(),
        dialog.introduceEnumConstant());
  }
    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);
      }
    }