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

new "for" statement fails to cast to second upper bound

XMLWordPrintable

    • b46
    • x86
    • windows_2000



      Name: jl125535 Date: 02/25/2004


      FULL PRODUCT VERSION :
      java version "1.5.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-beta2-b39)
      Java HotSpot(TM) Client VM (build 1.5.0-beta2-b39, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows 2000 [Version 5.00.2195]

      A DESCRIPTION OF THE PROBLEM :
      The new for construct fails to find iterator() when defined as second upper bound. See comment in following:

          public <T extends Runnable & Iterable<String>> void bar( T t){
              for( String e : t) //This line does not run
                 out.println(e.toUpperCase());

              for(Iterator<String> i = t.iterator(); i.hasNext();)
                  out.println( i.next().toUpperCase());
              t.run();
          }

      The second, traditional iteration, works well.

      Swap the bounds, and it finds the iterator. Like:
      public <T extends Iterable<String> & Runnable> void bar( T t){....

      Seems like the the new for construct forgets about casting. The bytecode seem like:
      public void bar(Runnable t)
      {
        for(Iterator i = t.iterator();....
        ..
        for(Iterator i = ((Iterable)t).iterator(); ....
        ..
        t.run();
      .. and a Runnable does not have a iterator()


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      java.lang.NoSuchMethodError: java.lang.Runnable.iterator()

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.Iterator;
      import java.util.Arrays;
      import java.util.List;
      import static java.lang.System.out;


      public class Foo implements Runnable, Iterable<String>{
          
          public <T extends Runnable & Iterable<String>> void bar( T t){
              for( String e : t) //This line does not run
                  out.println(e.toUpperCase());

              for(Iterator<String> i = t.iterator(); i.hasNext();)
                  out.println( i.next().toUpperCase());
              t.run();
          }
          
          public void run(){
              out.println("Running");
          }
          
          public Iterator<String> iterator(){
              List<String> list = Arrays.asList(new String[]{"Adam", "Eve"});
              return list.iterator();
          }
          
          public static void main(String... args) {
              Foo foo = new Foo();
              foo.bar( foo);
          }
      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
       swap the bounds
      (Incident Review ID: 240088)
      ======================================================================

            gafter Neal Gafter (Inactive)
            jleesunw Jon Lee (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: