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

javac / JLS does not prevent illogical wildcard upper bound

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 8, 11, 14, 16
    • tools
    • x86_64
    • windows_10

      A DESCRIPTION OF THE PROBLEM :
      When a wildcard is used for a type variable with an upper bound, javac (and possibly the JLS as well) permits using any supertype of the bound of the type variable as upper bound of the wildcard. This allows writing highly confusing code (by accident), see the code example below.

      Side note: For lower bounds of wildcards this is not possible. They are restricted to the same or subtypes of the type variable bound.


      ---------- BEGIN SOURCE ----------
      class WildcardBoundsTest {
          static class BaseClass {}
          static class SubClass extends BaseClass {}
          static class GenericClass<T extends SubClass> {}
          
          // Bound of wildcard is supertype of type variable bound
          // This is illogical because only SubClass or subtypes are permitted
          // as type argument; BaseClass or any other subtype are not
          void doSomething(GenericClass<? extends BaseClass> param) {}
          
          // Side note: This is not permitted by javac
          // void doSomething2(GenericClass<? super BaseClass> param) {}
      }

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

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: