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

Iterative for loop fails to find generics information of cast classes

XMLWordPrintable

    • x86
    • linux

      FULL PRODUCT VERSION :
      java version "1.6.0_12"
      Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
      Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)


      ADDITIONAL OS VERSION INFORMATION :
      Linux attemptkind-lx 2.6.27-9-generic #1 SMP Thu Nov 20 21:57:00 UTC 2008 i686 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      If you declare a class which includes a generic type, then try to reference a method of that class which uses an unrelated generic type through an iterative for loop, the compiler declares "incompatible types."

      I know that sounds confusing, sorry. Look at the code under the "Source code" section -- it's much clearer in code than in words.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      $ javac isitabug/genericscastfor/Whoa.java
      isitabug/genericscastfor/Whoa.java:27: incompatible types
      found : java.lang.Object
      required: java.lang.String
          for (String one: other.getThingList())
                                             ^
      Note: isitabug/genericscastfor/Whoa.java uses unchecked or unsafe operations.
      Note: Recompile with -Xlint:unchecked for details.
      1 error


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      package isitabug.genericscastfor;

      import java.util.LinkedList;
      import java.util.List;

      public class Whoa<T>
      {
        public List<String> getThingList ()
        {
          return _stringList;
        }
        
        public void walkStringListOk (Whoa<T> other)
        {
          // this works fine because the generic T is specified
          for (String one: other.getThingList())
          {
            // ...
          }
        }
        
        public void walkStringList (Whoa other)
        {
          // getting generically outside an iterative for loop works fine
          List<String> myList = other.getThingList();
          // getting with an iterative for loop won't compile
          for (String one: other.getThingList())
          {
            // ...
          }
        }
        
        private List<String> _stringList = new LinkedList<String>();
      }

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

      CUSTOMER SUBMITTED WORKAROUND :
      There are several workarounds, and they're all pretty obvious:

      1) Cast the item you're looping over.
      2) Declare the generic type when referencing the base object (Whoa)
      3) Don't use the iterative for loop.
      ... etc.

            mcimadamore Maurizio Cimadamore
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: