From 82e810423c1729f38afde3966eee63723bc647ff Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 18 Jun 2024 11:33:03 +0200 Subject: [PATCH] Ignore getters for Kotlin types deviating from Java Beans spec. We now ignore Kotlin getters that are either static methods or would require additional arguments. Closes #3109 --- .../data/util/KotlinBeanInfoFactory.java | 5 +++++ .../data/util/InlineClassWithProperty.kt | 21 +++++++++++++++++++ .../util/KotlinBeanInfoFactoryUnitTests.kt | 8 +++++++ 3 files changed, 34 insertions(+) create mode 100644 src/test/kotlin/org/springframework/data/util/InlineClassWithProperty.kt diff --git a/src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java b/src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java index eab80c9bc7..c6350647d8 100644 --- a/src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java +++ b/src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java @@ -28,6 +28,7 @@ import java.beans.PropertyDescriptor; import java.beans.SimpleBeanInfo; import java.lang.reflect.Method; +import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.LinkedHashSet; import java.util.Set; @@ -68,6 +69,10 @@ public BeanInfo getBeanInfo(Class beanClass) throws IntrospectionException { Method getter = ReflectJvmMapping.getJavaGetter(property); Method setter = property instanceof KMutableProperty kmp ? ReflectJvmMapping.getJavaSetter(kmp) : null; + if (getter != null && (Modifier.isStatic(getter.getModifiers()) || getter.getParameterCount() != 0)) { + continue; + } + if (getter != null && setter != null && setter.getParameterCount() == 1) { if (!getter.getReturnType().equals(setter.getParameters()[0].getType())) { // filter asymmetric getters/setters from being considered a Java Beans property diff --git a/src/test/kotlin/org/springframework/data/util/InlineClassWithProperty.kt b/src/test/kotlin/org/springframework/data/util/InlineClassWithProperty.kt new file mode 100644 index 0000000000..96af7b9870 --- /dev/null +++ b/src/test/kotlin/org/springframework/data/util/InlineClassWithProperty.kt @@ -0,0 +1,21 @@ +/* + * Copyright 2024 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 + * + * https://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.data.util + +@JvmInline +value class InlineClassWithProperty(val value: String) { + val foo: String get() = "foo-$value" +} diff --git a/src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt b/src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt index 659f3a300a..ada3014b61 100644 --- a/src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt +++ b/src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt @@ -48,6 +48,14 @@ class KotlinBeanInfoFactoryUnitTests { } } + @Test // GH-3109 + internal fun considersJavaBeansGettersOnly() { + + val pds = BeanUtils.getPropertyDescriptors(InlineClassWithProperty::class.java) + + assertThat(pds).hasSize(1).extracting("name").contains("value") + } + @Test internal fun determinesInlineClassConsumerProperties() {