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

Incorrect Implementation of the Upperbound wildcard in Generics

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P4 P4
    • None
    • 8u162
    • core-libs
    • x86_64
    • linux_ubuntu

      ADDITIONAL SYSTEM INFORMATION :
      Ubuntu (16.04.4 LTS) 64-bit OS.

      A DESCRIPTION OF THE PROBLEM :
      Please go through the source code that I have shared. The source code has sufficient code as part of the comment, where I have explained the details. I might be incorrect in my understanding, nonetheless, it can be verified by appropriate professionals at Oracle.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please go through the source code that I have shared in the field - 'Source code for an executable test case' on this web-page.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A runtime exception when the code is executed, per my understanding.
      ACTUAL -
      The program outputs successfully without any runtime exception as follows:
      java.lang.RuntimeException
      java.lang.IllegalArgumentException
      java.lang.NullPointerException
      java.lang.NumberFormatException

      ---------- BEGIN SOURCE ----------
      package bugs.generics;

      import java.util.ArrayList;
      import java.util.List;

      /**
       *
       * @author sawanpatwari
       *
       */
      public class UpperBoundWildCardBug {

      public static void main(String[] args) {
      // TODO Auto-generated method stub
      List<RuntimeException> list = new ArrayList<>();
      list.add(new RuntimeException());
      list.add(new IllegalArgumentException());
      list.add(new NullPointerException());
      list.add(new NumberFormatException());

      printExtendsExceptions(list);

      /*
      The above function call with the parameter 'list' of type 'List<RuntimeException>'
      doesn't adhere to one of the rules of Java, which is, a class cannot extend itself.
      example:

      class Parent extend Parent{// this gives compile time error.

      }

      */


      /*
      The output of the method call doesn't throw any runtime exception instead
      it executes properly. This means that the statement -
      'RuntimeException class extends itself' is true.

      //technically, below sort of class is valid.
      class RuntimeException extends RuntimeException{

      }

      */
      }

      /*The wild card - '?' shouldn't accept an instance of the class -
      'RuntimeException' as part of the input parameter -
      'list' since the class 'RuntimeException' doesn't extend itself.

      Expected Output: A runtime exception being thrown when the program is executed.
      */
      public static void printExtendsExceptions(List<? extends RuntimeException> list) {
      for (RuntimeException x : list)
      System.out.println(x);
      }

      }

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

            psonal Pallavi Sonal (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: