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

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

XMLWordPrintable

    • b18
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      The `java.lang.constant.ConstantDesc::resolveConstantDesc(…)` interface method should be overridden by the `java.lang.constant.ClassDesc`, `java.lang.constant.MethodHandleDesc`, and `java.lang.constant.MethodTypeDesc` to return the exact type as in the `java.lang.constant.ClassDescImpl`, `java.lang.constant.MethodTypeDescImpl`, `java.lang.constant.DirectMethodHandleDescImpl`, and `java.lang.constant.AsTypeMethodHandleDesc`.

      This would avoid the need to write explicit casts at use sites of said methods.

      Proposed change:
      ```
      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 6742b622a40..5673f42ddf8 100644
      --- a/src/java.base/share/classes/java/lang/constant/ClassDesc.java
      +++ b/src/java.base/share/classes/java/lang/constant/ClassDesc.java
      @@ -24,6 +24,7 @@
        */
       package java.lang.constant;
       
      +import java.lang.invoke.MethodHandles;
       import java.lang.invoke.TypeDescriptor;
       import java.util.stream.Stream;
       
      @@ -332,6 +333,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/MethodHandleDesc.java b/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java
      index 8c9ee15208e..d3b94094c3d 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..15758a99cee 100644
      --- a/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
      +++ b/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
      @@ -24,6 +24,7 @@
        */
       package java.lang.constant;
       
      +import java.lang.invoke.MethodHandles;
       import java.lang.invoke.MethodType;
       import java.lang.invoke.TypeDescriptor;
       import java.util.List;
      @@ -188,6 +189,9 @@ public sealed interface MethodTypeDesc
                                    returnType().displayName());
           }
       
      + @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
            Votes:
            1 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: