Wrong specification of MethodHandles.constant(...)

XMLWordPrintable

    • Type: CSR
    • Resolution: Approved
    • Priority: P4
    • 27
    • Component/s: core-libs
    • None
    • behavioral
    • minimal
    • No compatibility risk, just a fix to the specification.
    • Java API
    • SE

      Summary

      Correct the wrong specification of MethodHandles.constant(Class,Object) regarding the equivalence with identity(...).bindTo(...).

      Problem

      The specification stated that the returned MethodHandle is equivalent to creating an identity handle and binding the value to the first parameter. However, this is incorrect for primitive types, which are not supported by MethodHandle::bindTo but are by MethodHandles::constant.

      Solution

      Replace the wrong example with a combination of identity with MethodHandles::insertArguments, which is actually equivalent.

      Specification

          diff --git a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
      --- a/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	(revision 659b53fe33eaa531bca1951a26f357b51902311e)
      +++ b/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	(revision 7889ae2e343500e3bcd3da3e90cc9ae3dc7bbff9)
      @@ -4826,7 +4826,9 @@
            * Before the method handle is returned, the passed-in value is converted to the requested type.
            * If the requested type is primitive, widening primitive conversions are attempted,
            * else reference conversions are attempted.
      -     * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)}.
      +     * <p>The returned method handle is equivalent to {@code identity(type).bindTo(value)},
      +     * for reference types.  For all types it is equivalent to
      +     * {@code insertArguments(identity(type), 0, value)}.
            * @param type the return type of the desired method handle
            * @param value the value to return
            * @return a method handle of the given return type and no arguments, which always returns the given value
      

            Assignee:
            Hannes Greule
            Reporter:
            Hannes Greule
            Chen Liang, Jorn Vernee
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: