From 8b0673fa081b604e51e478c821089d255c7b12a8 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 14 Jan 2025 11:53:15 +0100 Subject: [PATCH] Polishing. Refine deprecations. See #3208 --- .../springframework/data/querydsl/QSort.java | 1 - .../support/RepositoryFactorySupport.java | 15 ++++++++------ .../data/repository/query/Parameters.java | 2 -- .../data/util/ReflectionUtils.java | 20 ++++++++++++------- .../data/util/TypeInformation.java | 1 - .../ReactiveWrapperConvertersUnitTests.java | 1 - .../data/util/ReflectionUtilsUnitTests.java | 8 ++++---- 7 files changed, 26 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/springframework/data/querydsl/QSort.java b/src/main/java/org/springframework/data/querydsl/QSort.java index d04368802..70b47ddbd 100644 --- a/src/main/java/org/springframework/data/querydsl/QSort.java +++ b/src/main/java/org/springframework/data/querydsl/QSort.java @@ -57,7 +57,6 @@ public class QSort extends Sort implements Serializable { * * @param orderSpecifiers must not be {@literal null}. */ - @SuppressWarnings("deprecation") public QSort(List> orderSpecifiers) { super(toOrders(orderSpecifiers)); diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java index c986be826..fa235537d 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java @@ -602,13 +602,16 @@ public abstract class RepositoryFactorySupport @SuppressWarnings("unchecked") protected final R instantiateClass(Class baseClass, Object... constructorArguments) { - Optional> constructor = ReflectionUtils.findConstructor(baseClass, constructorArguments); + Constructor constructor = ReflectionUtils.findConstructor(baseClass, constructorArguments); - return constructor.map(it -> (R) BeanUtils.instantiateClass(it, constructorArguments)) - .orElseThrow(() -> new IllegalStateException(String.format( - "No suitable constructor found on %s to match the given arguments: %s. Make sure you implement a constructor taking these", - baseClass, Arrays.stream(constructorArguments).map(Object::getClass).map(ClassUtils::getQualifiedName) - .collect(Collectors.joining(", "))))); + if (constructor == null) { + throw new IllegalStateException(String.format( + "No suitable constructor found on %s to match the given arguments: %s. Make sure you implement a constructor taking these", + baseClass, Arrays.stream(constructorArguments).map(Object::getClass).map(ClassUtils::getQualifiedName) + .collect(Collectors.joining(", ")))); + } + + return (R) BeanUtils.instantiateClass(constructor, constructorArguments); } private ApplicationStartup getStartup() { diff --git a/src/main/java/org/springframework/data/repository/query/Parameters.java b/src/main/java/org/springframework/data/repository/query/Parameters.java index d6a6a9c49..f2c35956a 100644 --- a/src/main/java/org/springframework/data/repository/query/Parameters.java +++ b/src/main/java/org/springframework/data/repository/query/Parameters.java @@ -77,8 +77,6 @@ public abstract class Parameters, T extends Parameter Assert.notNull(parametersSource, "ParametersSource must not be null"); Assert.notNull(parameterFactory, "Parameter factory must not be null"); - // Factory nullability not enforced yet to support falling back to the deprecated - Method method = parametersSource.getMethod(); int parameterCount = method.getParameterCount(); diff --git a/src/main/java/org/springframework/data/util/ReflectionUtils.java b/src/main/java/org/springframework/data/util/ReflectionUtils.java index 4e2c4330c..b36a6a5ac 100644 --- a/src/main/java/org/springframework/data/util/ReflectionUtils.java +++ b/src/main/java/org/springframework/data/util/ReflectionUtils.java @@ -23,11 +23,12 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Optional; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; +import org.jetbrains.annotations.NotNull; + import org.springframework.core.KotlinDetector; import org.springframework.core.MethodParameter; import org.springframework.core.ResolvableType; @@ -282,17 +283,22 @@ public final class ReflectionUtils { * @param type must not be {@literal null}. * @param constructorArguments must not be {@literal null}. * @return a {@link Constructor} that is compatible with the given arguments. - * @deprecated since 3.5, return type will change to nullable instead of Optional. */ - @Deprecated - public static Optional> findConstructor(Class type, Object... constructorArguments) { + @Nullable + @SuppressWarnings("unchecked") + public static Constructor findConstructor(Class type, Object... constructorArguments) { Assert.notNull(type, "Target type must not be null"); Assert.notNull(constructorArguments, "Constructor arguments must not be null"); - return Arrays.stream(type.getDeclaredConstructors())// - .filter(constructor -> argumentsMatch(constructor.getParameterTypes(), constructorArguments))// - .findFirst(); + for (@NotNull + Constructor declaredConstructor : type.getDeclaredConstructors()) { + if (argumentsMatch(declaredConstructor.getParameterTypes(), constructorArguments)) { + return (Constructor) declaredConstructor; + } + } + + return null; } /** diff --git a/src/main/java/org/springframework/data/util/TypeInformation.java b/src/main/java/org/springframework/data/util/TypeInformation.java index 6a0cb892b..2005d84fb 100644 --- a/src/main/java/org/springframework/data/util/TypeInformation.java +++ b/src/main/java/org/springframework/data/util/TypeInformation.java @@ -39,7 +39,6 @@ import org.springframework.util.Assert; * @author Johannes Englmeier * @author Christoph Strobl */ -@SuppressWarnings({ "deprecation", "rawtypes" }) public interface TypeInformation { TypeInformation COLLECTION = ClassTypeInformation.COLLECTION; diff --git a/src/test/java/org/springframework/data/repository/util/ReactiveWrapperConvertersUnitTests.java b/src/test/java/org/springframework/data/repository/util/ReactiveWrapperConvertersUnitTests.java index 0af0cfe94..e74630437 100644 --- a/src/test/java/org/springframework/data/repository/util/ReactiveWrapperConvertersUnitTests.java +++ b/src/test/java/org/springframework/data/repository/util/ReactiveWrapperConvertersUnitTests.java @@ -133,7 +133,6 @@ class ReactiveWrapperConvertersUnitTests { } @Test // DATACMNS-1763 - @SuppressWarnings("deprecation") void shouldMapKotlinFlow() { var flow = FlowKt.asFlow(new String[] { "foo" }); diff --git a/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java b/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java index 8dc9c1f9c..b5f507312 100755 --- a/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java @@ -96,22 +96,22 @@ class ReflectionUtilsUnitTests { @Test // DATACMNS-542 void detectsConstructorForCompleteMatch() throws Exception { - assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test")).hasValue(constructor); + assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test")).isEqualTo(constructor); } @Test // DATACMNS-542 void detectsConstructorForMatchWithNulls() throws Exception { - assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, null)).hasValue(constructor); + assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, null)).isEqualTo(constructor); } @Test // DATACMNS-542 void rejectsConstructorIfNumberOfArgumentsDontMatch() throws Exception { - assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test", "test")).isNotPresent(); + assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test", "test")).isNull(); } @Test // DATACMNS-542 void rejectsConstructorForNullForPrimitiveArgument() throws Exception { - assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, null, "test")).isNotPresent(); + assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, null, "test")).isNull(); } @Test // DATACMNS-1154