Skip to content

Commit a3e46a2

Browse files
committed
ResolvableType.forInstance returns NONE for null instance
Closes gh-28776
1 parent de1b938 commit a3e46a2

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,8 @@ public Class<?> getRawClass() {
221221
/**
222222
* Return the underlying source of the resolvable type. Will return a {@link Field},
223223
* {@link MethodParameter} or {@link Type} depending on how the {@link ResolvableType}
224-
* was constructed. With the exception of the {@link #NONE} constant, this method will
225-
* never return {@code null}. This method is primarily to provide access to additional
226-
* type information or meta-data that alternative JVM languages may provide.
224+
* was constructed. This method is primarily to provide access to additional type
225+
* information or meta-data that alternative JVM languages may provide.
227226
*/
228227
public Object getSource() {
229228
Object source = (this.typeProvider != null ? this.typeProvider.getSource() : null);
@@ -1103,20 +1102,20 @@ public static ResolvableType forClassWithGenerics(Class<?> clazz, ResolvableType
11031102
* convey generic information but if it implements {@link ResolvableTypeProvider} a
11041103
* more precise {@link ResolvableType} can be used than the simple one based on
11051104
* the {@link #forClass(Class) Class instance}.
1106-
* @param instance the instance
1107-
* @return a {@link ResolvableType} for the specified instance
1105+
* @param instance the instance (possibly {@code null})
1106+
* @return a {@link ResolvableType} for the specified instance,
1107+
* or {@code NONE} for {@code null}
11081108
* @since 4.2
11091109
* @see ResolvableTypeProvider
11101110
*/
1111-
public static ResolvableType forInstance(Object instance) {
1112-
Assert.notNull(instance, "Instance must not be null");
1111+
public static ResolvableType forInstance(@Nullable Object instance) {
11131112
if (instance instanceof ResolvableTypeProvider) {
11141113
ResolvableType type = ((ResolvableTypeProvider) instance).getResolvableType();
11151114
if (type != null) {
11161115
return type;
11171116
}
11181117
}
1119-
return ResolvableType.forClass(instance.getClass());
1118+
return (instance != null ? forClass(instance.getClass()) : NONE);
11201119
}
11211120

11221121
/**

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,9 @@ void forRawClassAssignableFromTypeVariable() throws Exception {
146146
assertThat(typeVariable.isAssignableFrom(raw)).isTrue();
147147
}
148148

149-
@Test
150-
void forInstanceMustNotBeNull() throws Exception {
151-
assertThatIllegalArgumentException()
152-
.isThrownBy(() -> ResolvableType.forInstance(null))
153-
.withMessage("Instance must not be null");
149+
@Test // gh-28776
150+
void forInstanceNull() throws Exception {
151+
assertThat(ResolvableType.forInstance(null)).isEqualTo(ResolvableType.NONE);
154152
}
155153

156154
@Test

0 commit comments

Comments
 (0)