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

Missing unchecked cast warning in polymorphic method call

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 24
    • 24
    • tools
    • None
    • b24

      No unchecked cast is issued here:

      import java.lang.invoke.VarHandle;
      class VarHandleCast<V> {
           VarHandle vh;
           V method(Object obj) {
               return (V)vh.getAndSet(this, obj);
           }
      }

      The JLS says (15.12.3):

      > The compile-time result is determined as follows:
      >
      > If the signature polymorphic method is either void or has a return type other than Object, the compile-time result is the result of the invocation type of the compile-time declaration (§15.12.2.6).
      >
      > Otherwise, if the method invocation expression is an expression statement, the compile-time result is void.
      >
      > Otherwise, if the method invocation expression is the operand of a cast expression (§15.16), the compile-time result is the erasure of the type of the cast expression (§4.6).
      >
      > Otherwise, the compile-time result is the signature polymorphic method's return type, Object.

      So, in the above, the result type is Object. And a cast from Object to V is unchecked. A warning should be issued here.

            vromero Vicente Arturo Romero Zaldivar
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: