From e5bef10d8547e7829e15008c50534f862e952cfb Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sat, 20 Jul 2019 13:16:07 +0100 Subject: [PATCH] Fix ResolvableType raw class isAssignable checks Fix `isAssignable` for `ResolvableType.forRawClass` so that it can be used with types backed by a `TypeVarible`. Prior to this commit the rawClass value was used, which wouldn't always work. Closes gh-23321 --- .../org/springframework/core/ResolvableType.java | 2 +- .../springframework/core/ResolvableTypeTests.java | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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 e633b0ecddbf..469e934f1314 100644 --- a/spring-core/src/main/java/org/springframework/core/ResolvableType.java +++ b/spring-core/src/main/java/org/springframework/core/ResolvableType.java @@ -1015,7 +1015,7 @@ public boolean isAssignableFrom(Class other) { } @Override public boolean isAssignableFrom(ResolvableType other) { - Class otherClass = other.getRawClass(); + Class otherClass = other.resolve(); return (otherClass != null && (clazz == null || ClassUtils.isAssignable(clazz, otherClass))); } }; 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 f44c393915bf..f4a4490da259 100644 --- a/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java +++ b/spring-core/src/test/java/org/springframework/core/ResolvableTypeTests.java @@ -135,6 +135,18 @@ public void forRawClassWithNull() throws Exception { assertTrue(type.isAssignableFrom(String.class)); } + @Test + public void forRawClassAssignableFromTypeVariable() { // gh-23321 + ResolvableType typeVariable = ResolvableType.forClass(ExtendsList.class).as(List.class).getGeneric(); + ResolvableType raw = ResolvableType.forRawClass(CharSequence.class); + assertThat(raw.resolve()).isEqualTo(CharSequence.class); + assertThat(typeVariable.resolve()).isEqualTo(CharSequence.class); + assertThat(raw.resolve().isAssignableFrom(typeVariable.resolve())).isTrue(); + assertThat(typeVariable.resolve().isAssignableFrom(raw.resolve())).isTrue(); + assertThat(raw.isAssignableFrom(typeVariable)).isTrue(); + assertThat(typeVariable.isAssignableFrom(raw)).isTrue(); + } + @Test public void forInstanceMustNotBeNull() { this.thrown.expect(IllegalArgumentException.class);