2222import java .lang .reflect .Constructor ;
2323import java .util .ArrayList ;
2424import java .util .List ;
25+ import java .util .Map ;
2526import java .util .function .Function ;
27+ import java .util .stream .Collectors ;
2628import java .util .stream .IntStream ;
2729
2830import org .springframework .data .mapping .InstanceCreatorMetadata ;
4042 * constructor argument extraction.
4143 *
4244 * @author Mark Paluch
45+ * @author Yohan Lee
4346 * @since 3.1
4447 */
4548class KotlinInstantiationDelegate {
4649
4750 private final KFunction <?> constructor ;
4851 private final List <KParameter > kParameters ;
52+ private final Map <KParameter , Integer > indexByKParameter ;
4953 private final List <Function <Object , Object >> wrappers = new ArrayList <>();
5054 private final Constructor <?> constructorToInvoke ;
5155 private final boolean hasDefaultConstructorMarker ;
@@ -62,6 +66,8 @@ public KotlinInstantiationDelegate(PreferredConstructor<?, ?> preferredConstruct
6266
6367 this .constructor = kotlinConstructor ;
6468 this .kParameters = kotlinConstructor .getParameters ();
69+ this .indexByKParameter = IntStream .range (0 , kParameters .size ()).boxed ()
70+ .collect (Collectors .toMap (kParameters ::get , Function .identity ()));
6571 this .constructorToInvoke = constructorToInvoke ;
6672 this .hasDefaultConstructorMarker = hasDefaultConstructorMarker (constructorToInvoke .getParameters ());
6773
@@ -118,7 +124,7 @@ public <P extends PersistentProperty<P>> Object[] extractInvocationArguments(Obj
118124
119125 KotlinDefaultMask defaultMask = KotlinDefaultMask .forConstructor (constructor , it -> {
120126
121- int index = kParameters . indexOf (it );
127+ int index = indexByKParameter . get (it );
122128
123129 Parameter <Object , P > parameter = parameters .get (index );
124130 Class <Object > type = parameter .getType ().getType ();
0 commit comments