Skip to content

Commit 73db208

Browse files
committed
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
1 parent 38490f1 commit 73db208

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public String[] getParameterNames(Method method) {
5858
@Override
5959
@Nullable
6060
public String[] getParameterNames(Constructor<?> ctor) {
61-
if (!KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
61+
if (ctor.getDeclaringClass().isEnum() || !KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
6262
return null;
6363
}
6464

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.core
18+
19+
import org.junit.Assert.assertEquals
20+
import org.junit.Test
21+
22+
class KotlinDefaultParameterNameDiscovererTests {
23+
24+
private val parameterNameDiscoverer = DefaultParameterNameDiscoverer()
25+
26+
enum class MyEnum {
27+
ONE, TWO
28+
}
29+
30+
@Test // SPR-16931
31+
fun getParameterNamesOnEnum() {
32+
val constructor = MyEnum::class.java.declaredConstructors[0]
33+
val actualParams = parameterNameDiscoverer.getParameterNames(constructor)
34+
assertEquals(2, actualParams!!.size)
35+
}
36+
}

0 commit comments

Comments
 (0)