void (final PsiFile file, final Project project, final PsiLocalVariable local, final Editor editor) { if (!CommonRefactoringUtil.checkReadOnlyStatus(project, file)) return; String localName = local.getName(); final PsiExpression initializer = local.getInitializer(); if (initializer == null) { String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.has.no.initializer", localName)); CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.REPLACE_TEMP_WITH_QUERY); return; } final PsiReference[] refs = ReferencesSearch.search(local, GlobalSearchScope.projectScope(project), false).toArray( PsiReference.EMPTY_ARRAY); if (refs.length == 0) { String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("variable.is.never.used", localName)); CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.REPLACE_TEMP_WITH_QUERY); return; } final HighlightManager highlightManager = HighlightManager.getInstance(project); ArrayList<PsiReference> array = new ArrayList<>(); for (PsiReference ref : refs) { PsiElement refElement = ref.getElement(); if (PsiUtil.isAccessedForWriting((PsiExpression)refElement)) { array.add(ref); } if (!array.isEmpty()) { PsiReference[] refsForWriting = array.toArray(PsiReference.EMPTY_ARRAY); highlightManager.addOccurrenceHighlights(editor, refsForWriting, EditorColors.SEARCH_RESULT_ATTRIBUTES, true, null); String message = RefactoringBundle.getCannotRefactorMessage(JavaRefactoringBundle.message("variable.is.accessed.for.writing", localName)); CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.REPLACE_TEMP_WITH_QUERY); WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting")); return; } } final ExtractMethodProcessor processor = new ExtractMethodProcessor( project, editor, new PsiElement[]{initializer}, local.getType(), getRefactoringName(), localName, HelpID.REPLACE_TEMP_WITH_QUERY ); try { if (!processor.prepare()) return; } catch (PrepareFailedException e) { CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), getRefactoringName(), HelpID.REPLACE_TEMP_WITH_QUERY); ExtractMethodHandler.highlightPrepareError(e, file, editor, project); return; } final PsiClass targetClass = processor.getTargetClass(); if (targetClass != null && targetClass.isInterface()) { String message = JavaRareRefactoringsBundle.message("cannot.replace.temp.with.query.in.interface"); CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.REPLACE_TEMP_WITH_QUERY); return; } if (processor.showDialog()) { CommandProcessor.getInstance().executeCommand( project, () -> { final Runnable action = () -> { try { processor.doRefactoring(); local.normalizeDeclaration(); PsiExpression initializer1 = local.getInitializer(); PsiExpression[] exprs = new PsiExpression[refs.length]; for (int idx = 0; idx < refs.length; idx++) { PsiElement ref = refs[idx].getElement(); exprs[idx] = (PsiExpression) ref.replace(initializer1); } PsiDeclarationStatement declaration = (PsiDeclarationStatement) local.getParent(); declaration.delete(); highlightManager.addOccurrenceHighlights(editor, exprs, EditorColors.SEARCH_RESULT_ATTRIBUTES, true, null); } catch (IncorrectOperationException e) { LOG.error(e); } }; PostprocessReformattingAspect.getInstance(project).postponeFormattingInside(() -> { ApplicationManager.getApplication().runWriteAction(action); DuplicatesImpl.processDuplicates(processor, project, editor); }); }, getRefactoringName(), null ); } WindowManager.getInstance().getStatusBar(project).setInfo(RefactoringBundle.message("press.escape.to.remove.the.highlighting")); }