From 5e975f51dd6ff2936b87ec2ecd1facecd75aa287 Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Thu, 26 Mar 2026 15:46:02 +0100 Subject: [PATCH] Remove redundant Assert.notNull() checks in ResolvableType Since equivalent Assert.notNull() checks are already performed by subsequent code (constructors and factory methods), there is no need to perform the exact same assertion twice in such use cases. Closes gh-36544 --- .../springframework/core/ResolvableType.java | 7 ----- .../core/ResolvableTypeTests.java | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/ResolvableType.java b/spring-core/src/main/java/org/springframework/core/ResolvableType.java index af01421dc27..e053cd3998a 100644 --- a/spring-core/src/main/java/org/springframework/core/ResolvableType.java +++ b/spring-core/src/main/java/org/springframework/core/ResolvableType.java @@ -1152,7 +1152,6 @@ public class ResolvableType implements Serializable { * @see #forClassWithGenerics(Class, ResolvableType...) */ public static ResolvableType forClassWithGenerics(Class clazz, Class... generics) { - Assert.notNull(clazz, "Class must not be null"); Assert.notNull(generics, "Generics array must not be null"); ResolvableType[] resolvableGenerics = new ResolvableType[generics.length]; for (int i = 0; i < generics.length; i++) { @@ -1289,7 +1288,6 @@ public class ResolvableType implements Serializable { * @see #forConstructorParameter(Constructor, int, Class) */ public static ResolvableType forConstructorParameter(Constructor constructor, int parameterIndex) { - Assert.notNull(constructor, "Constructor must not be null"); return forMethodParameter(new MethodParameter(constructor, parameterIndex)); } @@ -1307,7 +1305,6 @@ public class ResolvableType implements Serializable { public static ResolvableType forConstructorParameter(Constructor constructor, int parameterIndex, Class implementationClass) { - Assert.notNull(constructor, "Constructor must not be null"); MethodParameter methodParameter = new MethodParameter(constructor, parameterIndex, implementationClass); return forMethodParameter(methodParameter); } @@ -1319,7 +1316,6 @@ public class ResolvableType implements Serializable { * @see #forMethodReturnType(Method, Class) */ public static ResolvableType forMethodReturnType(Method method) { - Assert.notNull(method, "Method must not be null"); return forMethodParameter(new MethodParameter(method, -1)); } @@ -1333,7 +1329,6 @@ public class ResolvableType implements Serializable { * @see #forMethodReturnType(Method) */ public static ResolvableType forMethodReturnType(Method method, Class implementationClass) { - Assert.notNull(method, "Method must not be null"); MethodParameter methodParameter = new MethodParameter(method, -1, implementationClass); return forMethodParameter(methodParameter); } @@ -1347,7 +1342,6 @@ public class ResolvableType implements Serializable { * @see #forMethodParameter(MethodParameter) */ public static ResolvableType forMethodParameter(Method method, int parameterIndex) { - Assert.notNull(method, "Method must not be null"); return forMethodParameter(new MethodParameter(method, parameterIndex)); } @@ -1363,7 +1357,6 @@ public class ResolvableType implements Serializable { * @see #forMethodParameter(MethodParameter) */ public static ResolvableType forMethodParameter(Method method, int parameterIndex, Class implementationClass) { - Assert.notNull(method, "Method must not be null"); MethodParameter methodParameter = new MethodParameter(method, parameterIndex, implementationClass); return forMethodParameter(methodParameter); } diff --git a/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java b/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java index 8b67d70d783..40d69967b3a 100644 --- a/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java +++ b/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java @@ -221,6 +221,13 @@ class ResolvableTypeTests { .withMessage("Constructor must not be null"); } + @Test + void forConstructorParameterWithImplementationClassMustNotBeNull() { + assertThatIllegalArgumentException() + .isThrownBy(() -> ResolvableType.forConstructorParameter(null, 0, TypedConstructors.class)) + .withMessage("Executable must not be null"); + } + @Test void forMethodParameterByIndex() throws Exception { Method method = Methods.class.getMethod("charSequenceParameter", List.class); @@ -235,6 +242,13 @@ class ResolvableTypeTests { .withMessage("Method must not be null"); } + @Test + void forMethodParameterByIndexWithImplementationClassMustNotBeNull() { + assertThatIllegalArgumentException() + .isThrownBy(() -> ResolvableType.forMethodParameter(null, 0, TypedMethods.class)) + .withMessage("Executable must not be null"); + } + @Test void forMethodParameter() throws Exception { Method method = Methods.class.getMethod("charSequenceParameter", List.class); @@ -302,6 +316,13 @@ class ResolvableTypeTests { .withMessage("Method must not be null"); } + @Test + void forMethodReturnWithImplementationClassMustNotBeNull() { + assertThatIllegalArgumentException() + .isThrownBy(() -> ResolvableType.forMethodReturnType(null, TypedMethods.class)) + .withMessage("Executable must not be null"); + } + @Test // gh-27748 void genericMatchesReturnType() throws Exception { Method method = SomeRepository.class.getMethod("someMethod", Class.class, Class.class, Class.class); @@ -1307,6 +1328,13 @@ class ResolvableTypeTests { assertThat(type.asMap().toString()).isEqualTo("java.util.Map>"); } + @Test + void forClassWithGenericsClassMustNotBeNull() { + assertThatIllegalArgumentException() + .isThrownBy(() -> ResolvableType.forClassWithGenerics(null, String.class)) + .withMessage("Class must not be null"); + } + @Test void forClassWithMismatchedGenerics() { assertThatIllegalArgumentException()