Summary
Add record classes to the Java language. Record classes, a.k.a. records, allow the definition of shallowly immutable, transparent carriers for a fixed set of values, the record components.
Problem
Modeling data classes with the Java language is possible but users have to generate a lot of boilerplate to model even the simplest of them. There is also a lot of repetition implying a lot of places for bugs to hide. Users are forced to provide implementations for methods like: toString, equals and hashCode even when those implementations could be directly derived from the state of transparent data carriers and easy to generate automatically by a compiler.
Solution
The Java language will be enhanced by supporting record classes: a new construction for the definition of shallowly immutable, transparent carriers for a fixed set of values, its record components. These feature will constitute a preview feature (JEP 12) in Java SE 14.
Specification
diff -r fd51f02ef75c -r cba5b68a6424 src/jdk.compiler/share/classes/com/sun/source/tree/ClassTree.java
--- a/src/jdk.compiler/share/classes/com/sun/source/tree/ClassTree.java Mon Nov 18 16:30:21 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/ClassTree.java Mon Nov 18 16:40:42 2019 -0500
@@ -29,7 +29,7 @@
import javax.lang.model.element.Name;
/**
- * A tree node for a class, interface, enum, or annotation
+ * A tree node for a class, interface, enum, record, or annotation
* type declaration.
*
* For example:
@@ -44,6 +44,7 @@
*
* @jls 8.1 Class Declarations
* @jls 8.9 Enum Types
+ * @jls 8.10 Record Types
* @jls 9.1 Interface Declarations
* @jls 9.6 Annotation Types
*
diff -r fd51f02ef75c -r cba5b68a6424 src/jdk.compiler/share/classes/com/sun/source/tree/Tree.java
--- a/src/jdk.compiler/share/classes/com/sun/source/tree/Tree.java Mon Nov 18 16:30:21 2019 -0500
+++ b/src/jdk.compiler/share/classes/com/sun/source/tree/Tree.java Mon Nov 18 16:40:42 2019 -0500
@@ -644,6 +644,20 @@
PROVIDES(ProvidesTree.class),
/**
+ * {@preview Associated with records, a preview feature of the Java language.
+ *
+ * This enum constant is associated with <i>records</i>, a preview
+ * feature of the Java language. Preview features
+ * may be removed in a future release, or upgraded to permanent
+ * features of the Java language.}
+ *
+ * Used for instances of {@link ClassTree} representing records.
+ *
+ * @since 14
+ */
+ RECORD(ClassTree.class),
+
+ /**
* Used for instances of {@link RequiresTree} representing
* requires directives in a module declaration.
*/
In addition, proposed changes to the JLS are available at http://cr.openjdk.java.net/~gbierman/jep359/jep359-20191125/specs/records-jls.html
- csr of
-
JDK-8225054 Compiler implementation for records
- Resolved
- relates to
-
JDK-8225057 JLS changes for Records (Preview)
- Resolved