Skip to content

Commit 3e667cc

Browse files
committed
8265356: need code example for getting canonical constructor of a Record
Reviewed-by: smarks
1 parent f86b70c commit 3e667cc

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

src/java.base/share/classes/java/lang/Class.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,7 +1631,7 @@ public Class<?> getEnclosingClass() throws SecurityException {
16311631
* in source code, can have a non-empty name including special
16321632
* characters, such as "{@code $}".
16331633
*
1634-
* <p>The simple name of an {@linkplain isArray() array class} is the simple name of the
1634+
* <p>The simple name of an {@linkplain #isArray() array class} is the simple name of the
16351635
* component type with "[]" appended. In particular the simple
16361636
* name of an array class whose component type is anonymous is "[]".
16371637
*
@@ -2361,6 +2361,19 @@ public Field[] getDeclaredFields() throws SecurityException {
23612361
* Conversely, if {@link #isRecord()} returns {@code true}, then this method
23622362
* returns a non-null value.
23632363
*
2364+
* @apiNote
2365+
* <p> The following method can be used to find the record canonical constructor:
2366+
*
2367+
* <pre>{@code
2368+
* static <T extends Record> Constructor<T> getCanonicalConstructor(Class<T> cls)
2369+
* throws NoSuchMethodException {
2370+
* Class<?>[] paramTypes =
2371+
* Arrays.stream(cls.getRecordComponents())
2372+
* .map(RecordComponent::getType)
2373+
* .toArray(Class<?>[]::new);
2374+
* return cls.getDeclaredConstructor(paramTypes);
2375+
* }}</pre>
2376+
*
23642377
* @return An array of {@code RecordComponent} objects representing all the
23652378
* record components of this record class, or {@code null} if this
23662379
* class is not a record class
@@ -3113,15 +3126,15 @@ static <T> boolean casReflectionData(Class<?> clazz,
31133126
return unsafe.compareAndSetReference(clazz, reflectionDataOffset, oldData, newData);
31143127
}
31153128

3116-
static <T> boolean casAnnotationType(Class<?> clazz,
3117-
AnnotationType oldType,
3118-
AnnotationType newType) {
3129+
static boolean casAnnotationType(Class<?> clazz,
3130+
AnnotationType oldType,
3131+
AnnotationType newType) {
31193132
return unsafe.compareAndSetReference(clazz, annotationTypeOffset, oldType, newType);
31203133
}
31213134

3122-
static <T> boolean casAnnotationData(Class<?> clazz,
3123-
AnnotationData oldData,
3124-
AnnotationData newData) {
3135+
static boolean casAnnotationData(Class<?> clazz,
3136+
AnnotationData oldData,
3137+
AnnotationData newData) {
31253138
return unsafe.compareAndSetReference(clazz, annotationDataOffset, oldData, newData);
31263139
}
31273140
}

src/java.base/share/classes/java/lang/Record.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@
7979
* <cite>Java Object Serialization Specification,</cite> Section 1.13,
8080
* "Serialization of Records"</a>.
8181
*
82+
* @apiNote
83+
* A record class structure can be obtained at runtime via reflection.
84+
* See {@link Class#isRecord()} and {@link Class#getRecordComponents()} for more details.
85+
*
8286
* @jls 8.10 Record Types
8387
* @since 16
8488
*/

0 commit comments

Comments
 (0)