LocalQuickFix (PsiMethodCallExpression call) { PsiExpression qualifier = call.getMethodExpression().getQualifierExpression(); if (qualifier == null) return null; PsiClass optionalClass = PsiUtil.resolveClassInClassTypeOnly(qualifier.getType()); if (optionalClass == null || !CommonClassNames.JAVA_UTIL_OPTIONAL.equals(optionalClass.getQualifiedName())) return null; PsiType optionalElementType = OptionalUtil.getOptionalElementType(qualifier.getType()); if (optionalElementType == null) return null; PsiMethodCallExpression nextCall = ExpressionUtils.getCallForQualifier(call); if (nextCall != null) { if (optionalClass.equals(PsiUtil.resolveClassInClassTypeOnly(nextCall.getType()))) { if (!LambdaGenerationUtil.canBeUncheckedLambda(nextCall)) { // Probably qualifier accesses non-final vars or throws exception: we will replace qualifier, so this is not a problem PsiMethodCallExpression copy = (PsiMethodCallExpression)nextCall.copy(); PsiExpression copyQualifier = Objects.requireNonNull(copy.getMethodExpression().getQualifierExpression()); try { copyQualifier.replace(JavaPsiFacade.getElementFactory(call.getProject()) .createExpressionFromText("((" + optionalElementType.getCanonicalText() + ")null)", copyQualifier)); } catch (IncorrectOperationException e) { return null; } if (!LambdaGenerationUtil.canBeUncheckedLambda(copy)) { return null; } } return new UseFlatMapFix(); } } return null; }