Browse Source

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
pull/3119/head
Mark Paluch 2 years ago
parent
commit
2bed1f2ee5
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 5
      src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java
  2. 21
      src/test/kotlin/org/springframework/data/util/InlineClassWithProperty.kt
  3. 8
      src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt

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

@ -28,6 +28,7 @@ import java.beans.IntrospectionException; @@ -28,6 +28,7 @@ import java.beans.IntrospectionException;
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.Collection;
import java.util.LinkedHashSet;
@ -70,6 +71,10 @@ public class KotlinBeanInfoFactory implements BeanInfoFactory, Ordered { @@ -70,6 +71,10 @@ public class KotlinBeanInfoFactory implements BeanInfoFactory, Ordered {
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

21
src/test/kotlin/org/springframework/data/util/InlineClassWithProperty.kt

@ -0,0 +1,21 @@ @@ -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"
}

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

@ -48,6 +48,14 @@ class KotlinBeanInfoFactoryUnitTests { @@ -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() {

Loading…
Cancel
Save