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

TypeVariable.getAnnotatedBounds() returns wrong result if the type parameter declares no bounds

XMLWordPrintable

      Spec for j.l.r.TypeVariable.getAnnotatedBounds() says:
      "Returns an array of length 0 if the type parameter declares no bounds."

      The following minimized test shows that if the type parameter declares no bounds, the methods returns array of length 1:
      -----------------------------------------------------------------------------------------
      public class Test {

          public static void main(String[] args) {
              AnnotatedTypeVariable atv = (AnnotatedTypeVariable) ((AnnotatedParameterizedType) Cls.class.getAnnotatedInterfaces()[0])
                      .getAnnotatedActualTypeArguments()[0];
              TypeVariable tv = (TypeVariable) atv.getType();

              // should return an empty array
              // Assertion: "Returns an array of length 0 if the type parameter declares no bounds."
              AnnotatedType[] aBounds = tv.getAnnotatedBounds();
              System.out.println(aBounds.length);
              System.out.println(aBounds[0].getType());
          }
      }

      interface Intf<T> {}

      class Cls<T> implements Intf<T> {}
      -----------------------------------------------------------------------------------------

      Output will be:
      -----------------------------------------------------------------------------------------
      1
      class java.lang.Object
      1
      class java.lang.Object
      -----------------------------------------------------------------------------------------

      Output should be:
      -----------------------------------------------------------------------------------------
      1
      class java.lang.Object
      0
      ...ArrayIndexOutOfBoundsException...
      -----------------------------------------------------------------------------------------

      Tested with 8u20

            abuckley Alex Buckley
            evotchen Elena Votchennikova (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: