IteratorDeclaration (PsiStatement statement) { PsiLocalVariable variable = getDeclaredVariable(statement); if (variable == null) return null; PsiExpression initializer = PsiUtil.skipParenthesizedExprDown(variable.getInitializer()); if (!(initializer instanceof PsiMethodCallExpression call)) return null; if (!call.getArgumentList().isEmpty()) return null; PsiReferenceExpression methodExpression = call.getMethodExpression(); boolean listIterator = "listIterator".equals(methodExpression.getReferenceName()); if (!"iterator".equals(methodExpression.getReferenceName()) && !listIterator) return null; PsiMethod method = call.resolveMethod(); if (method == null) return null; boolean isCollection; if (listIterator) { if (!InheritanceUtil.isInheritor(method.getContainingClass(), CommonClassNames.JAVA_UTIL_LIST)) return null; isCollection = true; } else { if (!InheritanceUtil.isInheritor(method.getContainingClass(), CommonClassNames.JAVA_LANG_ITERABLE)) return null; isCollection = InheritanceUtil.isInheritor(method.getContainingClass(), CommonClassNames.JAVA_UTIL_COLLECTION); } if (!InheritanceUtil.isInheritor(variable.getType(), CommonClassNames.JAVA_UTIL_ITERATOR)) return null; return new IteratorDeclaration(variable, methodExpression.getQualifierExpression(), isCollection); }