File tree 2 files changed +30
-0
lines changed
main/java/org/springframework/beans
test/java/org/springframework/beans 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 23
23
import java .lang .reflect .InvocationTargetException ;
24
24
import java .lang .reflect .Method ;
25
25
import java .lang .reflect .Modifier ;
26
+ import java .lang .reflect .RecordComponent ;
26
27
import java .lang .reflect .Type ;
27
28
import java .util .Arrays ;
28
29
import java .util .Collections ;
@@ -252,6 +253,19 @@ else if (ctors.length == 0) {
252
253
return (Constructor <T >) ctors [0 ];
253
254
}
254
255
}
256
+ else if (clazz .isRecord ()) {
257
+ try {
258
+ // if record -> use canonical constructor, which is always presented
259
+ Class <?>[] paramTypes
260
+ = Arrays .stream (clazz .getRecordComponents ())
261
+ .map (RecordComponent ::getType )
262
+ .toArray (Class <?>[]::new );
263
+ return clazz .getDeclaredConstructor (paramTypes );
264
+ }
265
+ catch (NoSuchMethodException ex ) {
266
+ // Giving up with record...
267
+ }
268
+ }
255
269
256
270
// Several constructors -> let's try to take the default constructor
257
271
try {
Original file line number Diff line number Diff line change @@ -521,10 +521,26 @@ void isNotSimpleProperty(Class<?> type) {
521
521
assertThat (BeanUtils .isSimpleProperty (type )).as ("Type [" + type .getName () + "] should not be a simple property" ).isFalse ();
522
522
}
523
523
524
+ @ Test
525
+ void resolveRecordConstructor () throws NoSuchMethodException {
526
+ assertThat (BeanUtils .getResolvableConstructor (RecordWithMultiplePublicConstructors .class ))
527
+ .isEqualTo (getRecordWithMultipleVariationsConstructor ());
528
+ }
529
+
524
530
private void assertSignatureEquals (Method desiredMethod , String signature ) {
525
531
assertThat (BeanUtils .resolveSignature (signature , MethodSignatureBean .class )).isEqualTo (desiredMethod );
526
532
}
527
533
534
+ public record RecordWithMultiplePublicConstructors (String value , String name ) {
535
+ public RecordWithMultiplePublicConstructors (String value ) {
536
+ this (value , "default value" );
537
+ }
538
+ }
539
+
540
+ private Constructor <RecordWithMultiplePublicConstructors > getRecordWithMultipleVariationsConstructor () throws NoSuchMethodException {
541
+ return RecordWithMultiplePublicConstructors .class .getConstructor (String .class , String .class );
542
+ }
543
+
528
544
529
545
@ SuppressWarnings ("unused" )
530
546
private static class NumberHolder {
You can’t perform that action at this time.
0 commit comments