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

JShell does not work correctly when a class named Object or Throwable is defined

XMLWordPrintable

    • b21
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      In JShell, after a class named Object is defined, it will not create any type of scratch variables except this "Object" class, including java.lang.Object and primitive types. Any attempt to create a scratch variable or call a non-void function will either fail with an error message "incompatible types: <type> cannot be converted to Object" or discard the value, even if the type is java.lang.Object, unless the type is the post-defined Object class.

      After a class named Throwable is defined, every statement, whether it throws an exception or not, will fail with an error message "incompatible types: Throwable cannot be converted to java.lang.Throwable".

      This issue does not exist for javac. In javac, java.lang.Object and java.lang.Throwable can be distinguished from the user-defined Object and Throwable type by specifying the full name `java.lang.Object` or `java.lang.Throwable`.

      This issue does not exist for Iterable and AutoCloseable.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Run JShell and input the test cases shown below.

      ACTUAL -
      It behaves like this:

      #1
      $ jshell -v
      | Welcome to JShell -- Version 21.0.2
      | For an introduction type: /help intro

      jshell> class Object {}
      | created class Object

      jshell> 0
      | Error:
      | incompatible types: int cannot be converted to Object
      | 0
      | ^

      jshell> 0.0
      | Error:
      | incompatible types: double cannot be converted to Object
      | 0.0
      | ^-^

      jshell> false
      | Error:
      | incompatible types: boolean cannot be converted to Object
      | false
      | ^---^

      jshell> null
      $2 ==> null
      | created scratch variable $2 : Object

      jshell> (java.lang.Object)null
      | Error:
      | incompatible types: java.lang.Object cannot be converted to Object
      | (java.lang.Object)null
      | ^--------------------^

      jshell> "string"
      | Error:
      | incompatible types: java.lang.String cannot be converted to Object
      | "string"
      | ^------^

      jshell> Math.sqrt(2.0) // the return value is discarded

      jshell> new Object()
      $4 ==> Object@1e80bfe8
      | created scratch variable $4 : Object

      jshell> new java.lang.Object() // discarded

      jshell> System.out.println("Hello")
      Hello

      jshell> System.out.println($4.getClass() == Class.forName("java.lang.Object"))
      false

      jshell> System.out.println($4.getClass() == java.lang.Object.class)
      | Error:
      | incomparable types: java.lang.Class<capture#2 of ? extends Object> and java.lang.Class<java.lang.Object>
      | System.out.println($4.getClass() == java.lang.Object.class)
      | ^-------------------------------------^

      #2
      $ jshell -v
      | Welcome to JShell -- Version 21.0.2
      | For an introduction type: /help intro

      jshell> class Throwable {}
      | created class Throwable

      jshell> 0
      | Error:
      | incompatible types: Throwable cannot be converted to java.lang.Throwable
      | 0
      | ^

      jshell> 0.0
      | Error:
      | incompatible types: Throwable cannot be converted to java.lang.Throwable
      | 0.0
      | ^

      jshell> "string"
      | Error:
      | incompatible types: Throwable cannot be converted to java.lang.Throwable
      | "string"
      | ^

      jshell> throw new Throwable()
      | Error:
      | incompatible types: Throwable cannot be converted to java.lang.Throwable
      | throw new Throwable();
      | ^--------------------^

      jshell> throw new java.lang.Throwable()
      | Error:
      | incompatible types: Throwable cannot be converted to java.lang.Throwable
      | throw new java.lang.Throwable();
      | ^
      | Error:
      | unreported exception java.lang.Throwable; must be caught or declared to be thrown
      | throw new java.lang.Throwable();
      | ^------------------------------^

      jshell> Objects.requireNonNull(null)
      | Error:
      | incompatible types: Throwable cannot be converted to java.lang.Throwable
      | Objects.requireNonNull(null)
      | ^

      jshell> Objects.requireNonNull(new Object())
      | Error:
      | incompatible types: Throwable cannot be converted to java.lang.Throwable
      | Objects.requireNonNull(new Object())
      | ^

      jshell> System.out.println("Hello")
      | Error:
      | incompatible types: Throwable cannot be converted to java.lang.Throwable
      | System.out.println("Hello")
      | ^

      FREQUENCY : always


            jlahoda Jan Lahoda
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: