Skip to content
Permalink
Browse files
8265356: need code example for getting canonical constructor of a Record
Reviewed-by: smarks
  • Loading branch information
Tagir F. Valeev committed May 1, 2021
1 parent f86b70c commit 3e667cc40521dfb6d07dda07c2f33e37086ee64b
Showing with 24 additions and 7 deletions.
  1. +20 −7 src/java.base/share/classes/java/lang/Class.java
  2. +4 −0 src/java.base/share/classes/java/lang/Record.java
@@ -1631,7 +1631,7 @@ boolean isPartial() {
* in source code, can have a non-empty name including special
* characters, such as "{@code $}".
*
* <p>The simple name of an {@linkplain isArray() array class} is the simple name of the
* <p>The simple name of an {@linkplain #isArray() array class} is the simple name of the
* component type with "[]" appended. In particular the simple
* name of an array class whose component type is anonymous is "[]".
*
@@ -2361,6 +2361,19 @@ public Method getMethod(String name, Class<?>... parameterTypes)
* Conversely, if {@link #isRecord()} returns {@code true}, then this method
* returns a non-null value.
*
* @apiNote
* <p> The following method can be used to find the record canonical constructor:
*
* <pre>{@code
* static <T extends Record> Constructor<T> getCanonicalConstructor(Class<T> cls)
* throws NoSuchMethodException {
* Class<?>[] paramTypes =
* Arrays.stream(cls.getRecordComponents())
* .map(RecordComponent::getType)
* .toArray(Class<?>[]::new);
* return cls.getDeclaredConstructor(paramTypes);
* }}</pre>
*
* @return An array of {@code RecordComponent} objects representing all the
* record components of this record class, or {@code null} if this
* class is not a record class
@@ -3113,15 +3126,15 @@ private String resolveName(String name) {
return unsafe.compareAndSetReference(clazz, reflectionDataOffset, oldData, newData);
}

static <T> boolean casAnnotationType(Class<?> clazz,
AnnotationType oldType,
AnnotationType newType) {
static boolean casAnnotationType(Class<?> clazz,
AnnotationType oldType,
AnnotationType newType) {
return unsafe.compareAndSetReference(clazz, annotationTypeOffset, oldType, newType);
}

static <T> boolean casAnnotationData(Class<?> clazz,
AnnotationData oldData,
AnnotationData newData) {
static boolean casAnnotationData(Class<?> clazz,
AnnotationData oldData,
AnnotationData newData) {
return unsafe.compareAndSetReference(clazz, annotationDataOffset, oldData, newData);
}
}
@@ -79,6 +79,10 @@
* <cite>Java Object Serialization Specification,</cite> Section 1.13,
* "Serialization of Records"</a>.
*
* @apiNote
* A record class structure can be obtained at runtime via reflection.
* See {@link Class#isRecord()} and {@link Class#getRecordComponents()} for more details.
*
* @jls 8.10 Record Types
* @since 16
*/

0 comments on commit 3e667cc

Please sign in to comment.