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 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); } }