PsiElement (PsiMethodCallExpression mapToObjCall) { Project project = mapToObjCall.getProject(); PsiExpression mapper = ArrayUtil.getFirstElement(mapToObjCall.getArgumentList().getExpressions()); LimitedContainer limitedContainer = extractContainer(getQualifierMethodCall(mapToObjCall), mapper); if (limitedContainer == null) return null; IndexedContainer container = limitedContainer.myContainer; PsiExpression limit = limitedContainer.myLimit; PsiExpression containerQualifier = container.getQualifier(); PsiType type = containerQualifier.getType(); PsiType elementType = container.getElementType(); PsiType outElementType = StreamApiUtil.getStreamElementType(mapToObjCall.getType()); if (type == null || elementType == null) return null; String replacement; if (type instanceof PsiArrayType) { replacement = JAVA_UTIL_ARRAYS + ".stream(" + containerQualifier.getText() + ")"; } else { replacement = ParenthesesUtils.getText(containerQualifier, ParenthesesUtils.POSTFIX_PRECEDENCE) + ".stream()"; } PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); CommentTracker ct = new CommentTracker(); if (limit != null) { replacement += ".limit(" + ct.text(limit) + ")"; } if (mapper instanceof PsiMethodReferenceExpression) { mapper = LambdaRefactoringUtil.convertMethodReferenceToLambda((PsiMethodReferenceExpression)mapper, false, true); } if (!(mapper instanceof PsiLambdaExpression lambda)) return null; PsiParameter indexParameter = ArrayUtil.getFirstElement(lambda.getParameterList().getParameters()); PsiElement body = lambda.getBody(); if (body == null || indexParameter == null) return null; VariableNameGenerator generator = new VariableNameGenerator(mapToObjCall, VariableKind.PARAMETER); if (containerQualifier instanceof PsiReferenceExpression) { generator.byCollectionName(((PsiReferenceExpression)containerQualifier).getReferenceName()); } String name = generator.byType(elementType).byName("item", "element").generate(true); List<PsiReference> refs = new ArrayList<>(ReferencesSearch.search(indexParameter, new LocalSearchScope(body)).findAll()); refs.sort(Comparator.comparingInt(ref -> ref.getElement().getTextOffset())); for (PsiReference ref : refs) { PsiExpression getExpression = container.extractGetExpressionFromIndex(tryCast(ref, PsiExpression.class)); if (getExpression != null) { PsiElement result = ct.replace(getExpression, factory.createIdentifier(name)); if (getExpression == body) { body = result; } } } PsiLambdaExpression newLambda = (PsiLambdaExpression)factory .createExpressionFromText("(" + elementType.getCanonicalText() + " " + name + ")->" + ct.text(body), mapToObjCall); PsiParameter newParameter = ArrayUtil.getFirstElement(newLambda.getParameterList().getParameters()); replacement += StreamRefactoringUtil.generateMapOperation(newParameter, outElementType, newLambda.getBody()); PsiElement result = ct.replaceAndRestoreComments(mapToObjCall, replacement); LambdaCanBeMethodReferenceInspection.replaceAllLambdasWithMethodReferences(result); result = JavaCodeStyleManager.getInstance(project).shortenClassReferences(result); return CodeStyleManager.getInstance(project).reformat(result); }