Polishing.

Refine deprecations.

See #3208
This commit is contained in:
Mark Paluch
2025-01-14 11:53:15 +01:00
parent d6e2f8ab98
commit 8b0673fa08
7 changed files with 26 additions and 22 deletions

View File

@@ -57,7 +57,6 @@ public class QSort extends Sort implements Serializable {
*
* @param orderSpecifiers must not be {@literal null}.
*/
@SuppressWarnings("deprecation")
public QSort(List<OrderSpecifier<?>> orderSpecifiers) {
super(toOrders(orderSpecifiers));

View File

@@ -602,13 +602,16 @@ public abstract class RepositoryFactorySupport
@SuppressWarnings("unchecked")
protected final <R> R instantiateClass(Class<?> baseClass, Object... constructorArguments) {
Optional<Constructor<?>> 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() {

View File

@@ -77,8 +77,6 @@ public abstract class Parameters<S extends Parameters<S, T>, 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();

View File

@@ -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<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments) {
@Nullable
@SuppressWarnings("unchecked")
public static <T> Constructor<T> findConstructor(Class<T> 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<T>) declaredConstructor;
}
}
return null;
}
/**

View File

@@ -39,7 +39,6 @@ import org.springframework.util.Assert;
* @author Johannes Englmeier
* @author Christoph Strobl
*/
@SuppressWarnings({ "deprecation", "rawtypes" })
public interface TypeInformation<S> {
TypeInformation<Collection> COLLECTION = ClassTypeInformation.COLLECTION;

View File

@@ -133,7 +133,6 @@ class ReactiveWrapperConvertersUnitTests {
}
@Test // DATACMNS-1763
@SuppressWarnings("deprecation")
void shouldMapKotlinFlow() {
var flow = FlowKt.asFlow(new String[] { "foo" });

View File

@@ -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