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

Type variable bounds referring to outer class not properly captured

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      The capture conversion of a wildcard for a type variable with a bound referring to an outer class is created incorrectly. It creates a capture type that still has an unresolved type variable in it.
      This can expose the type variable outside the scope where it is defined; or cause other type confusion, leading to heap pollution.


      ---------- BEGIN SOURCE ----------
      public class OuterInnerConfusion {
        static class Outer<P> {
          class MyList<I extends P> extends ArrayList<I> {
            MyList(I i) {add(i);}
          }
          P getIt(Outer<?>.MyList<?> myList) {
            // myList is a MyList<capture of ?>, where the capture has upper bound P...
            // but that's supposed to be a different P than the return type P here.
            return myList.get(0); // this should not be valid
          }
        }
        public static void main(String args[]) {
          // ClassCastException
          String s = new Outer<String>().getIt(new Outer<Integer>().new MyList<Integer>(1));
        }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            vromero Vicente Arturo Romero Zaldivar
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: