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

Add <init> and <clinit> method constants to ConstantDescs

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 21
    • core-libs
    • None
    • minimal
    • Addition of new static final fields in `ConstantDescs` class, so the risk is minimal
    • Java API
    • SE

      Summary

      Add constants in java.lang.constant.ConstantDescs to describe the names of instance and class initialization methods and a method type descriptor of "()V".

      Problem

      In Classfile generation and JDK itself, the names of these special methods are frequently used; having to define them in each use site is prone to typos that javac cannot catch easily.

      Solution

      Add INIT_NAME, CLASS_INIT_NAME, MTD_void to java.lang.constant.ConstantDescs class, representing the special name for instance initialization, class initialization methods and their common descriptor.

      Specification

      @@ -283,6 +283,33 @@ public final class ConstantDescs {
                   = DynamicConstantDesc.ofNamed(BSM_GET_STATIC_FINAL,
                                                 "FALSE", CD_Boolean, CD_Boolean);
      
      +    /**
      +     * The special name of instance initialization methods, {@value}. An instance
      +     * initialization method has this special name and is {@code void}.
      +     *
      +     * @jvms 2.9.1 Instance Initialization Methods
      +     * @since 21
      +     */
      +    public static final String INIT_NAME = "<init>";
      +
      +    /**
      +     * The special name of class initialization methods, {@value}. A class
      +     * initialization method has this special name, {@link java.lang.reflect.AccessFlag#STATIC
      +     * ACC_STATIC} flag set, is {@link #MTD_void void} and takes no arguments.
      +     *
      +     * @jvms 2.9.2 Class Initialization Methods
      +     * @since 21
      +     */
      +    public static final String CLASS_INIT_NAME = "<clinit>";
      +
      +    /**
      +     * Nominal descriptor representing the method descriptor {@code ()V},
      +     * taking no argument and returning {@code void}.
      +     *
      +     * @since 21
      +     */
      +    public static final MethodTypeDesc MTD_void = MethodTypeDesc.of(CD_void);
      +
           static final DirectMethodHandleDesc MHD_METHODHANDLE_ASTYPE
                   = MethodHandleDesc.ofMethod(Kind.VIRTUAL, CD_MethodHandle, "asType",
                                               MethodTypeDesc.of(CD_MethodHandle, CD_MethodType));

            liach Chen Liang
            liach Chen Liang
            Mandy Chung (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: