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

Confusing generic requirements.

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 5.0
    • tools
    • x86
    • windows_xp

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

      ADDITIONAL OS VERSION INFORMATION :
      Windows XP SP 2

      A DESCRIPTION OF THE PROBLEM :
      Perhaps it is documented somewhere by I cannot find it.

      Is it possible to get the following to compile without warning

        From the Collections.sort() method
          public static <T> void sort(List<T> list, Comparator<? super T> c) {
              Object[] a = list.toArray();
      // original
              Arrays.sort(a, (Comparator) c);
      // also compile with a warning.
              Arrays.sort(a, (Comparator<? super Object>) c);



      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      see previous.

      Arrays.sort(a, (Comparator<? super Object>) c);
      produces the warning
      warning (1,1) [unchecked] unchecked cast
      found: java.util.Comparator<capture of ? super T>
      required: java.util.Comparator<? super java.lang.Object>


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      If you take the expected type, character for character, then the code should compile without error.


      ACTUAL -
      I have found "capture of" means you have to use your imagination as what should fix the problem, and can mean different things in different situations.


      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      warning (1,1) [unchecked] unchecked cast
      found: java.util.Comparator<capture of ? super T>
      required: java.util.Comparator<? super java.lang.Object>


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      Another example
      Say you write
          public static<K,V> void addResverse(Map<K,V> map1, Map<? extends K, ? extends V> map2) {
              Iterator<Map.Entry<? extends K, ? extends V>> iterator = map2.entrySet().iterator();
          }

      The expected type in the warning is
      Iterator<Map.Entry<capture of ? extends K, capture of ? extends V>>

      however the type actaully expected is
      Iterator<? extends Map.Entry<? extends K, ? extends V>>
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Ignore the warning if you cannot make it go away. My IDE has the option of selectively turning off a warning on a line. Perhaps this is why they do this.

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

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: