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

Delegating Iterator implementations that don't delegate forEachRemaining()

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P3
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
      None

      Backports

        Description

        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?

          Attachments

            Issue Links

              Activity

                People

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

                  Dates

                  Created:
                  Updated:
                  Resolved: