Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8265356: need code example for getting canonical constructor of a Record #3556

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions src/java.base/share/classes/java/lang/Class.java
Expand Up @@ -1631,7 +1631,7 @@ public Class<?> getEnclosingClass() throws SecurityException {
* 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 "[]".
*
Expand Down Expand Up @@ -2361,6 +2361,19 @@ public Field[] getDeclaredFields() throws SecurityException {
* Conversely, if {@link #isRecord()} returns {@code true}, then this method
* returns a non-null value.
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to mention, this example should be within an @apiNote.

* @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
Expand Down Expand Up @@ -3113,15 +3126,15 @@ static <T> boolean casReflectionData(Class<?> clazz,
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);
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/java.base/share/classes/java/lang/Record.java
Expand Up @@ -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
*/
Expand Down