JVM implementation of strict field initialization

XMLWordPrintable

    • Type: CSR
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Component/s: hotspot
    • None
    • minimal
    • No change in the treatment of existing class files
    • Class file construct
    • SE

      Summary

      Enhance the JVM with strictly-initialized fields.

      Problem

      The JEP provides a high-level discussion motivating the feature.

      The JVM wants to guarantee that certain fields have been assigned an initial value before they are read and, if final, will not be assigned again. This involves new verification checks for instance fields and run-time checks for static fields. But there needs to be an opt-in signal from class file authors that they accept these new semantics.

      Solution

      New class file features include:

      • An ACC_STRICT_INIT (0x0800) flag for fields, indicating that the field accepts the strict initialization validation rules

      • An early_larval_frame entry in the StackMapTable, allowing class authors to express type states for <init> methods in which some ACC_STRICT_INIT instance fields have been set, but a delegating constructor invocation (super()/this()) has not yet occurred

      These features are supported by javap, and by the java.lang.reflect and java.lang.classfile APIs.

      Specification

      JVMS changes are included in the attached spec change document bundle. See the file specs/strict-fields-jvms.html.

      Inline API changes below.

      java.lang.reflect.AcessFlag

          /**
           * The access flag {@code ACC_STRICT_INIT}, with a mask value of
           * <code>{@value "0x%04x" ClassFile#ACC_STRICT_INIT}</code>.
           *
           * @jvms strict-fields-4.5 Field access and property flags
           * @since Valhalla
           */
          @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective=true)
          STRICT_INIT
      

      java.lang.reflect.Field

          /**
           * Returns {@code true} if this field is a strictly
           * initialized field; returns {@code false} otherwise.
           *
           * @return true if and only if this field is a strictly
           * initialized field as defined by the Java Virtual Machine Specification
           * @jvms strict-fields-4.5 Field access and property flags
           * @since Valhalla
           */
          @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
          public boolean isStrictInit()
      

      java.lang.classfile.ClassFile

          /**
           * The bit mask of {@link AccessFlag#STRICT_INIT} access and property modifier.
           *
           * @since Valhalla
           */
          @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
          int ACC_STRICT_INIT = 0x0800;
      

      java.lang.classfile.attribute.StackMapFrameInfo

          /**
           * {@return a new stack map frame}
           * @param target the location of the frame
           * @param locals the complete list of frame locals
           * @param stack the complete frame stack
           * @param unsetFields the complete list of unset fields
           * @throws IllegalArgumentException if the number of elements in {@code locals},
           *         {@code stack}, or {@code unsetFields} exceeds the limit of
           *         {@link java.lang.classfile##u2 u2}; or if unset fields has
           *         elements, but no {@link SimpleVerificationTypeInfo#UNINITIALIZED_THIS
           *         uninitializedThis} is present in {@code locals}
           * @since Valhalla
           */
          @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
          public static StackMapFrameInfo of(Label target,
                                             List<VerificationTypeInfo> locals,
                                             List<VerificationTypeInfo> stack,
                                             List<NameAndTypeEntry> unsetFields)
      
          /**
           * {@return the expanded unset fields}
           *
           * @jvms strict-fields-4.7.4 The {@code StackMapTable} Attribute
           * @since Valhalla
           */
          @PreviewFeature(feature = PreviewFeature.Feature.STRICT_FIELDS, reflective = true)
          List<NameAndTypeEntry> unsetFields();
      

            Assignee:
            Dan Smith
            Reporter:
            Dan Smith
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: