Fix Kotlin inner class nested configuration handling

Before this commit, Kotlin inner class nested configuration
handling thrown an IndexOutOfBoundsException due to bogus filtering
of its constructor parameter reference to an instance of the outer
class.

This commit keep constructor parameter of type INSTANCE in order to
throw a more meaningful NoSuchBeanDefinitionException.

Issue: SPR-17222
This commit is contained in:
Sebastien Deleuze
2018-09-07 11:17:52 +02:00
parent c8627c05ed
commit 8d45e3e7ef
3 changed files with 50 additions and 23 deletions

View File

@@ -29,6 +29,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import kotlin.reflect.KFunction; import kotlin.reflect.KFunction;
@@ -768,17 +769,21 @@ public class MethodParameter {
} }
else { else {
KFunction<?> function = null; KFunction<?> function = null;
Predicate<KParameter> predicate = null;
if (method != null) { if (method != null) {
function = ReflectJvmMapping.getKotlinFunction(method); function = ReflectJvmMapping.getKotlinFunction(method);
predicate = p -> KParameter.Kind.VALUE.equals(p.getKind());
} }
else if (ctor != null) { else if (ctor != null) {
function = ReflectJvmMapping.getKotlinFunction(ctor); function = ReflectJvmMapping.getKotlinFunction(ctor);
predicate = p -> KParameter.Kind.VALUE.equals(p.getKind()) ||
KParameter.Kind.INSTANCE.equals(p.getKind());
} }
if (function != null) { if (function != null) {
List<KParameter> parameters = function.getParameters(); List<KParameter> parameters = function.getParameters();
KParameter parameter = parameters KParameter parameter = parameters
.stream() .stream()
.filter(p -> KParameter.Kind.VALUE.equals(p.getKind())) .filter(predicate)
.collect(Collectors.toList()) .collect(Collectors.toList())
.get(index); .get(index);
return (parameter.getType().isMarkedNullable() || parameter.isOptional()); return (parameter.getType().isMarkedNullable() || parameter.isOptional());

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2018 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@@ -13,15 +13,11 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.springframework.core package org.springframework.core
import java.lang.reflect.Method
import org.junit.Before
import org.junit.Test import org.junit.Test
import org.junit.Assert.* import org.junit.Assert.*
import java.lang.reflect.Method
/** /**
* Tests for Kotlin support in [MethodParameter]. * Tests for Kotlin support in [MethodParameter].
@@ -32,36 +28,58 @@ import org.junit.Assert.*
*/ */
class KotlinMethodParameterTests { class KotlinMethodParameterTests {
lateinit var nullableMethod: Method private val nullableMethod: Method = javaClass.getMethod("nullable", String::class.java)
lateinit var nonNullableMethod: Method private val nonNullableMethod = javaClass.getMethod("nonNullable", String::class.java)
private val innerClassConstructor = InnerClass::class.java.getConstructor(KotlinMethodParameterTests::class.java)
@Before private val innerClassWithParametersConstructor = InnerClassWithParameter::class.java
@Throws(NoSuchMethodException::class) .getConstructor(KotlinMethodParameterTests::class.java, String::class.java, String::class.java)
fun setup() {
nullableMethod = javaClass.getMethod("nullable", String::class.java) private val regularClassConstructor = RegularClass::class.java.getConstructor(String::class.java, String::class.java)
nonNullableMethod = javaClass.getMethod("nonNullable", String::class.java)
}
@Test @Test
fun `Method parameter nullability`() { fun `Method parameter nullability`() {
assertTrue(MethodParameter(nullableMethod, 0).isOptional()) assertTrue(MethodParameter(nullableMethod, 0).isOptional)
assertFalse(MethodParameter(nonNullableMethod, 0).isOptional()) assertFalse(MethodParameter(nonNullableMethod, 0).isOptional)
} }
@Test @Test
fun `Method return type nullability`() { fun `Method return type nullability`() {
assertTrue(MethodParameter(nullableMethod, -1).isOptional()) assertTrue(MethodParameter(nullableMethod, -1).isOptional)
assertFalse(MethodParameter(nonNullableMethod, -1).isOptional()) assertFalse(MethodParameter(nonNullableMethod, -1).isOptional)
}
@Test // SPR-17222
fun `Inner class constructor`() {
assertFalse(MethodParameter(innerClassConstructor, 0).isOptional)
assertFalse(MethodParameter(innerClassWithParametersConstructor, 0).isOptional)
assertFalse(MethodParameter(innerClassWithParametersConstructor, 1).isOptional)
assertTrue(MethodParameter(innerClassWithParametersConstructor, 2).isOptional)
}
@Test
fun `Regular class constructor`() {
assertFalse(MethodParameter(regularClassConstructor, 0).isOptional)
assertTrue(MethodParameter(regularClassConstructor, 1).isOptional)
} }
@Suppress("unused", "unused_parameter") @Suppress("unused_parameter")
fun nullable(p1: String?): Int? = 42 fun nullable(nullable: String?): Int? = 42
@Suppress("unused", "unused_parameter") @Suppress("unused_parameter")
fun nonNullable(p1: String): Int = 42 fun nonNullable(nonNullable: String): Int = 42
inner class InnerClass
@Suppress("unused_parameter")
inner class InnerClassWithParameter(nonNullable: String, nullable: String?)
@Suppress("unused_parameter")
class RegularClass(nonNullable: String, nullable: String?)
} }

View File

@@ -143,6 +143,10 @@ for serializing / deserializing JSON data is automatically registered when
found in the classpath and a warning message will be logged if Jackson and Kotlin are found in the classpath and a warning message will be logged if Jackson and Kotlin are
detected without the Jackson Kotlin module present. detected without the Jackson Kotlin module present.
Configuration classes can be
https://kotlinlang.org/docs/reference/nested-classes.html[top level or nested but not inner]
since the later requires a reference to the outer class.