void (@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement element) { PsiLambdaExpression lambdaExpression = PsiTreeUtil.getParentOfType(element, PsiLambdaExpression.class, false); if (lambdaExpression != null) { PsiCodeBlock body = CommonJavaRefactoringUtil.expandExpressionLambdaToCodeBlock(lambdaExpression); PsiClass targetClass = ClassUtils.getContainingClass(lambdaExpression); if (targetClass == null) return; PsiElement[] elements = body.getStatements(); HashSet<PsiField> usedFields = new HashSet<>(); boolean canBeStatic = CommonJavaRefactoringUtil.canBeStatic(targetClass, lambdaExpression, elements, usedFields) && usedFields.isEmpty(); PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(targetClass.getProject()); PsiType functionalInterfaceType = lambdaExpression.getFunctionalInterfaceType(); String parameters = LambdaUtil.createLambdaParameterListWithFormalTypes(functionalInterfaceType, lambdaExpression, false) + "{}"; String targetMethodName = getUniqueMethodName(targetClass, elementFactory, functionalInterfaceType, parameters); PsiType returnType = LambdaUtil.getFunctionalInterfaceReturnType(lambdaExpression); LOG.assertTrue(returnType != null); PsiMethod container = PsiTreeUtil.getParentOfType(lambdaExpression, PsiMethod.class); PsiTypeParameterList typeParamsList = container != null ? CommonJavaRefactoringUtil.createTypeParameterListWithUsedTypeParameters(container.getTypeParameterList(), elements) : null; PsiMethod emptyMethod = elementFactory.createMethodFromText("private " + (canBeStatic ? "static " : "") + (typeParamsList != null ? typeParamsList.getText() + " " : "") + returnType.getCanonicalText() + " " + targetMethodName + parameters, targetClass); PsiCodeBlock targetMethodBody = emptyMethod.getBody(); LOG.assertTrue(targetMethodBody != null); targetMethodBody.replace(body); PsiMethod method = (PsiMethod)CodeStyleManager.getInstance(project) .reformat(JavaCodeStyleManager.getInstance(project).shortenClassReferences(targetClass.add(emptyMethod))); PsiMethodReferenceExpression methodReference = (PsiMethodReferenceExpression)elementFactory .createExpressionFromText((canBeStatic ? targetClass.getName() : "this") + "::" + targetMethodName, lambdaExpression); CommentTracker tracker = new CommentTracker(); tracker.markUnchanged(lambdaExpression.getBody()); methodReference = (PsiMethodReferenceExpression)tracker.replace(lambdaExpression, methodReference); tracker.insertCommentsBefore(methodReference); startInplaceRename(editor, method, methodReference); } }