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
This commit is contained in:
Sam Brannen 2022-01-09 16:55:17 +01:00
parent e41d865193
commit 8087eb69bf
2 changed files with 5 additions and 4 deletions

View File

@ -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++) {

View File

@ -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<K,V>");
}
@Test