From 5455bd1c3074ab5c996f62ecfb9df92aa82578f6 Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 30 Jul 2024 13:17:17 +0200 Subject: [PATCH 1/2] Prepare issue branch. --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index c6dc5a59a0..ba57247c4e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.data spring-data-commons - 3.4.0-SNAPSHOT + 3.4.x-GH-3127-SNAPSHOT Spring Data Core Core Spring concepts underpinning every Spring Data module. From 4b9da4bbaf483d99c21b64f39be765c8e780104e Mon Sep 17 00:00:00 2001 From: Christoph Strobl Date: Tue, 30 Jul 2024 14:03:17 +0200 Subject: [PATCH 2/2] Fix property lookup for projections on Kotlin types. This commit makes sure to use the target objects method to determine the property used for the projection. --- .../PropertyAccessingMethodInterceptor.java | 14 ++++++++--- ...tyAccessingMethodInterceptorUnitTests.java | 10 ++++++++ .../data/projection/WithIsNamedProperty.kt | 23 +++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 src/test/kotlin/org/springframework/data/projection/WithIsNamedProperty.kt diff --git a/src/main/java/org/springframework/data/projection/PropertyAccessingMethodInterceptor.java b/src/main/java/org/springframework/data/projection/PropertyAccessingMethodInterceptor.java index 46a2b21f52..6f19a9d06d 100644 --- a/src/main/java/org/springframework/data/projection/PropertyAccessingMethodInterceptor.java +++ b/src/main/java/org/springframework/data/projection/PropertyAccessingMethodInterceptor.java @@ -33,6 +33,7 @@ * @author Oliver Gierke * @author Mark Paluch * @author Johannes Englmeier + * @author Christoph Strobl * @since 1.10 */ class PropertyAccessingMethodInterceptor implements MethodInterceptor { @@ -54,12 +55,11 @@ public PropertyAccessingMethodInterceptor(Object target) { @Override public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) throws Throwable { - Method method = invocation.getMethod(); - - if (ReflectionUtils.isObjectMethod(method)) { + if (ReflectionUtils.isObjectMethod(invocation.getMethod())) { return invocation.proceed(); } + Method method = lookupTargetMethod(invocation, target.getWrappedClass()); PropertyDescriptor descriptor = BeanUtils.findPropertyForMethod(method); if (descriptor == null) { @@ -81,4 +81,12 @@ public Object invoke(@SuppressWarnings("null") MethodInvocation invocation) thro private static boolean isSetterMethod(Method method, PropertyDescriptor descriptor) { return method.equals(descriptor.getWriteMethod()); } + + private static Method lookupTargetMethod(MethodInvocation invocation, Class targetType) { + + Method method = BeanUtils.findMethod(targetType, invocation.getMethod().getName(), + invocation.getMethod().getParameterTypes()); + + return method != null ? method : invocation.getMethod(); + } } diff --git a/src/test/java/org/springframework/data/projection/PropertyAccessingMethodInterceptorUnitTests.java b/src/test/java/org/springframework/data/projection/PropertyAccessingMethodInterceptorUnitTests.java index c67cae1516..d09f4b1ea6 100755 --- a/src/test/java/org/springframework/data/projection/PropertyAccessingMethodInterceptorUnitTests.java +++ b/src/test/java/org/springframework/data/projection/PropertyAccessingMethodInterceptorUnitTests.java @@ -32,6 +32,7 @@ * * @author Oliver Gierke * @author Mark Paluch + * @author Christoph Strobl */ @ExtendWith(MockitoExtension.class) class PropertyAccessingMethodInterceptorUnitTests { @@ -111,6 +112,15 @@ void throwsAppropriateExceptionIfThePropertyCannotWritten() throws Throwable { .isThrownBy(() -> new PropertyAccessingMethodInterceptor(new Source()).invoke(invocation)); } + @Test // GH-3127 + void detectsKotlinPropertiesWithLeadingIsOnTargetType() throws Throwable { + + var source = new WithIsNamedProperty(true); + when(invocation.getMethod()).thenReturn(WithIsNamedPropertyProjection.class.getMethod("isValid")); + + assertThat(new PropertyAccessingMethodInterceptor(source).invoke(invocation)).isEqualTo(true); + } + static class Source { String firstname; diff --git a/src/test/kotlin/org/springframework/data/projection/WithIsNamedProperty.kt b/src/test/kotlin/org/springframework/data/projection/WithIsNamedProperty.kt new file mode 100644 index 0000000000..fc9e236eec --- /dev/null +++ b/src/test/kotlin/org/springframework/data/projection/WithIsNamedProperty.kt @@ -0,0 +1,23 @@ +/* + * 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.projection + +interface WithIsNamedPropertyProjection { + val isValid: Boolean +} + +class WithIsNamedProperty(val isValid : Boolean)