-
Type:
CSR
-
Resolution: Unresolved
-
Priority:
P4
-
None
-
Component/s: core-libs
-
None
-
behavioral
-
high
-
All instances of value classes, including classes like Integer, Optional, and LocalDate, behave differently when using identity-sensitive APIs
-
Java API
-
SE
Summary
Provide library support for value classes and objects, and migrate certain standard classes to be value classes.
Problem
The value classes and objects features are motivated and described by the JEP.
A runtime environment that supports these features needs some accommodation from the standard libraries, including:
- Identity-based equality and hash code operations need an alternative behavior for value objects that depends on field values, not identity
- Synchronization does not make sense for value objects
- Reflection should support queries about an object's identity, and about new modifiers on classes and fields
- Serialization must not expose not-yet-initialized value objects when it creates them
- The garbage collection concept of "reachability" does not make sense for value objects—identical instances can always be reconstructed
- The class file API should support new modifiers and the new
LoadableDescriptorsattribute
In addition, to properly introduce users to the new features, the standard libraries should migrate some commonly-used classes to be treated as value classes (but only in preview mode).
Solution
The following APIs are modified to handle value objects:
-
System.identityHashCodeis specified to hash together the class and field values of a value object. -
The methods of
java.lang.Objectare indirectly affected by language changes involving==andsynchronized, and the revisedidentityHashCode. Their specifications would benefit, in general, from a direct explanation of their behavior for value objects. -
A preview exception class,
IdentityException, is available to indicate that an object is expected to have identity. -
java.util.Objectsadds preview methodshasIdentityandrequireIdentityto help distinguish identity objects from value objects. -
java.lang.Class,java.lang.reflect.Modifier, andjava.lang.reflect.AccessFlagreflect the newvalue/ACC_IDENTITYandACC_STRICTflags. -
ObjectInputStream.readObjectandwriteObjectgenerally throw anInvalidClassExceptionif the object being serialized/deserialized is an instance of a value class, whether concrete or abstract, with some exceptions for cases like value records and primitive wrapper classes. -
Instances of
java.lang.ref.Referencethrow an exception if created for a value object, andWeakHashMaprejects value objects as keys. -
java.lang.classfilesupports new class file modifiers and the newLoadableDescriptorsattribute.
The following classes get preview-only value class variations (no changes to the APIs other than the value modifier):
java.lang.Numberjava.lang.Record- All 8 primitive wrapper classes
- All 4
java.util.Optional*classes - 12
java.timeclasses:Duration,Instant,LocalTime,Year,YearMonth,MonthDay,Period,LocalDate,LocalDateTime,OffsetTime,OffsetDateTime,ZonedDateTime - 5
java.time.chronoclasses:ChronoLocalDateImpl,MinguoDate,HijrahDate,JapaneseDate, andThaiBuddhistDate
A more comprehensive outline of the solution can be found in the JDK-8317279 description.
Specification
Javadoc changes will be attached.
Changes to the serialization spec will also be attached.
- csr of
-
JDK-8317279 Standard library implementation of value classes and objects
-
- In Progress
-
- relates to
-
JDK-8339130 JVM implementation of value classes and objects
-
- Draft
-
-
JDK-8339181 Java language implementation of value classes and objects
-
- Draft
-