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

Add overloads to MethodTypeDesc::of

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 21
    • core-libs
    • None
    • source
    • low
    • Hide
      Old explicit calls like MethodTypeDesc.of(returnType, null) will now fail to compile, but we don't anticipate users to call in such ways as it always throws NullPointerException. Old calls to MethodTypeDesc.of(returnType) now links to the new method after recompilation, but the behavior is the same.
      Show
      Old explicit calls like MethodTypeDesc.of(returnType, null) will now fail to compile, but we don't anticipate users to call in such ways as it always throws NullPointerException. Old calls to MethodTypeDesc.of(returnType) now links to the new method after recompilation, but the behavior is the same.
    • Java API
    • SE

      Summary

      Add two overloads to MethodTypeDesc.of for user convenience.

      Problem

      MethodTypeDesc constructor only accepts array as parameters, which is somewhat troublesome for users who want to pass collections or to create types with no parameter.

      Solution

      Add these two methods: of(ClassDesc) and of(ClassDesc, List), which allows implementation to perform optimizations later.

      Specification

      (I didn't upload a Javadoc preview for initial version 0, which took a Collection than a List) Version 1: https://cr.openjdk.org/~liach/8306698/1/java.base/java/lang/constant/MethodTypeDesc.html

      --- a/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
      +++ b/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
      @@ -55,6 +55,34 @@ public sealed interface MethodTypeDesc
               return MethodTypeDescImpl.ofDescriptor(descriptor);
           }
      
      +    /**
      +     * {@return a {@linkplain MethodTypeDesc} with the given return type and no
      +     * parameter types}
      +     *
      +     * @param returnDesc a {@linkplain ClassDesc} describing the return type
      +     * @throws NullPointerException if {@code returnDesc} is {@code null}
      +     * @since 21
      +     */
      +    static MethodTypeDesc of(ClassDesc returnDesc) {
      +        return new MethodTypeDescImpl(returnDesc, ConstantUtils.EMPTY_CLASSDESC);
      +    }
      +
      +    /**
      +     * {@return a {@linkplain MethodTypeDesc} given the return type and a list of
      +     * parameter types}
      +     *
      +     * @param returnDesc a {@linkplain ClassDesc} describing the return type
      +     * @param paramDescs a {@linkplain List} of {@linkplain ClassDesc}s
      +     * describing the parameter types
      +     * @throws NullPointerException if any argument or its contents are {@code null}
      +     * @throws IllegalArgumentException if any element of {@code paramDescs} is a
      +     * {@link ClassDesc} for {@code void}
      +     * @since 21
      +     */
      +    static MethodTypeDesc of(ClassDesc returnDesc, List<ClassDesc> paramDescs) {
      +        return of(returnDesc, paramDescs.toArray(ConstantUtils.EMPTY_CLASSDESC));
      +    }
      +
           /**
            * Returns a {@linkplain MethodTypeDesc} given the return type and parameter
            * types.

            liach Chen Liang
            liach Chen Liang
            Adam Sotona
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: