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

JVM reflection support for records

    XMLWordPrintable

Details

    • behavioral
    • minimal
    • Programs with an existing ClassFile attribute named Record may notice a change in behavior. Such programs are expected to be rare.
    • Class file construct
    • SE

    Description

      Summary

      Introduce a Record attribute in the class file format, so that the JVM can provide reflection support for components of record types.

      Problem

      Record classes (JEP 359) are a preview feature of Java SE 14. The JVM will need to support reflecting over records and their components.

      Solution

      A Record attribute will be defined in the ClassFile format, containing information about the record class and its components. The JVM will use the attribute's contents to satisfy reflection requests when the Java API Class::getRecordComponents is used.

      The format of the Record attribute is checked by the JVM even if no reflection is performed. The checking is done at class load time, and is similar to format checking of attributes such as Exceptions and InnerClasses. For example, the JVM checks that the attribute's constant pool indexes are valid and that each component's attributes occur only once. This contrasts with other attributes, such as MethodParameters, that are not examined unless reflection is performed.

      Note that the Record attribute could potentially be useful in other ways, for example, in determining if a class is a record.

      The specifications for class redefinition and retransformation, in the JVM TI specification are enhanced to disallow any modification of the Record attribute.

      Specification

      Java Virtual Machine Spec changes for the Record attribute are attached.

      The JVM TI specification changes are as follows:

      @@ -7633,8 +7633,8 @@
               (unless explicitly prohibited).
               The retransformation must not add, remove or rename fields or methods, change the
               signatures of methods, change modifiers, or change inheritance.
      -        The retransformation must not change the <code>NestHost</code> or
      -        <code>NestMembers</code> attributes.
      +        The retransformation must not change the <code>NestHost</code>,
      +        <code>NestMembers</code>, or <code>Record</code> attributes.
               These restrictions may be lifted in future versions.
               See the error return description below for information on error codes
               returned if an unsupported retransformation is attempted.
      
      @@ -7786,11 +7786,11 @@
               (unless explicitly prohibited).
               The redefinition must not add, remove or rename fields or methods, change the
               signatures of methods, change modifiers, or change inheritance.
      -        The retransformation must not change the <code>NestHost</code> or
      -        <code>NestMembers</code> attributes.
      +        The redefinition must not change the <code>NestHost</code>,
      +        <code>NestMembers</code>, or <code>Record</code> attributes.
               These restrictions may be lifted in future versions.
      -   See the error return description below for information on error codes
      -   returned if an unsupported redefinition is attempted.
      +        See the error return description below for information on error codes
      +        returned if an unsupported redefinition is attempted.
               The class file bytes are not verified or installed until they have passed
               through the chain of <eventlink id="ClassFileLoadHook"/> events, thus the
               returned error code reflects the result of the transformations applied

      Attachments

        1. 8233595-20191108.zip
          22 kB
        2. 8233595-20191120.zip
          22 kB
        3. 8233595-20191120.zip
          22 kB
        4. 8233595-20191125.zip
          21 kB
        5. Records_Preview.html
          82 kB

        Issue Links

          Activity

            People

              dholmes David Holmes
              hseigel Harold Seigel (Inactive)
              Alex Buckley
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: