void () { try { InitializationPlace initializerPlace = mySettings.getInitializerPlace(); final PsiLocalVariable localVariable = mySettings.getLocalVariable(); final boolean deleteLocalVariable = mySettings.isDeleteLocalVariable(); @Nullable PsiExpression initializer = null; if (localVariable != null) { initializer = localVariable.getInitializer(); } else if (!(mySelectedExpr instanceof PsiReferenceExpression && ((PsiReferenceExpression)mySelectedExpr).resolve() == null)){ initializer = mySelectedExpr; } final SmartTypePointer type = SmartTypePointerManager.getInstance(myProject).createSmartTypePointer(myType); initializer = IntroduceVariableBase.simplifyVariableInitializer(initializer, myType, initializerPlace == InitializationPlace.IN_FIELD_DECLARATION); final PsiMethod enclosingConstructor = getEnclosingConstructor(myParentClass, myAnchorElement); PsiClass destClass = mySettings.getDestinationClass() == null ? myParentClass : mySettings.getDestinationClass(); if (!CommonRefactoringUtil.checkReadOnlyStatus(myProject, destClass.getContainingFile())) return; ChangeContextUtil.encodeContextInfo(destClass, true); myField = mySettings.isIntroduceEnumConstant() ? EnumConstantsUtil.createEnumConstant(destClass, myFieldName, initializer) : createField(myFieldName, type.getType(), initializer, initializerPlace == InitializationPlace.IN_FIELD_DECLARATION && initializer != null, myParentClass); setModifiers(myField, mySettings); PsiElement finalAnchorElement = null; if (destClass == myParentClass) { for (finalAnchorElement = myAnchorElement; finalAnchorElement != null && finalAnchorElement.getParent() != destClass; finalAnchorElement = finalAnchorElement.getParent()) { } } PsiMember anchorMember = finalAnchorElement instanceof PsiMember ? (PsiMember)finalAnchorElement : null; if (anchorMember instanceof PsiEnumConstant && destClass == anchorMember.getContainingClass() && initializer != null && PsiTreeUtil.isAncestor(((PsiEnumConstant)anchorMember).getArgumentList(), initializer, false)) { final String initialName = "Constants"; String constantsClassName = initialName; PsiClass innerClass = destClass.findInnerClassByName(constantsClassName, true); if (innerClass == null || !isConstantsClass(innerClass)) { int i = 1; while (destClass.findInnerClassByName(constantsClassName, true) != null) { constantsClassName = initialName + i++; } PsiClass psiClass = JavaPsiFacade.getElementFactory(myProject).createClass(constantsClassName); PsiUtil.setModifierProperty(psiClass, PsiModifier.PRIVATE, true); PsiUtil.setModifierProperty(psiClass, PsiModifier.STATIC, true); destClass = (PsiClass)destClass.add(psiClass); } else { destClass = innerClass; } anchorMember = null; } myField = appendField(initializer, initializerPlace, destClass, myParentClass, myField, anchorMember); if (!mySettings.isIntroduceEnumConstant()) { VisibilityUtil.fixVisibility(myOccurrences, myField, mySettings.getFieldVisibility()); } PsiStatement assignStatement = null; PsiElement anchorElementHere = null; if (initializerPlace == InitializationPlace.IN_CURRENT_METHOD && initializer != null || initializerPlace == InitializationPlace.IN_CONSTRUCTOR && enclosingConstructor != null && initializer != null) { if (myReplaceAll) { if (enclosingConstructor != null) { final PsiElement anchorInConstructor = CommonJavaRefactoringUtil.getAnchorElementForMultipleExpressions(mySettings.myOccurrences, enclosingConstructor); anchorElementHere = anchorInConstructor != null ? anchorInConstructor : myAnchorStatementIfAll; } else { anchorElementHere = myAnchorStatementIfAll; } } else { anchorElementHere = myAnchorElementIfOne; } assignStatement = createAssignment(myField, initializer, anchorElementHere, myParentClass); if (anchorElementHere != null && !CommonJavaRefactoringUtil.isLoopOrIf(anchorElementHere.getParent())) { anchorElementHere.getParent().addBefore(assignStatement, getNormalizedAnchor(anchorElementHere)); } } if (initializerPlace == InitializationPlace.IN_CONSTRUCTOR && initializer != null) { addInitializationToConstructors(initializer, myField, enclosingConstructor, myParentClass); } if (initializerPlace == InitializationPlace.IN_SETUP_METHOD && initializer != null) { addInitializationToSetUp(initializer, myField, myOccurrences, myReplaceAll, myParentClass); } if (mySelectedExpr.getParent() instanceof PsiParenthesizedExpression) { mySelectedExpr = (PsiExpression)mySelectedExpr.getParent(); } if (myOutOfCodeBlockExtraction != null) { final int endOffset = mySelectedExpr.getUserData(ElementToWorkOn.TEXT_RANGE).getEndOffset(); PsiElement endElement = myElement.getContainingFile().findElementAt(endOffset); while (true) { final PsiElement parent = endElement.getParent(); if (parent instanceof PsiClass) break; endElement = parent; } PsiElement last = PsiTreeUtil.skipWhitespacesBackward(endElement); if (last.getTextRange().getStartOffset() < myElement.getTextRange().getStartOffset()) { last = myElement; } myElement.getParent().deleteChildRange(myElement, last); } else if (myDeleteSelf) { myElement.getParent().delete(); } if (myReplaceAll) { List<PsiElement> array = new ArrayList<>(); for (PsiExpression occurrence : myOccurrences) { occurrence = CommonJavaRefactoringUtil.outermostParenthesizedExpression(occurrence); if (myDeleteSelf && occurrence.equals(mySelectedExpr)) continue; final PsiElement replaced = RefactoringUtil.replaceOccurenceWithFieldRef(occurrence, myField, destClass); if (replaced != null) { array.add(replaced); } } if (myEditor != null) { if (!ApplicationManager.getApplication().isUnitTestMode()) { PsiElement[] exprsToHighlight = PsiUtilCore.toPsiElementArray(array); HighlightManager highlightManager = HighlightManager.getInstance(myProject); highlightManager.addOccurrenceHighlights(myEditor, exprsToHighlight, EditorColors.SEARCH_RESULT_ATTRIBUTES, true, null); WindowManager .getInstance().getStatusBar(myProject).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting")); } } } else { if (!myDeleteSelf) { mySelectedExpr = CommonJavaRefactoringUtil.outermostParenthesizedExpression(mySelectedExpr); RefactoringUtil.replaceOccurenceWithFieldRef(mySelectedExpr, myField, destClass); } } if (anchorElementHere != null && CommonJavaRefactoringUtil.isLoopOrIf(anchorElementHere.getParent())) { CommonJavaRefactoringUtil.putStatementInLoopBody(assignStatement, anchorElementHere.getParent(), anchorElementHere); } if (localVariable != null) { if (deleteLocalVariable) { localVariable.normalizeDeclaration(); localVariable.getParent().delete(); } } ChangeContextUtil.decodeContextInfo(destClass, destClass, null); } catch (IncorrectOperationException e) { LOG.error(e); } }