Browse Source

Support non-standard classes in Kotlin reflection discovery methods

Issue: SPR-15999
pull/1520/merge
Sebastien Deleuze 7 years ago
parent
commit
3996f33399
  1. 17
      spring-beans/src/main/java/org/springframework/beans/BeanUtils.java
  2. 15
      spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java
  3. 18
      spring-core/src/main/java/org/springframework/core/KotlinReflectionParameterNameDiscoverer.java

17
spring-beans/src/main/java/org/springframework/beans/BeanUtils.java

@ -735,14 +735,19 @@ public abstract class BeanUtils { @@ -735,14 +735,19 @@ public abstract class BeanUtils {
*/
@Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
KFunction<T> primaryConstructor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz));
if (primaryConstructor == null) {
try {
KFunction<T> primaryConstructor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz));
if (primaryConstructor == null) {
return null;
}
Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryConstructor);
Assert.notNull(constructor,
() -> "Failed to find Java constructor corresponding to Kotlin primary constructor: " + clazz.getName());
return constructor;
}
catch (UnsupportedOperationException ex) {
return null;
}
Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryConstructor);
Assert.notNull(constructor,
() -> "Failed to find Java constructor corresponding to Kotlin primary constructor: " + clazz.getName());
return constructor;
}
/**

15
spring-beans/src/main/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessor.java

@ -781,13 +781,18 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean @@ -781,13 +781,18 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
*/
@Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
KFunction<T> primaryConstructor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz));
if (primaryConstructor == null) {
try {
KFunction<T> primaryConstructor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz));
if (primaryConstructor == null) {
return null;
}
Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryConstructor);
Assert.notNull(constructor, "Can't get the Java constructor corresponding to the Kotlin primary constructor of " + clazz.getName());
return constructor;
}
catch (UnsupportedOperationException ex) {
return null;
}
Constructor<T> constructor = ReflectJvmMapping.getJavaConstructor(primaryConstructor);
Assert.notNull(constructor, "Can't get the Java constructor corresponding to the Kotlin primary constructor of " + clazz.getName());
return constructor;
}
}

18
spring-core/src/main/java/org/springframework/core/KotlinReflectionParameterNameDiscoverer.java

@ -62,8 +62,13 @@ public class KotlinReflectionParameterNameDiscoverer implements ParameterNameDis @@ -62,8 +62,13 @@ public class KotlinReflectionParameterNameDiscoverer implements ParameterNameDis
if (!useKotlinSupport(method.getDeclaringClass())) {
return null;
}
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
return (function != null ? getParameterNames(function.getParameters()) : null);
try {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
return (function != null ? getParameterNames(function.getParameters()) : null);
}
catch (UnsupportedOperationException ex) {
return null;
}
}
@Override
@ -72,8 +77,13 @@ public class KotlinReflectionParameterNameDiscoverer implements ParameterNameDis @@ -72,8 +77,13 @@ public class KotlinReflectionParameterNameDiscoverer implements ParameterNameDis
if (!useKotlinSupport(ctor.getDeclaringClass())) {
return null;
}
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(ctor);
return (function != null ? getParameterNames(function.getParameters()) : null);
try {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(ctor);
return (function != null ? getParameterNames(function.getParameters()) : null);
}
catch (UnsupportedOperationException ex) {
return null;
}
}
@Nullable

Loading…
Cancel
Save