Browse Source

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
pull/36549/head
Sam Brannen 5 days ago
parent
commit
5e975f51dd
  1. 7
      spring-core/src/main/java/org/springframework/core/ResolvableType.java
  2. 28
      spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java

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

@ -1152,7 +1152,6 @@ public class ResolvableType implements Serializable {
* @see #forClassWithGenerics(Class, ResolvableType...) * @see #forClassWithGenerics(Class, ResolvableType...)
*/ */
public static ResolvableType forClassWithGenerics(Class<?> clazz, Class<?>... generics) { 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"); Assert.notNull(generics, "Generics array must not be null");
ResolvableType[] resolvableGenerics = new ResolvableType[generics.length]; ResolvableType[] resolvableGenerics = new ResolvableType[generics.length];
for (int i = 0; i < generics.length; i++) { for (int i = 0; i < generics.length; i++) {
@ -1289,7 +1288,6 @@ public class ResolvableType implements Serializable {
* @see #forConstructorParameter(Constructor, int, Class) * @see #forConstructorParameter(Constructor, int, Class)
*/ */
public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex) { public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex) {
Assert.notNull(constructor, "Constructor must not be null");
return forMethodParameter(new MethodParameter(constructor, parameterIndex)); return forMethodParameter(new MethodParameter(constructor, parameterIndex));
} }
@ -1307,7 +1305,6 @@ public class ResolvableType implements Serializable {
public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex, public static ResolvableType forConstructorParameter(Constructor<?> constructor, int parameterIndex,
Class<?> implementationClass) { Class<?> implementationClass) {
Assert.notNull(constructor, "Constructor must not be null");
MethodParameter methodParameter = new MethodParameter(constructor, parameterIndex, implementationClass); MethodParameter methodParameter = new MethodParameter(constructor, parameterIndex, implementationClass);
return forMethodParameter(methodParameter); return forMethodParameter(methodParameter);
} }
@ -1319,7 +1316,6 @@ public class ResolvableType implements Serializable {
* @see #forMethodReturnType(Method, Class) * @see #forMethodReturnType(Method, Class)
*/ */
public static ResolvableType forMethodReturnType(Method method) { public static ResolvableType forMethodReturnType(Method method) {
Assert.notNull(method, "Method must not be null");
return forMethodParameter(new MethodParameter(method, -1)); return forMethodParameter(new MethodParameter(method, -1));
} }
@ -1333,7 +1329,6 @@ public class ResolvableType implements Serializable {
* @see #forMethodReturnType(Method) * @see #forMethodReturnType(Method)
*/ */
public static ResolvableType forMethodReturnType(Method method, Class<?> implementationClass) { public static ResolvableType forMethodReturnType(Method method, Class<?> implementationClass) {
Assert.notNull(method, "Method must not be null");
MethodParameter methodParameter = new MethodParameter(method, -1, implementationClass); MethodParameter methodParameter = new MethodParameter(method, -1, implementationClass);
return forMethodParameter(methodParameter); return forMethodParameter(methodParameter);
} }
@ -1347,7 +1342,6 @@ public class ResolvableType implements Serializable {
* @see #forMethodParameter(MethodParameter) * @see #forMethodParameter(MethodParameter)
*/ */
public static ResolvableType forMethodParameter(Method method, int parameterIndex) { public static ResolvableType forMethodParameter(Method method, int parameterIndex) {
Assert.notNull(method, "Method must not be null");
return forMethodParameter(new MethodParameter(method, parameterIndex)); return forMethodParameter(new MethodParameter(method, parameterIndex));
} }
@ -1363,7 +1357,6 @@ public class ResolvableType implements Serializable {
* @see #forMethodParameter(MethodParameter) * @see #forMethodParameter(MethodParameter)
*/ */
public static ResolvableType forMethodParameter(Method method, int parameterIndex, Class<?> implementationClass) { 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); MethodParameter methodParameter = new MethodParameter(method, parameterIndex, implementationClass);
return forMethodParameter(methodParameter); return forMethodParameter(methodParameter);
} }

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

@ -221,6 +221,13 @@ class ResolvableTypeTests {
.withMessage("Constructor must not be null"); .withMessage("Constructor must not be null");
} }
@Test
void forConstructorParameterWithImplementationClassMustNotBeNull() {
assertThatIllegalArgumentException()
.isThrownBy(() -> ResolvableType.forConstructorParameter(null, 0, TypedConstructors.class))
.withMessage("Executable must not be null");
}
@Test @Test
void forMethodParameterByIndex() throws Exception { void forMethodParameterByIndex() throws Exception {
Method method = Methods.class.getMethod("charSequenceParameter", List.class); Method method = Methods.class.getMethod("charSequenceParameter", List.class);
@ -235,6 +242,13 @@ class ResolvableTypeTests {
.withMessage("Method must not be null"); .withMessage("Method must not be null");
} }
@Test
void forMethodParameterByIndexWithImplementationClassMustNotBeNull() {
assertThatIllegalArgumentException()
.isThrownBy(() -> ResolvableType.forMethodParameter(null, 0, TypedMethods.class))
.withMessage("Executable must not be null");
}
@Test @Test
void forMethodParameter() throws Exception { void forMethodParameter() throws Exception {
Method method = Methods.class.getMethod("charSequenceParameter", List.class); Method method = Methods.class.getMethod("charSequenceParameter", List.class);
@ -302,6 +316,13 @@ class ResolvableTypeTests {
.withMessage("Method must not be null"); .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 @Test // gh-27748
void genericMatchesReturnType() throws Exception { void genericMatchesReturnType() throws Exception {
Method method = SomeRepository.class.getMethod("someMethod", Class.class, Class.class, Class.class); 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<java.lang.Integer, java.util.List<java.lang.String>>"); assertThat(type.asMap().toString()).isEqualTo("java.util.Map<java.lang.Integer, java.util.List<java.lang.String>>");
} }
@Test
void forClassWithGenericsClassMustNotBeNull() {
assertThatIllegalArgumentException()
.isThrownBy(() -> ResolvableType.forClassWithGenerics(null, String.class))
.withMessage("Class must not be null");
}
@Test @Test
void forClassWithMismatchedGenerics() { void forClassWithMismatchedGenerics() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()

Loading…
Cancel
Save