Refine Kotlin changes
This refinement ensures the constructor is properly accessible, avoid duplicating current logic and provide a slightly faster implementation of the Kotlin codepath. See gh-24104
This commit is contained in:
parent
edcc559b4a
commit
8e73b57329
|
|
@ -51,7 +51,6 @@ import org.springframework.util.Assert;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.ConcurrentReferenceHashMap;
|
import org.springframework.util.ConcurrentReferenceHashMap;
|
||||||
import org.springframework.util.ObjectUtils;
|
|
||||||
import org.springframework.util.ReflectionUtils;
|
import org.springframework.util.ReflectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
|
@ -189,9 +188,6 @@ public abstract class BeanUtils {
|
||||||
try {
|
try {
|
||||||
ReflectionUtils.makeAccessible(ctor);
|
ReflectionUtils.makeAccessible(ctor);
|
||||||
if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
|
if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
|
||||||
if (ObjectUtils.isEmpty(args)) {
|
|
||||||
return KotlinDelegate.instantiateClass(ctor);
|
|
||||||
}
|
|
||||||
return KotlinDelegate.instantiateClass(ctor, args);
|
return KotlinDelegate.instantiateClass(ctor, args);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
|
@ -884,9 +880,13 @@ public abstract class BeanUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
List<KParameter> parameters = kotlinConstructor.getParameters();
|
List<KParameter> parameters = kotlinConstructor.getParameters();
|
||||||
Map<KParameter, Object> argParameters = CollectionUtils.newHashMap(parameters.size());
|
|
||||||
Assert.isTrue(args.length <= parameters.size(),
|
Assert.isTrue(args.length <= parameters.size(),
|
||||||
"Number of provided arguments should be less of equals than number of constructor parameters");
|
"Number of provided arguments should be less of equals than number of constructor parameters");
|
||||||
|
if (parameters.isEmpty()) {
|
||||||
|
return kotlinConstructor.call();
|
||||||
|
}
|
||||||
|
Map<KParameter, Object> argParameters = CollectionUtils.newHashMap(parameters.size());
|
||||||
for (int i = 0 ; i < args.length ; i++) {
|
for (int i = 0 ; i < args.length ; i++) {
|
||||||
if (!(parameters.get(i).isOptional() && args[i] == null)) {
|
if (!(parameters.get(i).isOptional() && args[i] == null)) {
|
||||||
argParameters.put(parameters.get(i), args[i]);
|
argParameters.put(parameters.get(i), args[i]);
|
||||||
|
|
@ -895,24 +895,6 @@ public abstract class BeanUtils {
|
||||||
return kotlinConstructor.callBy(argParameters);
|
return kotlinConstructor.callBy(argParameters);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Instantiate a Kotlin class using provided no-arg constructor.
|
|
||||||
* @param ctor the constructor of the Kotlin class to instantiate
|
|
||||||
*/
|
|
||||||
public static <T> T instantiateClass(Constructor<T> ctor)
|
|
||||||
throws IllegalAccessException, InvocationTargetException, InstantiationException {
|
|
||||||
|
|
||||||
KFunction<T> kotlinConstructor = ReflectJvmMapping.getKotlinFunction(ctor);
|
|
||||||
if (kotlinConstructor == null) {
|
|
||||||
return ctor.newInstance();
|
|
||||||
}
|
|
||||||
List<KParameter> parameters = kotlinConstructor.getParameters();
|
|
||||||
Assert.isTrue(parameters.isEmpty(), "Default no-args constructor must have no params");
|
|
||||||
Map<KParameter, Object> argParameters = Collections.emptyMap();
|
|
||||||
return kotlinConstructor.callBy(argParameters);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue