PsiExpression (PsiStatement statement, PsiExpression[] args) { assert args.length == 1 || args.length == 2 : "The number of arguments must be either 1 or 2"; PsiElement parent = statement.getParent(); if (parent instanceof PsiForeachStatement foreachStatement) { PsiExpression iteratedValue = foreachStatement.getIteratedValue(); if (args.length == 2) { if (isGetKeyAndGetValue(args, foreachStatement.getIterationParameter())) { PsiMethodCallExpression entrySetCandidate = ObjectUtils.tryCast(iteratedValue, PsiMethodCallExpression.class); if (MAP_ENTRY_SET.test(entrySetCandidate)) { return entrySetCandidate.getMethodExpression().getQualifierExpression(); } } } else if (ExpressionUtils.isReferenceTo(args[0], foreachStatement.getIterationParameter())) { return iteratedValue; } } if (parent instanceof PsiLoopStatement) { IteratorDeclaration declaration = IteratorDeclaration.fromLoop((PsiLoopStatement)parent); if (declaration != null && declaration.isIteratorMethodCall(args[0], "next")) { return declaration.getIterable(); } } if (parent instanceof PsiForStatement) { return findIterableForIndexedLoop((PsiForStatement)parent, args[0]); } return null; }