Skip to content

Commit 8087eb6

Browse files
committed
Improve error message in ResolvableType.forClassWithGenerics()
Prior to this commit, the error message generated for a mismatched number of generics did not include the information about the class in question. This commit improves the error message by providing more context, specifically the result of invoking toGenericString() on the class. For example, instead of throwing an IllegalArgumentException with the error message "Mismatched number of generics specified", the error message would now be "Mismatched number of generics specified for public abstract interface java.util.Map<K,V>". Closes gh-27847
1 parent e41d865 commit 8087eb6

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ public static ResolvableType forClassWithGenerics(Class<?> clazz, ResolvableType
10851085
Assert.notNull(clazz, "Class must not be null");
10861086
Assert.notNull(generics, "Generics array must not be null");
10871087
TypeVariable<?>[] variables = clazz.getTypeParameters();
1088-
Assert.isTrue(variables.length == generics.length, "Mismatched number of generics specified");
1088+
Assert.isTrue(variables.length == generics.length, () -> "Mismatched number of generics specified for " + clazz.toGenericString());
10891089

10901090
Type[] arguments = new Type[generics.length];
10911091
for (int i = 0; i < generics.length; i++) {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1221,9 +1221,10 @@ void classWithGenericsAs() throws Exception {
12211221

12221222
@Test
12231223
void forClassWithMismatchedGenerics() throws Exception {
1224-
assertThatIllegalArgumentException().isThrownBy(() ->
1225-
ResolvableType.forClassWithGenerics(Map.class, Integer.class))
1226-
.withMessageContaining("Mismatched number of generics specified");
1224+
assertThatIllegalArgumentException()
1225+
.isThrownBy(() -> ResolvableType.forClassWithGenerics(Map.class, Integer.class))
1226+
.withMessageContaining("Mismatched number of generics specified for")
1227+
.withMessageContaining("java.util.Map<K,V>");
12271228
}
12281229

12291230
@Test

0 commit comments

Comments
 (0)