Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8205184

Delegating Iterator implementations that don't delegate forEachRemaining()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 11
    • None
    • core-libs
    • None

        Colleague Tobias Thierer writes:

        Classes that delegate/forward calls to a wrapped class normally want to override all methods, including ones that are nonabstract in the super class (eg. interface default methods); otherwise, a custom overridden implementation in the wrapped class would be replaced with the super / default behavior.

        However there are three Iterator implementations in java/util/Collections.java that delegate to another iterator but which use the super, rather than delegate, implementation of forEachRemaining(). This looks like an oversight from when default methods were introduced in OpenJDK 8.

        This affects at least the following methods / implementations:

        UnmodifiableEntrySetSpliterator.iterator()
        CheckedCollection.iterator()
        CheckedEntrySet.iterator()

        I'm attaching a test that demonstrates the oversight. The test uses iterator implementations that override forEachRemaining() to throw UnsupportedOperationException - just as an easily tested placeholder for more reasonable ways that an application may want to override forEachRemaining().

        Perhaps those Iterator implementations might want to override forEachRemaining() to call the delegate implementation of forEachRemaining(), rather than the super implementation?

              martin Martin Buchholz
              martin Martin Buchholz
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: