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

ObjectReference.invokeMethod() on default method throws ClassCastException

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P3 P3
    • 8u20
    • 8
    • core-svc
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0"
      Java(TM) SE Runtime Environment (build 1.8.0-b129)
      Java HotSpot(TM) 64-Bit Server VM (build 25.0-b69, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601]

      A DESCRIPTION OF THE PROBLEM :
      When ObjectReference.invokeMethod() is called with a default method that resolves to an interface implementation as the Method parameter, a ClassCastException is thrown. If the default method resolves to an overriding implementation in a concrete class, everything works as expected.

      From 8031195 I see that InterfaceType.invokeMethod() will not be added soon, but the call should still succeed on an appropriate ObjectReference.

      At the very least, IllegalArgumentException should be thrown instead, for compatibility with existing tools.

      Also, in the future, INVOKE_NONVIRTUAL should be supported for ObjectReference.invokeMethod() when called with a default method.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      compile source code
      run: jdb InvokeMethodBug
      do: stop at InvokeMethodBug:6
              run
              eval p.negate();

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Successful evaluation.
      ACTUAL -
      jdb reports a ClassCastException

      ERROR MESSAGES/STACK TRACES THAT OCCUR :
      Here is a stack dump from a similar procedure using the jGRASP debugger:
      -------------------------------------------------------------------------------------------------

      java.lang.ClassCastException: com.sun.tools.jdi.InterfaceTypeImpl cannot be cast to com.sun.tools.jdi.ClassTypeImpl
      at com.sun.tools.jdi.ObjectReferenceImpl.validateMethodInvocation(ObjectReferenceImpl.java:328)
      at com.sun.tools.jdi.ObjectReferenceImpl.invokeMethod(ObjectReferenceImpl.java:381)
      at grasp.debugger.JdiDebug.handleInvokeMethod(JdiDebug.java:10895)
      ...

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.util.function.Predicate;

      public class InvokeMethodBug {
         public static void main(String[] args) {
            Predicate p = (n)->true;
            p.negate();
         }
      }
      ---------- END SOURCE ----------

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: