Skip to content

Exclude "entries" property from bean info for kotlin enum class #2991

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* {@link BeanInfoFactory} specific to Kotlin types using Kotlin reflection to determine bean properties.
*
* @author Mark Paluch
* @author Yanming Zhou
* @since 3.2
* @see JvmClassMappingKt
* @see ReflectJvmMapping
Expand All @@ -64,6 +65,12 @@ public BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException {

if (member instanceof KProperty<?> property) {

// Kotlin introduce auto-generated "entries" property for enum class since 1.8.20 and stable from 1.9.0
// see https://youtrack.jetbrains.com/issue/KT-48872
if (beanClass.isEnum() && property.getName().equals("entries")) {
continue;
}

Method getter = ReflectJvmMapping.getJavaGetter(property);
Method setter = property instanceof KMutableProperty<?> kmp ? ReflectJvmMapping.getJavaSetter(kmp) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.springframework.beans.BeanUtils
/**
* Unit tests for [KotlinBeanInfoFactory].
* @author Mark Paluch
* @author Yanming Zhou
*/
class KotlinBeanInfoFactoryUnitTests {

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

@Test // GH-2990
internal fun determinesEnumClassProperties() {

val pds = BeanUtils.getPropertyDescriptors(Gender::class.java)

assertThat(pds).extracting("name").containsOnly("value", "name", "ordinal");
}

data class SimpleDataClass(val id: String, var name: String)

@JvmInline
Expand All @@ -86,4 +95,7 @@ class KotlinBeanInfoFactoryUnitTests {
fun getFirstname(): String
}

enum class Gender(val value: Int) {
Male(0), FEMALE(1)
}
}