PsiExpression (PsiFunctionalExpression function) { PsiElement parent = PsiUtil.skipParenthesizedExprUp(function.getParent()); if (parent instanceof PsiTypeCastExpression) parent = PsiUtil.skipParenthesizedExprUp(parent.getParent()); if (!(parent instanceof PsiExpressionList)) return null; PsiExpression[] args = ((PsiExpressionList)parent).getExpressions(); if (args.length != 1) return null; parent = parent.getParent(); if (!(parent instanceof PsiMethodCallExpression parentCall)) return null; PsiExpression parentQualifier = PsiUtil.skipParenthesizedExprDown(parentCall.getMethodExpression().getQualifierExpression()); if (MethodCallUtils.isCallToMethod(parentCall, CommonClassNames.JAVA_LANG_ITERABLE, null, "forEach", new PsiType[]{null})) { PsiMethodCallExpression entrySetCandidate = ObjectUtils.tryCast(parentQualifier, PsiMethodCallExpression.class); return MAP_ENTRY_SET.test(entrySetCandidate) ? ExpressionUtils.getEffectiveQualifier(entrySetCandidate.getMethodExpression()) : ExpressionUtils.getEffectiveQualifier(parentCall.getMethodExpression()); } if (MethodCallUtils.isCallToMethod(parentCall, CommonClassNames.JAVA_UTIL_MAP, null, "forEach", new PsiType[]{null})) { return ExpressionUtils.getEffectiveQualifier(parentCall.getMethodExpression()); } if (MethodCallUtils.isCallToMethod(parentCall, CommonClassNames.JAVA_UTIL_STREAM_STREAM, null, FOR_EACH_METHOD, new PsiType[]{null}) && parentQualifier instanceof PsiMethodCallExpression grandParentCall) { if (MethodCallUtils.isCallToMethod(grandParentCall, CommonClassNames.JAVA_UTIL_COLLECTION, null, "stream", PsiType.EMPTY_ARRAY)) { return ExpressionUtils.getEffectiveQualifier(grandParentCall.getMethodExpression()); } PsiExpression[] grandParentArgs = grandParentCall.getArgumentList().getExpressions(); if (grandParentArgs.length == 1) { PsiExpression maybeArray = grandParentArgs[0]; if (MethodCallUtils.isCallToStaticMethod(grandParentCall, CommonClassNames.JAVA_UTIL_ARRAYS, "stream", 1)) { return maybeArray; } if (MethodCallUtils.isCallToStaticMethod(grandParentCall, CommonClassNames.JAVA_UTIL_STREAM_STREAM, "of", 1)) { PsiType type = maybeArray.getType(); if (type instanceof PsiArrayType) { return maybeArray; } } } } return null; }