ExtractMethodProcessor (final PsiElement[] elements, final Project project, final PsiFile file, final Editor editor, final boolean showErrorMessages, final @Nullable Consumer<? super ExtractMethodProcessor> pass) { if (elements == null || elements.length == 0) { if (showErrorMessages) { String message = RefactoringBundle .getCannotRefactorMessage(RefactoringBundle.message("selected.block.should.represent.a.set.of.statements.or.an.expression")); CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.EXTRACT_METHOD); } return null; } for (PsiElement element : elements) { if (element instanceof PsiStatement statement && JavaHighlightUtil.isSuperOrThisCall(statement, true, true)) { if (showErrorMessages) { String message = RefactoringBundle .getCannotRefactorMessage(JavaRefactoringBundle.message("selected.block.contains.invocation.of.another.class.constructor")); CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.EXTRACT_METHOD); } return null; } if (element instanceof PsiStatement && PsiTreeUtil.getParentOfType(element, PsiClass.class) == null) { if (showErrorMessages) { String message = RefactoringBundle .getCannotRefactorMessage(JavaRefactoringBundle.message("selected.block.contains.statement.outside.of.class")); CommonRefactoringUtil.showErrorHint(project, editor, message, getRefactoringName(), HelpID.EXTRACT_METHOD); } return null; } } String initialMethodName = Optional.ofNullable(ExtractMethodSnapshot.SNAPSHOT_KEY.get(file)).map(s -> s.myMethodName).orElse(""); final ExtractMethodProcessor processor = new ExtractMethodProcessor(project, editor, elements, null, getRefactoringName(), initialMethodName, HelpID.EXTRACT_METHOD); processor.setShowErrorDialogs(showErrorMessages); try { if (!processor.prepare(pass)) return null; } catch (PrepareFailedException e) { if (showErrorMessages) { CommonRefactoringUtil.showErrorHint(project, editor, e.getMessage(), getRefactoringName(), HelpID.EXTRACT_METHOD); highlightPrepareError(e, file, editor, project); } return null; } return processor; }