From 8087eb69bfc2782bd8c5b81c57d63c849b87ed8c Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Sun, 9 Jan 2022 16:55:17 +0100 Subject: [PATCH] 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". Closes gh-27847 --- .../main/java/org/springframework/core/ResolvableType.java | 2 +- .../java/org/springframework/core/ResolvableTypeTests.java | 7 ++++--- 2 files changed, 5 insertions(+), 4 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 d976d99d5b..6ba088cfd1 100644 --- a/spring-core/src/main/java/org/springframework/core/ResolvableType.java +++ b/spring-core/src/main/java/org/springframework/core/ResolvableType.java @@ -1085,7 +1085,7 @@ public class ResolvableType implements Serializable { Assert.notNull(clazz, "Class must not be null"); Assert.notNull(generics, "Generics array must not be null"); TypeVariable[] variables = clazz.getTypeParameters(); - Assert.isTrue(variables.length == generics.length, "Mismatched number of generics specified"); + Assert.isTrue(variables.length == generics.length, () -> "Mismatched number of generics specified for " + clazz.toGenericString()); Type[] arguments = new Type[generics.length]; for (int i = 0; i < generics.length; i++) { 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 c153c88386..71b17e0da0 100644 --- a/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java +++ b/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java @@ -1221,9 +1221,10 @@ class ResolvableTypeTests { @Test void forClassWithMismatchedGenerics() throws Exception { - assertThatIllegalArgumentException().isThrownBy(() -> - ResolvableType.forClassWithGenerics(Map.class, Integer.class)) - .withMessageContaining("Mismatched number of generics specified"); + assertThatIllegalArgumentException() + .isThrownBy(() -> ResolvableType.forClassWithGenerics(Map.class, Integer.class)) + .withMessageContaining("Mismatched number of generics specified for") + .withMessageContaining("java.util.Map"); } @Test