Skip to content

Commit db36bb2

Browse files
committed
Exclude "entries" property from bean info for kotlin enum class
Fix spring-projectsGH-2990
1 parent cbcf186 commit db36bb2

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java

+7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* {@link BeanInfoFactory} specific to Kotlin types using Kotlin reflection to determine bean properties.
4040
*
4141
* @author Mark Paluch
42+
* @author Yanming Zhou
4243
* @since 3.2
4344
* @see JvmClassMappingKt
4445
* @see ReflectJvmMapping
@@ -64,6 +65,12 @@ public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {
6465

6566
if (member instanceof KProperty<?> property) {
6667

68+
// Kotlin introduce auto-generated "entries" property for enum class since 1.8.20 and stable from 1.9.0
69+
// see https://youtrack.jetbrains.com/issue/KT-48872
70+
if (beanClass.isEnum() && property.getName().equals("entries")) {
71+
continue;
72+
}
73+
6774
Method getter = ReflectJvmMapping.getJavaGetter(property);
6875
Method setter = property instanceof KMutableProperty<?> kmp ? ReflectJvmMapping.getJavaSetter(kmp) : null;
6976

src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt

+12
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.springframework.beans.BeanUtils
2222
/**
2323
* Unit tests for [KotlinBeanInfoFactory].
2424
* @author Mark Paluch
25+
* @author Yanming Zhou
2526
*/
2627
class KotlinBeanInfoFactoryUnitTests {
2728

@@ -73,6 +74,14 @@ class KotlinBeanInfoFactoryUnitTests {
7374
assertThat(pds).hasSize(1).extracting("name").containsOnly("firstname")
7475
}
7576

77+
@Test // GH-2990
78+
internal fun determinesEnumClassProperties() {
79+
80+
val pds = BeanUtils.getPropertyDescriptors(Gender::class.java)
81+
82+
assertThat(pds).extracting("name").containsOnly("value", "name", "ordinal");
83+
}
84+
7685
data class SimpleDataClass(val id: String, var name: String)
7786

7887
@JvmInline
@@ -86,4 +95,7 @@ class KotlinBeanInfoFactoryUnitTests {
8695
fun getFirstname(): String
8796
}
8897

98+
enum class Gender(val value: Int) {
99+
Male(0), FEMALE(1)
100+
}
89101
}

0 commit comments

Comments
 (0)