Skip to content

Commit 5e975f5

Browse files
committed
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
1 parent ccf0cae commit 5e975f5

2 files changed

Lines changed: 28 additions & 7 deletions

File tree

spring-core/src/main/java/org/springframework/core/ResolvableType.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,6 @@ public static ResolvableType forClass(Class<?> baseType, Class<?> implementation
11521152
* @see #forClassWithGenerics(Class, ResolvableType...)
11531153
*/
11541154
public static ResolvableType forClassWithGenerics(Class<?> clazz, Class<?>... generics) {
1155-
Assert.notNull(clazz, "Class must not be null");
11561155
Assert.notNull(generics, "Generics array must not be null");
11571156
ResolvableType[] resolvableGenerics = new ResolvableType[generics.length];
11581157
for (int i = 0; i < generics.length; i++) {
@@ -1289,7 +1288,6 @@ public static ResolvableType forField(Field field, int nestingLevel, @Nullable C
12891288
* @see #forConstructorParameter(Constructor, int, Class)
12901289
*/
12911290
public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex) {
1292-
Assert.notNull(constructor, "Constructor must not be null");
12931291
return forMethodParameter(new MethodParameter(constructor, parameterIndex));
12941292
}
12951293

@@ -1307,7 +1305,6 @@ public static ResolvableType forConstructorParameter(Constructor<?> constructor,
13071305
public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex,
13081306
Class<?> implementationClass) {
13091307

1310-
Assert.notNull(constructor, "Constructor must not be null");
13111308
MethodParameter methodParameter = new MethodParameter(constructor, parameterIndex, implementationClass);
13121309
return forMethodParameter(methodParameter);
13131310
}
@@ -1319,7 +1316,6 @@ public static ResolvableType forConstructorParameter(Constructor<?> constructor,
13191316
* @see #forMethodReturnType(Method, Class)
13201317
*/
13211318
public static ResolvableType forMethodReturnType(Method method) {
1322-
Assert.notNull(method, "Method must not be null");
13231319
return forMethodParameter(new MethodParameter(method, -1));
13241320
}
13251321

@@ -1333,7 +1329,6 @@ public static ResolvableType forMethodReturnType(Method method) {
13331329
* @see #forMethodReturnType(Method)
13341330
*/
13351331
public static ResolvableType forMethodReturnType(Method method, Class<?> implementationClass) {
1336-
Assert.notNull(method, "Method must not be null");
13371332
MethodParameter methodParameter = new MethodParameter(method, -1, implementationClass);
13381333
return forMethodParameter(methodParameter);
13391334
}
@@ -1347,7 +1342,6 @@ public static ResolvableType forMethodReturnType(Method method, Class<?> impleme
13471342
* @see #forMethodParameter(MethodParameter)
13481343
*/
13491344
public static ResolvableType forMethodParameter(Method method, int parameterIndex) {
1350-
Assert.notNull(method, "Method must not be null");
13511345
return forMethodParameter(new MethodParameter(method, parameterIndex));
13521346
}
13531347

@@ -1363,7 +1357,6 @@ public static ResolvableType forMethodParameter(Method method, int parameterInde
13631357
* @see #forMethodParameter(MethodParameter)
13641358
*/
13651359
public static ResolvableType forMethodParameter(Method method, int parameterIndex, Class<?> implementationClass) {
1366-
Assert.notNull(method, "Method must not be null");
13671360
MethodParameter methodParameter = new MethodParameter(method, parameterIndex, implementationClass);
13681361
return forMethodParameter(methodParameter);
13691362
}

spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,13 @@ void forConstructorParameterMustNotBeNull() {
221221
.withMessage("Constructor must not be null");
222222
}
223223

224+
@Test
225+
void forConstructorParameterWithImplementationClassMustNotBeNull() {
226+
assertThatIllegalArgumentException()
227+
.isThrownBy(() -> ResolvableType.forConstructorParameter(null, 0, TypedConstructors.class))
228+
.withMessage("Executable must not be null");
229+
}
230+
224231
@Test
225232
void forMethodParameterByIndex() throws Exception {
226233
Method method = Methods.class.getMethod("charSequenceParameter", List.class);
@@ -235,6 +242,13 @@ void forMethodParameterByIndexMustNotBeNull() {
235242
.withMessage("Method must not be null");
236243
}
237244

245+
@Test
246+
void forMethodParameterByIndexWithImplementationClassMustNotBeNull() {
247+
assertThatIllegalArgumentException()
248+
.isThrownBy(() -> ResolvableType.forMethodParameter(null, 0, TypedMethods.class))
249+
.withMessage("Executable must not be null");
250+
}
251+
238252
@Test
239253
void forMethodParameter() throws Exception {
240254
Method method = Methods.class.getMethod("charSequenceParameter", List.class);
@@ -302,6 +316,13 @@ void forMethodReturnMustNotBeNull() {
302316
.withMessage("Method must not be null");
303317
}
304318

319+
@Test
320+
void forMethodReturnWithImplementationClassMustNotBeNull() {
321+
assertThatIllegalArgumentException()
322+
.isThrownBy(() -> ResolvableType.forMethodReturnType(null, TypedMethods.class))
323+
.withMessage("Executable must not be null");
324+
}
325+
305326
@Test // gh-27748
306327
void genericMatchesReturnType() throws Exception {
307328
Method method = SomeRepository.class.getMethod("someMethod", Class.class, Class.class, Class.class);
@@ -1307,6 +1328,13 @@ void classWithGenericsAs() {
13071328
assertThat(type.asMap().toString()).isEqualTo("java.util.Map<java.lang.Integer, java.util.List<java.lang.String>>");
13081329
}
13091330

1331+
@Test
1332+
void forClassWithGenericsClassMustNotBeNull() {
1333+
assertThatIllegalArgumentException()
1334+
.isThrownBy(() -> ResolvableType.forClassWithGenerics(null, String.class))
1335+
.withMessage("Class must not be null");
1336+
}
1337+
13101338
@Test
13111339
void forClassWithMismatchedGenerics() {
13121340
assertThatIllegalArgumentException()

0 commit comments

Comments
 (0)