File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
main/java/org/springframework/beans
test/java/org/springframework/beans Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 2323import java .lang .reflect .InvocationTargetException ;
2424import java .lang .reflect .Method ;
2525import java .lang .reflect .Modifier ;
26+ import java .lang .reflect .RecordComponent ;
2627import java .lang .reflect .Type ;
2728import java .util .Arrays ;
2829import java .util .Collections ;
@@ -252,6 +253,19 @@ else if (ctors.length == 0) {
252253 return (Constructor <T >) ctors [0 ];
253254 }
254255 }
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+ }
255269
256270 // Several constructors -> let's try to take the default constructor
257271 try {
Original file line number Diff line number Diff line change @@ -521,10 +521,26 @@ void isNotSimpleProperty(Class<?> type) {
521521 assertThat (BeanUtils .isSimpleProperty (type )).as ("Type [" + type .getName () + "] should not be a simple property" ).isFalse ();
522522 }
523523
524+ @ Test
525+ void resolveRecordConstructor () throws NoSuchMethodException {
526+ assertThat (BeanUtils .getResolvableConstructor (RecordWithMultiplePublicConstructors .class ))
527+ .isEqualTo (getRecordWithMultipleVariationsConstructor ());
528+ }
529+
524530 private void assertSignatureEquals (Method desiredMethod , String signature ) {
525531 assertThat (BeanUtils .resolveSignature (signature , MethodSignatureBean .class )).isEqualTo (desiredMethod );
526532 }
527533
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+
528544
529545 @ SuppressWarnings ("unused" )
530546 private static class NumberHolder {
You can’t perform that action at this time.
0 commit comments