Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8316779 Null-Restricted Value Class Types (Preview)
  3. JDK-8317767

Standard library implementation of null-restricted value class types

    XMLWordPrintable

Details

    • Sub-task
    • Resolution: Unresolved
    • P4
    • None
    • None
    • core-libs
    • None

    Description

      This task summarizes the standard library changes introduced by the parent JEP, JDK-8317765.



      ### CheckedType

      java.lang.reflect.CheckedType is a new preview interface to represent dynamically-enforced types.

      sealed interface CheckedType permits Class, NullRestrictedCheckedType {
          Object cast(Object arg); // may throw CCE or NPE
          boolean canCast(Object arg); // true or false
          Class<?> boundingClass(); // class that contains all values of this CheckedType
      }

      (TBD whether this interface is generic, like Class.)

      java.lang.Class implements CheckedType and the 'canCast' and 'boundingClass' methods. (But the primitive types and 'void' are not considered valid CheckedTypes, despite this relationship.)

      A new preview class, java.lang.reflect.NullRestrictedCheckedType, implements CheckedType.

      final class NullRestrictedCheckedType implements CheckedType {
          public static NullRestrictedCheckedType of(Class<?> valueClass) { ... }
      }



      ### CheckedTypeBootstraps

      java.lang.runtime.CheckedTypeBootstraps is a new preview class that provides bootstraps to create CheckedTypes.

      class CheckedTypeBootstraps {
          public CallSite nullRestrictedCheckedType(Lookup lookup, String name, Class<?> resultType, Class<?> valueClass);
          public CallSite classType(Lookup lookup, String className, Class<?> resultType);
      }



      ### Arrays

      New preview methods allow the CheckedType of an array to be queried and used to create new arrays.

      public final class Array {
          public static CheckedType getComponentType(Object array) { ... }

          public static Object newInstance(CheckedType componentType, int length) { ... }
          // no support for multi-dimensional 'newInstance' creation
      }

      The Array.set method enforces the array's CheckedType and, in the case of failure, throws IllegalArgumentException.

      java.util.Arrays.copyOf and copyOfRange should use this API to accurately copy arrays. So should any other users of 'newInstance' (like AbstractCollection.toArray).



      ### Fields

      java.lang.reflect.Field adds a preview method to query the checked type of a field.

      public final class Field {
          public Class<?> getType() { ... }
          public Type getGenericType() { ... }
          public CheckedType getCheckedType() { ... }
          // By default returns null, or returns Object.class, or returns an Optional<CheckedType>
      }

      Field.set is responsible for dynamically checking _both_ the linkage type of the field and its checked type, if any. A failure in either case causes an IllegalArgumentException (compare the behavior of Array.set).

      A new preview class, java.lang.FieldStoreException, represents a failed write to a null-restricted field (e.g., due to separate compilation). It serves the same purpose as ArrayStoreException.



      ### Implicit constructors

      The zero-arg constructor of a class with the `ImplicitCreation` attribute can be assumed to be `implicit`. A preview Constructor.isImplicit method should surface this modifier.

      java.lang.Class may also provide `hasZeroInstance` and/or `getZeroInstance` methods.



      ### Signature attributes

      A new preview class, java.lang.reflect.NullRestrictedType, implements Type and surfaces null-restricted types appearing in `Signature` attributes.



      ### LooselyConsistentValue

      java.lang.LooselyConsistentValue is a marker interface for value classes that are willing to tolerate new corrupt instances created by non-atomic reads and writes.

      Attachments

        Issue Links

          Activity

            People

              dlsmith Dan Smith
              dlsmith Dan Smith
              Votes:
              1 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated: