boolean (final Editor editor, final Project project, final PsiExpression expr, PsiLocalVariable localVar, boolean invokedOnDeclaration) { LOG.assertTrue(!PsiDocumentManager.getInstance(project).hasUncommitedDocuments()); PsiMethod method; if (expr != null) { method = Util.getContainingMethod(expr); } else { method = Util.getContainingMethod(localVar); } if (LOG.isDebugEnabled()) { LOG.debug("expression:" + expr); } if (expr == null && localVar == null) { String message = RefactoringBundle.getCannotRefactorMessage(JavaRefactoringBundle.message("selected.block.should.represent.an.expression")); showErrorMessage(project, message, editor); return false; } if (localVar != null) { final PsiElement parent = localVar.getParent(); if (!(parent instanceof PsiDeclarationStatement)) { String message = RefactoringBundle.getCannotRefactorMessage(JavaRefactoringBundle.message("error.wrong.caret.position.local.or.expression.name")); showErrorMessage(project, message, editor); return false; } } if (method == null) { String message = RefactoringBundle.getCannotRefactorMessage(RefactoringBundle.message("is.not.supported.in.the.current.context", getRefactoringName())); showErrorMessage(project, message, editor); return false; } final PsiType typeByExpression = invokedOnDeclaration ? null : CommonJavaRefactoringUtil.getTypeByExpressionWithExpectedType(expr); if (!invokedOnDeclaration && (typeByExpression == null || LambdaUtil.notInferredType(typeByExpression))) { String message = RefactoringBundle.getCannotRefactorMessage(JavaRefactoringBundle.message("type.of.the.selected.expression.cannot.be.determined")); showErrorMessage(project, message, editor); return false; } if (!invokedOnDeclaration && PsiTypes.voidType().equals(typeByExpression)) { String message = RefactoringBundle.getCannotRefactorMessage(JavaRefactoringBundle.message("selected.expression.has.void.type")); showErrorMessage(project, message, editor); return false; } final List<PsiMethod> validEnclosingMethods = CommonJavaRefactoringUtil.getEnclosingMethods(method); if (validEnclosingMethods.isEmpty()) { return false; } if (!CommonRefactoringUtil.checkReadOnlyStatus(project, method)) return false; final Introducer introducer = new Introducer(project, expr, localVar, editor); final AbstractInplaceIntroducer inplaceIntroducer = AbstractInplaceIntroducer.getActiveIntroducer(editor); if (inplaceIntroducer instanceof InplaceIntroduceParameterPopup introduceParameterPopup) { introducer.introduceParameter(introduceParameterPopup.getMethodToIntroduceParameter(), introduceParameterPopup.getMethodToSearchFor()); return true; } chooseMethodToIntroduceParameter(editor, validEnclosingMethods, (methodToSearchIn, methodToSearchFor) -> introducer.introduceParameter(methodToSearchIn, methodToSearchFor)); return true; }