void (PsiMethod methodToIntroduceParameter, PsiMethod methodToSearchFor, Editor editor, final Project project, final PsiType selectedType, final ExtractMethodProcessor processor, final PsiElement[] elements) { if (!elements[0].isValid()) { return; } final PsiElement commonParent = findCommonParent(elements); if (commonParent == null) { LOG.error("Should have common parent:" + Arrays.toString(elements)); return; } final RangeMarker marker = editor.getDocument().createRangeMarker(commonParent.getTextRange()); final PsiElement[] copyElements = processor.getElements(); final PsiElement containerCopy = findCommonParent(copyElements); if (containerCopy == null) { LOG.error("Should have common parent:" + Arrays.toString(copyElements)); return; } final PsiClassType.ClassResolveResult resolveResult = PsiUtil.resolveGenericsClassInType(selectedType); final PsiClass wrapperClass = resolveResult.getElement(); LOG.assertTrue(wrapperClass != null); final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final Ref<String> suffixText = new Ref<>(); final Ref<String> prefixText = new Ref<>(); final Ref<String> methodText = new Ref<>(); WriteCommandAction.runWriteCommandAction(project, () -> { final PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(wrapperClass); LOG.assertTrue(method != null); final String interfaceMethodName = method.getName(); processor.setMethodName(interfaceMethodName); processor.doExtract(); final PsiMethod extractedMethod = processor.getExtractedMethod(); final PsiParameter[] parameters = extractedMethod.getParameterList().getParameters(); final PsiParameter[] interfaceParameters = method.getParameterList().getParameters(); final PsiSubstitutor substitutor = resolveResult.getSubstitutor(); for (int i = 0; i < interfaceParameters.length; i++) { final PsiTypeElement typeAfterInterface = factory.createTypeElement(substitutor.substitute(interfaceParameters[i].getType())); final PsiTypeElement typeElement = parameters[i].getTypeElement(); if (typeElement != null) { typeElement.replace(typeAfterInterface); } } methodText.set(extractedMethod.getText()); final PsiMethodCallExpression methodCall = processor.getMethodCall(); prefixText.set(containerCopy.getText().substring(0, methodCall.getTextRange().getStartOffset() - containerCopy.getTextRange().getStartOffset())); suffixText.set("." + methodCall.getText() + containerCopy.getText().substring(methodCall.getTextRange().getEndOffset() - containerCopy.getTextRange().getStartOffset())); }); PsiExpression expression = factory .createExpressionFromText("new " + selectedType.getCanonicalText() + "() {" + methodText.get() + "}", elements[0]); expression = (PsiExpression)JavaCodeStyleManager.getInstance(project).shortenClassReferences(expression); expression.putUserData(ElementToWorkOn.PARENT, commonParent); expression.putUserData(ElementToWorkOn.PREFIX, prefixText.get()); expression.putUserData(ElementToWorkOn.SUFFIX, suffixText.get()); expression.putUserData(ElementToWorkOn.TEXT_RANGE, marker); expression.putUserData(ElementToWorkOn.EXPR_RANGE, elements.length == 1 ? elements[0].getTextRange() : null); new Introducer(project, expression, null, editor) .introduceParameter(methodToIntroduceParameter, methodToSearchFor); }