Support non-standard classes in Kotlin reflection discovery methods

Issue: SPR-15999
This commit is contained in:
Sebastien Deleuze 2017-09-24 22:44:07 +02:00
parent 65f556c0e2
commit 3996f33399
3 changed files with 35 additions and 15 deletions

View File

@ -735,14 +735,19 @@ public abstract class BeanUtils {
*/ */
@Nullable @Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) { public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
KFunction<T> primaryConstructor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz)); try {
if (primaryConstructor == null) { 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; 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;
} }
/** /**

View File

@ -781,13 +781,18 @@ public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBean
*/ */
@Nullable @Nullable
public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) { public static <T> Constructor<T> findPrimaryConstructor(Class<T> clazz) {
KFunction<T> primaryConstructor = KClasses.getPrimaryConstructor(JvmClassMappingKt.getKotlinClass(clazz)); try {
if (primaryConstructor == null) { 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; 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;
} }
} }

View File

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