PsiElement (PsiMethodCallExpression streamOfCall) { PsiExpression streamOfArg = ArrayUtil.getFirstElement(streamOfCall.getArgumentList().getExpressions()); if (streamOfArg == null) return null; PsiMethodCallExpression nextCall = ExpressionUtils.getCallForQualifier(streamOfCall); PsiExpression nextArg = ArrayUtil.getFirstElement(nextCall.getArgumentList().getExpressions()); String replacement; if (myMode == ReplacementMode.OPTIONAL) { replacement = JAVA_UTIL_OPTIONAL + ".of(" + streamOfArg.getText() + ")"; } else { if (nextArg == null) return null; PsiMethod method = LambdaUtil.getFunctionalInterfaceMethod(nextArg.getType()); if (method == null) return null; String name = method.getName(); replacement = (myMode == ReplacementMode.NEGATED_FUNCTION ? "!" : "") + nextArg.getText() + "." + name + "(" + streamOfArg.getText() + ")"; } PsiElementFactory factory = JavaPsiFacade.getElementFactory(streamOfCall.getProject()); PsiExpression result = factory.createExpressionFromText(replacement, streamOfCall); return nextCall.replace(result); }