Skip to content

Commit 9d564e2

Browse files
committed
Polishing.
Refine deprecations. See #3208
1 parent d040f6f commit 9d564e2

File tree

7 files changed

+26
-22
lines changed

7 files changed

+26
-22
lines changed

src/main/java/org/springframework/data/querydsl/QSort.java

-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ public QSort(OrderSpecifier<?>... orderSpecifiers) {
5757
*
5858
* @param orderSpecifiers must not be {@literal null}.
5959
*/
60-
@SuppressWarnings("deprecation")
6160
public QSort(List<OrderSpecifier<?>> orderSpecifiers) {
6261

6362
super(toOrders(orderSpecifiers));

src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java

+9-6
Original file line numberDiff line numberDiff line change
@@ -602,13 +602,16 @@ protected final <R> R getTargetRepositoryViaReflection(RepositoryInformation inf
602602
@SuppressWarnings("unchecked")
603603
protected final <R> R instantiateClass(Class<?> baseClass, Object... constructorArguments) {
604604

605-
Optional<Constructor<?>> constructor = ReflectionUtils.findConstructor(baseClass, constructorArguments);
605+
Constructor<?> constructor = ReflectionUtils.findConstructor(baseClass, constructorArguments);
606606

607-
return constructor.map(it -> (R) BeanUtils.instantiateClass(it, constructorArguments))
608-
.orElseThrow(() -> new IllegalStateException(String.format(
609-
"No suitable constructor found on %s to match the given arguments: %s. Make sure you implement a constructor taking these",
610-
baseClass, Arrays.stream(constructorArguments).map(Object::getClass).map(ClassUtils::getQualifiedName)
611-
.collect(Collectors.joining(", ")))));
607+
if (constructor == null) {
608+
throw new IllegalStateException(String.format(
609+
"No suitable constructor found on %s to match the given arguments: %s. Make sure you implement a constructor taking these",
610+
baseClass, Arrays.stream(constructorArguments).map(Object::getClass).map(ClassUtils::getQualifiedName)
611+
.collect(Collectors.joining(", "))));
612+
}
613+
614+
return (R) BeanUtils.instantiateClass(constructor, constructorArguments);
612615
}
613616

614617
private ApplicationStartup getStartup() {

src/main/java/org/springframework/data/repository/query/Parameters.java

-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ protected Parameters(ParametersSource parametersSource,
7878
Assert.notNull(parametersSource, "ParametersSource must not be null");
7979
Assert.notNull(parameterFactory, "Parameter factory must not be null");
8080

81-
// Factory nullability not enforced yet to support falling back to the deprecated
82-
8381
Method method = parametersSource.getMethod();
8482
int parameterCount = method.getParameterCount();
8583

src/main/java/org/springframework/data/util/ReflectionUtils.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
import java.util.Arrays;
2424
import java.util.Collections;
2525
import java.util.List;
26-
import java.util.Optional;
2726
import java.util.function.Predicate;
2827
import java.util.stream.Collectors;
2928
import java.util.stream.Stream;
3029

30+
import org.jetbrains.annotations.NotNull;
31+
3132
import org.springframework.core.KotlinDetector;
3233
import org.springframework.core.MethodParameter;
3334
import org.springframework.core.ResolvableType;
@@ -282,17 +283,22 @@ public static void setField(Field field, Object target, @Nullable Object value)
282283
* @param type must not be {@literal null}.
283284
* @param constructorArguments must not be {@literal null}.
284285
* @return a {@link Constructor} that is compatible with the given arguments.
285-
* @deprecated since 3.5, return type will change to nullable instead of Optional.
286286
*/
287-
@Deprecated
288-
public static Optional<Constructor<?>> findConstructor(Class<?> type, Object... constructorArguments) {
287+
@Nullable
288+
@SuppressWarnings("unchecked")
289+
public static <T> Constructor<T> findConstructor(Class<T> type, Object... constructorArguments) {
289290

290291
Assert.notNull(type, "Target type must not be null");
291292
Assert.notNull(constructorArguments, "Constructor arguments must not be null");
292293

293-
return Arrays.stream(type.getDeclaredConstructors())//
294-
.filter(constructor -> argumentsMatch(constructor.getParameterTypes(), constructorArguments))//
295-
.findFirst();
294+
for (@NotNull
295+
Constructor<?> declaredConstructor : type.getDeclaredConstructors()) {
296+
if (argumentsMatch(declaredConstructor.getParameterTypes(), constructorArguments)) {
297+
return (Constructor<T>) declaredConstructor;
298+
}
299+
}
300+
301+
return null;
296302
}
297303

298304
/**

src/main/java/org/springframework/data/util/TypeInformation.java

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
* @author Johannes Englmeier
4040
* @author Christoph Strobl
4141
*/
42-
@SuppressWarnings({ "deprecation", "rawtypes" })
4342
public interface TypeInformation<S> {
4443

4544
TypeInformation<Collection> COLLECTION = ClassTypeInformation.COLLECTION;

src/test/java/org/springframework/data/repository/util/ReactiveWrapperConvertersUnitTests.java

-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ void shouldMapRxJava3Flowable() {
133133
}
134134

135135
@Test // DATACMNS-1763
136-
@SuppressWarnings("deprecation")
137136
void shouldMapKotlinFlow() {
138137

139138
var flow = FlowKt.asFlow(new String[] { "foo" });

src/test/java/org/springframework/data/util/ReflectionUtilsUnitTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,22 @@ void setsNonPublicField() {
9696

9797
@Test // DATACMNS-542
9898
void detectsConstructorForCompleteMatch() throws Exception {
99-
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test")).hasValue(constructor);
99+
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test")).isEqualTo(constructor);
100100
}
101101

102102
@Test // DATACMNS-542
103103
void detectsConstructorForMatchWithNulls() throws Exception {
104-
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, null)).hasValue(constructor);
104+
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, null)).isEqualTo(constructor);
105105
}
106106

107107
@Test // DATACMNS-542
108108
void rejectsConstructorIfNumberOfArgumentsDontMatch() throws Exception {
109-
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test", "test")).isNotPresent();
109+
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, 2, "test", "test")).isNull();
110110
}
111111

112112
@Test // DATACMNS-542
113113
void rejectsConstructorForNullForPrimitiveArgument() throws Exception {
114-
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, null, "test")).isNotPresent();
114+
assertThat(ReflectionUtils.findConstructor(ConstructorDetection.class, null, "test")).isNull();
115115
}
116116

117117
@Test // DATACMNS-1154

0 commit comments

Comments
 (0)