ResolvableType.forInstance returns NONE for null instance

Closes gh-28776
This commit is contained in:
Juergen Hoeller
2022-07-13 11:10:35 +02:00
parent de1b938e2e
commit a3e46a2db7
2 changed files with 10 additions and 13 deletions

View File

@@ -221,9 +221,8 @@ public class ResolvableType implements Serializable {
/** /**
* Return the underlying source of the resolvable type. Will return a {@link Field}, * Return the underlying source of the resolvable type. Will return a {@link Field},
* {@link MethodParameter} or {@link Type} depending on how the {@link ResolvableType} * {@link MethodParameter} or {@link Type} depending on how the {@link ResolvableType}
* was constructed. With the exception of the {@link #NONE} constant, this method will * was constructed. This method is primarily to provide access to additional type
* never return {@code null}. This method is primarily to provide access to additional * information or meta-data that alternative JVM languages may provide.
* type information or meta-data that alternative JVM languages may provide.
*/ */
public Object getSource() { public Object getSource() {
Object source = (this.typeProvider != null ? this.typeProvider.getSource() : null); Object source = (this.typeProvider != null ? this.typeProvider.getSource() : null);
@@ -1103,20 +1102,20 @@ public class ResolvableType implements Serializable {
* convey generic information but if it implements {@link ResolvableTypeProvider} a * convey generic information but if it implements {@link ResolvableTypeProvider} a
* more precise {@link ResolvableType} can be used than the simple one based on * more precise {@link ResolvableType} can be used than the simple one based on
* the {@link #forClass(Class) Class instance}. * the {@link #forClass(Class) Class instance}.
* @param instance the instance * @param instance the instance (possibly {@code null})
* @return a {@link ResolvableType} for the specified instance * @return a {@link ResolvableType} for the specified instance,
* or {@code NONE} for {@code null}
* @since 4.2 * @since 4.2
* @see ResolvableTypeProvider * @see ResolvableTypeProvider
*/ */
public static ResolvableType forInstance(Object instance) { public static ResolvableType forInstance(@Nullable Object instance) {
Assert.notNull(instance, "Instance must not be null");
if (instance instanceof ResolvableTypeProvider) { if (instance instanceof ResolvableTypeProvider) {
ResolvableType type = ((ResolvableTypeProvider) instance).getResolvableType(); ResolvableType type = ((ResolvableTypeProvider) instance).getResolvableType();
if (type != null) { if (type != null) {
return type; return type;
} }
} }
return ResolvableType.forClass(instance.getClass()); return (instance != null ? forClass(instance.getClass()) : NONE);
} }
/** /**

View File

@@ -146,11 +146,9 @@ class ResolvableTypeTests {
assertThat(typeVariable.isAssignableFrom(raw)).isTrue(); assertThat(typeVariable.isAssignableFrom(raw)).isTrue();
} }
@Test @Test // gh-28776
void forInstanceMustNotBeNull() throws Exception { void forInstanceNull() throws Exception {
assertThatIllegalArgumentException() assertThat(ResolvableType.forInstance(null)).isEqualTo(ResolvableType.NONE);
.isThrownBy(() -> ResolvableType.forInstance(null))
.withMessage("Instance must not be null");
} }
@Test @Test