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

javac emits incorrect code for for-each on an intersection type.

    XMLWordPrintable

Details

    • b20
    • generic
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      A for each on an intersection type with an Iterable whose iterator() method returns a subclass (e.g. MyIterator iterable()) generates a call to a non existing method of the class java.lang.Iterable (iterator() returning MyIterator).

      The error at runtime is:
      Exception in thread "main" java.lang.NoSuchMethodError: 'Main$MyIterable$MyIterator java.lang.Iterable.iterator()'
      at Main.main(Main.java:7


      The full offending program is below:

      public class Main {

        public static void main(String... args) {
          for (Object s : (MyIterable & Serializable) null) {}
        )}

        static class MyIterable implements Iterable<Object>, Serializable {
          class MyIterator implements Iterator<Object> {
            public boolean hasNext() {
              return false;
            }

            public Object next() {
              return null;
            }

            public void remove() {}
          }

          public MyIterator iterator() {
            return new MyIterator();
          }
        }
      }


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      ---- Main.java
      public class Main {

        public static void main(String... args) {
          for (Object s : (MyIterable & Serializable) null) {}
        )}

        static class MyIterable implements Iterable<Object>, Serializable {
          class MyIterator implements Iterator<Object> {
            public boolean hasNext() {
              return false;
            }

            public Object next() {
              return null;
            }

            public void remove() {}
          }

          public MyIterator iterator() {
            return new MyIterator();
          }
        }
      }
      ----

      javac Main.java
      java Main

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      no error
      ACTUAL -
      Exception in thread "main" java.lang.NoSuchMethodError: 'Main$MyIterable$MyIterator java.lang.Iterable.iterator()'
      at Main.main(Main.java:7)

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

              sadayapalam Srikanth Adayapalam (Inactive)
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: