Browse Source

Consider getters using get as getter for boolean Kotlin properties.

We now additionally consider get-prefixed methods in addition to is-prefixed methods as getters for boolean properties.

Closes #3249
3.4.x
Mark Paluch 10 months ago
parent
commit
3c625cad28
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 28
      src/main/java/org/springframework/data/util/KotlinBeanInfoFactory.java
  2. 1
      src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java
  3. 40
      src/test/kotlin/org/springframework/data/projection/Entities.kt
  4. 35
      src/test/kotlin/org/springframework/data/util/KotlinBeanInfoFactoryUnitTests.kt

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

@ -94,18 +94,9 @@ public class KotlinBeanInfoFactory implements BeanInfoFactory, Ordered {
if (member instanceof KProperty<?> property) { if (member instanceof KProperty<?> property) {
Method getter = ReflectJvmMapping.getJavaGetter(property);
Method setter = property instanceof KMutableProperty<?> kmp ? ReflectJvmMapping.getJavaSetter(kmp) : null; Method setter = property instanceof KMutableProperty<?> kmp ? ReflectJvmMapping.getJavaSetter(kmp) : null;
if (getter == null) {
Type javaType = ReflectJvmMapping.getJavaType(property.getReturnType()); Type javaType = ReflectJvmMapping.getJavaType(property.getReturnType());
getter = ReflectionUtils.findMethod(beanClass, Method getter = findGetter(beanClass, property, javaType);
javaType == Boolean.TYPE ? "is" : "get" + StringUtils.capitalize(property.getName()));
}
if (getter != null) {
getter = ClassUtils.getMostSpecificMethod(getter, beanClass);
}
if (getter != null && (Modifier.isStatic(getter.getModifiers()) || getter.getParameterCount() != 0)) { if (getter != null && (Modifier.isStatic(getter.getModifiers()) || getter.getParameterCount() != 0)) {
continue; continue;
@ -123,6 +114,21 @@ public class KotlinBeanInfoFactory implements BeanInfoFactory, Ordered {
} }
} }
private static @Nullable Method findGetter(Class<?> beanClass, KProperty<?> property, Type javaType) {
Method getter = ReflectJvmMapping.getJavaGetter(property);
if (getter == null && javaType == Boolean.TYPE) {
getter = ReflectionUtils.findMethod(beanClass, "is" + StringUtils.capitalize(property.getName()));
}
if (getter == null) {
getter = ReflectionUtils.findMethod(beanClass, "get" + StringUtils.capitalize(property.getName()));
}
return getter != null ? ClassUtils.getMostSpecificMethod(getter, beanClass) : null;
}
private static void collectBasicJavaProperties(Class<?> beanClass, Map<String, PropertyDescriptor> descriptors) private static void collectBasicJavaProperties(Class<?> beanClass, Map<String, PropertyDescriptor> descriptors)
throws IntrospectionException { throws IntrospectionException {
@ -204,5 +210,7 @@ public class KotlinBeanInfoFactory implements BeanInfoFactory, Ordered {
public Method getWriteMethod() { public Method getWriteMethod() {
return this.writeMethod; return this.writeMethod;
} }
} }
} }

1
src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java

@ -342,6 +342,7 @@ class ProxyProjectionFactoryUnitTests {
assertThat(excerpt.getBirthdate()).contains(LocalDateTime.of(1967, 1, 9, 0, 0)); assertThat(excerpt.getBirthdate()).contains(LocalDateTime.of(1967, 1, 9, 0, 0));
} }
interface Contact {} interface Contact {}
interface CustomerWithLocalDateTime { interface CustomerWithLocalDateTime {

40
src/test/kotlin/org/springframework/data/projection/Entities.kt

@ -0,0 +1,40 @@
/*
* Copyright 2025 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.projection
open class KClassWithJavaGetter() {
private var fromOuterSpace: Boolean = false
open fun getFromOuterSpace() = fromOuterSpace
open fun setFromOuterSpace(newValue: Boolean) {
this.fromOuterSpace = newValue
}
}
open class KClassWithIsGetter() {
private var fromOuterSpace: Boolean = false
open fun isFromOuterSpace() = fromOuterSpace
open fun setFromOuterSpace(newValue: Boolean) {
this.fromOuterSpace = newValue
}
}

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

@ -58,6 +58,16 @@ class KotlinBeanInfoFactoryUnitTests {
assertThat(pds).hasSize(1).extracting("name").contains("value") assertThat(pds).hasSize(1).extracting("name").contains("value")
} }
@Test // GH-3249
internal fun considersBooleanGetAndIsGetters() {
val isAndGet = BeanUtils.getPropertyDescriptors(KClassWithIsGetter::class.java)
assertThat(isAndGet[0].readMethod.name).isEqualTo("isFromOuterSpace")
val getOnly = BeanUtils.getPropertyDescriptors(KClassWithGetGetter::class.java)
assertThat(getOnly[0].readMethod.name).isEqualTo("getFromOuterSpace")
}
@Test @Test
internal fun determinesInlineClassConsumerProperties() { internal fun determinesInlineClassConsumerProperties() {
@ -200,4 +210,29 @@ class KotlinBeanInfoFactoryUnitTests {
class User : AbstractAuditable() { class User : AbstractAuditable() {
var name: String? = null var name: String? = null
} }
open class KClassWithGetGetter() {
private var fromOuterSpace: Boolean = false
open fun getFromOuterSpace() = fromOuterSpace
open fun setFromOuterSpace(newValue: Boolean) {
this.fromOuterSpace = newValue
}
}
open class KClassWithIsGetter() {
private var fromOuterSpace: Boolean = false
open fun isFromOuterSpace() = fromOuterSpace
open fun getFromOuterSpace() = fromOuterSpace
open fun setFromOuterSpace(newValue: Boolean) {
this.fromOuterSpace = newValue
}
}
} }

Loading…
Cancel
Save