-
CSR
-
Resolution: Unresolved
-
P4
-
None
-
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
LoadableDescriptors
attribute
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.identityHashCode
is specified to hash together the class and field values of a value object.The methods of
java.lang.Object
are 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.Objects
adds preview methodshasIdentity
andrequireIdentity
to help distinguish identity objects from value objects.java.lang.Class
,java.lang.reflect.Modifier
, andjava.lang.reflect.AccessFlag
reflect the newvalue
/ACC_IDENTITY
andACC_STRICT
flags.ObjectInputStream.readObject
andwriteObject
generally throw anInvalidClassException
if 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.Reference
throw an exception if created for a value object, andWeakHashMap
rejects value objects as keys.java.lang.classfile
supports new class file modifiers and the newLoadableDescriptors
attribute.
The following classes get preview-only value class variations (no changes to the APIs other than the value
modifier):
java.lang.Number
java.lang.Record
- All 8 primitive wrapper classes
- All 4
java.util.Optional*
classes - 12
java.time
classes:Duration
,Instant
,LocalTime
,Year
,YearMonth
,MonthDay
,Period
,LocalDate
,LocalDateTime
,OffsetTime
,OffsetDateTime
,ZonedDateTime
- 5
java.time.chrono
classes: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