From 2c5a1af236e6c90460b2279fd0a871d0fc6c9d6e Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Tue, 16 Oct 2018 16:40:02 +0200 Subject: [PATCH] Leverage Java reflection for Kotlin enums As discussed in KT-25165, from a Kotlin POV enum constructors have no parameter, this is an "implementation detail" required for running on the JVM, so it seems relevant to skip Kotlin reflection in that case and just delegate to Java reflection. Issue: SPR-16931 --- ...tlinReflectionParameterNameDiscoverer.java | 2 +- ...tlinDefaultParameterNameDiscovererTests.kt | 36 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt diff --git a/spring-core/src/main/java/org/springframework/core/KotlinReflectionParameterNameDiscoverer.java b/spring-core/src/main/java/org/springframework/core/KotlinReflectionParameterNameDiscoverer.java index 129a364a3f..597c3b6804 100644 --- a/spring-core/src/main/java/org/springframework/core/KotlinReflectionParameterNameDiscoverer.java +++ b/spring-core/src/main/java/org/springframework/core/KotlinReflectionParameterNameDiscoverer.java @@ -58,7 +58,7 @@ public class KotlinReflectionParameterNameDiscoverer implements ParameterNameDis @Override @Nullable public String[] getParameterNames(Constructor ctor) { - if (!KotlinDetector.isKotlinType(ctor.getDeclaringClass())) { + if (ctor.getDeclaringClass().isEnum() || !KotlinDetector.isKotlinType(ctor.getDeclaringClass())) { return null; } diff --git a/spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt b/spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt new file mode 100644 index 0000000000..6f794bc9ac --- /dev/null +++ b/spring-core/src/test/kotlin/org/springframework/core/KotlinDefaultParameterNameDiscovererTests.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.core + +import org.junit.Assert.assertEquals +import org.junit.Test + +class KotlinDefaultParameterNameDiscovererTests { + + private val parameterNameDiscoverer = DefaultParameterNameDiscoverer() + + enum class MyEnum { + ONE, TWO + } + + @Test // SPR-16931 + fun getParameterNamesOnEnum() { + val constructor = MyEnum::class.java.declaredConstructors[0] + val actualParams = parameterNameDiscoverer.getParameterNames(constructor) + assertEquals(2, actualParams!!.size) + } +} -- GitLab