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

Javac accepts a method parameter with an invalid type argument

XMLWordPrintable

    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      The following code uses a method parameter with invalid bound, but is accepted by the compiler:

      class Parent {}
      class Child extends Parent {}
      class Container<T extends Child> {}
      public class Test {
          public static <T extends Parent> int method(Container<? super T> par) { // Why is this allowed?
                 return 0;
          }
          public static void main(String args[]) {
              if (method(new Container<>()) == 0) {
                  System.out.println("Zero");
              }
          }
      }

      We are using the type argument ? super T that has no upper bound and does not satisfy the type parameter declaration on Container<T extends Child> {}.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Just compile the give code with the latest Javac tool.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The code should be rejected by the compiler.
      ACTUAL -
      The code is allowed without any error.

      ---------- BEGIN SOURCE ----------
      class Parent {}
      class Child extends Parent {}
      class Container<T extends Child> {}
      public class Test {
          public static <T extends Parent> int method(Container<? super T> par) { // Why is this allowed?
                 return 0;
          }
          public static void main(String args[]) {
              if (method(new Container<>()) == 0) {
                  System.out.println("Zero");
              }
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


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

              Created:
              Updated:
              Resolved: