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

Use covariant overrides for the resolveConstantDesc(Lookup) method in sub‑interfaces of java.lang.constant.ConstantDesc

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 21
    • core-libs
    • None
    • source, binary
    • minimal
    • Addition of methods in sealed interfaces has no risk to users. Compiler-generated bridge methods to original signatures should provide all necessary adaptations.
    • Java API
    • SE

      Summary

      Add covariant overrides to resolveConstantDesc(Lookup) in ClassDesc, MethodTypeDesc, and MethodHandleDesc interfaces.

      Problem

      Calls to resolveConstantDesc(Lookup) on the 3 interfaces require casting results, while the calls to other implementations, such as String or DynamicConstantDesc, do not.

      Solution

      Add covariant overrides of resolveConstantDesc(Lookup) in these three classes. Move the API notes for MethodTypeDesc.resolveConstantDesc(Lookup) to MethodTypeDesc itself, and add a general API note in ConstantDesc about the potential resolution errors.

      Specification

      diff --git a/src/java.base/share/classes/java/lang/constant/ClassDesc.java b/src/java.base/share/classes/java/lang/constant/ClassDesc.java
      index 3f0604b336a..06601c475b3 100644
      --- a/src/java.base/share/classes/java/lang/constant/ClassDesc.java
      +++ b/src/java.base/share/classes/java/lang/constant/ClassDesc.java
      @@ -362,6 +363,9 @@ public sealed interface ClassDesc
            */
           String descriptorString();
      
      +    @Override
      +    Class<?> resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException;
      +
           /**
            * Compare the specified object with this descriptor for equality.  Returns
            * {@code true} if and only if the specified object is also a
      diff --git a/src/java.base/share/classes/java/lang/constant/ConstantDesc.java b/src/java.base/share/classes/java/lang/constant/ConstantDesc.java
      index 2be66b7002c..ee2bc7fce96 100644
      --- a/src/java.base/share/classes/java/lang/constant/ConstantDesc.java
      +++ b/src/java.base/share/classes/java/lang/constant/ConstantDesc.java
      @@ -99,9 +99,9 @@ public sealed interface ConstantDesc
            * could not be reflectively resolved in the course of resolution
            * @throws LinkageError if a linkage error occurs
            *
      -     * @apiNote {@linkplain MethodTypeDesc} can represent method type descriptors
      -     * that are not representable by {@linkplain MethodType}, such as methods with
      -     * more than 255 parameter slots, so attempts to resolve these may result in errors.
      +     * @apiNote Some constant descriptors, such as {@linkplain MethodTypeDesc}, can represent
      +     * a value that is not representable by run-time entities. Attempts to resolve these may
      +     * result in errors.
            *
            * @jvms 5.4.3 Resolution
            * @jvms 5.4.4 Access Control
      
      diff --git a/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java b/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java
      index 8c9ee15208e..9e8633d26e0 100644
      --- a/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java
      +++ b/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java
      @@ -204,6 +204,9 @@ public sealed interface MethodHandleDesc
            */
           MethodTypeDesc invocationType();
      
      +    @Override
      +    MethodHandle resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException;
      +
           /**
            * Compares the specified object with this descriptor for equality.  Returns
            * {@code true} if and only if the specified object is also a
      diff --git a/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java b/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
      index 4750231ce37..175d91bf3f6 100644
      --- a/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
      +++ b/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
      @@ -188,6 +189,16 @@ public sealed interface MethodTypeDesc
                                    returnType().displayName());
           }
      
      +    /**
      +     * {@inheritDoc}
      +     *
      +     * @apiNote {@linkplain MethodTypeDesc} can represent method type descriptors
      +     * that are not representable by {@linkplain MethodType}, such as methods with
      +     * more than 255 parameter slots, so attempts to resolve these may result in errors.
      +     */
      +    @Override
      +    MethodType resolveConstantDesc(MethodHandles.Lookup lookup) throws ReflectiveOperationException;
      +
           /**
            * Compares the specified object with this descriptor for equality.  Returns
            * {@code true} if and only if the specified object is also a

            liach Chen Liang
            webbuggrp Webbug Group
            Mandy Chung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: