@@ -914,27 +914,31 @@ private static Object toEnum(String value, Class<?> type) {
914914 }
915915
916916 private static Object toRecord (Map <?, ?> map , Class <?> type ) {
917- var recordComponents = type . getRecordComponents ( );
917+ var properties = getProperties ( type );
918918
919- var parameterTypes = new Class <?>[recordComponents .length ];
920- var arguments = new Object [recordComponents .length ];
919+ var n = properties .size ();
921920
922- for ( var i = 0 ; i < recordComponents . length ; i ++) {
923- var recordComponent = recordComponents [ i ];
921+ var parameterTypes = new Class <?>[ n ];
922+ var arguments = new Object [ n ];
924923
925- parameterTypes [ i ] = recordComponent . getType () ;
924+ var i = 0 ;
926925
927- var accessor = recordComponent .getAccessor ();
926+ for (var entry : properties .entrySet ()) {
927+ var key = entry .getKey ();
928928
929- var key = getKey ( accessor , recordComponent . getName ()) ;
929+ var accessor = entry . getValue (). accessor ;
930930
931931 var value = map .get (key );
932932
933933 if (accessor .getAnnotation (Required .class ) != null && value == null ) {
934934 throw new RequiredValueException (key , type );
935935 }
936936
937- arguments [i ] = coerceGeneric (value , recordComponent .getGenericType ());
937+ parameterTypes [i ] = accessor .getReturnType ();
938+
939+ arguments [i ] = coerceGeneric (value , accessor .getGenericReturnType ());
940+
941+ i ++;
938942 }
939943
940944 Constructor <?> constructor ;
@@ -970,14 +974,14 @@ private static Object toBean(Map<?, ?> map, Class<?> type) {
970974 }
971975
972976 for (var entry : getProperties (type ).entrySet ()) {
977+ var key = entry .getKey ();
978+
973979 var property = entry .getValue ();
974980
975981 if (property .mutator == null ) {
976982 continue ;
977983 }
978984
979- var key = entry .getKey ();
980-
981985 var value = map .get (key );
982986
983987 if (property .accessor .getAnnotation (Required .class ) != null && value == null ) {
@@ -996,7 +1000,7 @@ private static Object toBean(Map<?, ?> map, Class<?> type) {
9961000 }
9971001
9981002 /**
999- * Returns the properties for a given type, sorted by name .
1003+ * Returns the properties for a given type.
10001004 *
10011005 * @param type
10021006 * The bean type.
@@ -1009,18 +1013,23 @@ public static Map<String, Property> getProperties(Class<?> type) {
10091013 }
10101014
10111015 private static Map <String , Property > computeProperties (Class <?> type ) {
1012- var accessors = new HashMap <String , Method >();
1013- var mutatorMap = new HashMap <String , List <Method >>();
1014-
1016+ Map <String , Property > properties ;
10151017 if (type .isRecord ()) {
1018+ properties = new LinkedHashMap <>();
1019+
10161020 var recordComponents = type .getRecordComponents ();
10171021
10181022 for (var i = 0 ; i < recordComponents .length ; i ++) {
10191023 var recordComponent = recordComponents [i ];
10201024
1021- accessors .put (recordComponent .getName (), recordComponent .getAccessor ());
1025+ var accessor = recordComponent .getAccessor ();
1026+
1027+ properties .put (getKey (accessor , recordComponent .getName ()), new Property (accessor , null ));
10221028 }
10231029 } else {
1030+ var accessors = new HashMap <String , Method >();
1031+ var mutatorLists = new HashMap <String , List <Method >>();
1032+
10241033 var methods = type .getMethods ();
10251034
10261035 for (var i = 0 ; i < methods .length ; i ++) {
@@ -1039,24 +1048,26 @@ private static Map<String, Property> computeProperties(Class<?> type) {
10391048 if (method .getParameterCount () == 0 ) {
10401049 accessors .put (propertyName , method );
10411050 } else {
1042- mutatorMap .computeIfAbsent (propertyName , key -> new LinkedList <>()).add (method );
1051+ mutatorLists .computeIfAbsent (propertyName , key -> new LinkedList <>()).add (method );
10431052 }
10441053 }
1045- }
10461054
1047- return immutableMapOf (sortedMapOf (mapAll (accessors .entrySet (), entry -> {
1048- var propertyName = entry .getKey ();
1055+ properties = sortedMapOf (mapAll (accessors .entrySet (), entry -> {
1056+ var propertyName = entry .getKey ();
1057+
1058+ var accessor = entry .getValue ();
10491059
1050- var accessor = entry . getValue ();
1060+ var propertyType = accessor . getReturnType ();
10511061
1052- var propertyType = accessor . getReturnType ( );
1062+ var mutatorList = coalesce ( mutatorLists . get ( propertyName ), () -> emptyListOf ( Method . class ) );
10531063
1054- var mutatorList = coalesce ( mutatorMap . get ( propertyName ), () -> emptyListOf ( Method . class ));
1064+ var mutator = firstOf ( filter ( mutatorList , method -> method . getParameterTypes ()[ 0 ] == propertyType ));
10551065
1056- var mutator = firstOf (filter (mutatorList , method -> method .getParameterTypes ()[0 ] == propertyType ));
1066+ return entry (getKey (accessor , propertyName ), new Property (accessor , mutator ));
1067+ }));
1068+ }
10571069
1058- return entry (getKey (accessor , propertyName ), new Property (accessor , mutator ));
1059- })));
1070+ return immutableMapOf (properties );
10601071 }
10611072
10621073 private static String getPropertyName (Method method ) {
0 commit comments