Skip to content

Commit

Permalink
Ignore getters for Kotlin types deviating from Java Beans spec.
Browse files Browse the repository at this point in the history
We now ignore Kotlin getters that are either static methods or would require additional arguments.

Closes #3109
  • Loading branch information
mp911de committed Jun 18, 2024
1 parent 4f5b6dc commit 82e8104
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down

0 comments on commit 82e8104

Please sign in to comment.