Skip to content

Commit 2ade122

Browse files
committed
Bypass serializable type wrapping if java.lang.Class not serializable
Issue: SPR-16992
1 parent 4ff1e3e commit 2ade122

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,26 @@
5656
* @author Juergen Hoeller
5757
* @since 4.0
5858
*/
59-
abstract class SerializableTypeWrapper {
59+
final class SerializableTypeWrapper {
6060

6161
private static final Class<?>[] SUPPORTED_SERIALIZABLE_TYPES = {
6262
GenericArrayType.class, ParameterizedType.class, TypeVariable.class, WildcardType.class};
6363

64+
/**
65+
* Let's test whether java.lang.Class itself is serializable...
66+
* Otherwise we can skip our serializable type wrapping to begin with.
67+
* This will be {@code true} on regular JVMs but {@code false} on GraalVM.
68+
* @since 5.1
69+
*/
70+
private static final boolean javaLangClassSerializable = Serializable.class.isAssignableFrom(Class.class);
71+
6472
static final ConcurrentReferenceHashMap<Type, Type> cache = new ConcurrentReferenceHashMap<>(256);
6573

6674

75+
private SerializableTypeWrapper() {
76+
}
77+
78+
6779
/**
6880
* Return a {@link Serializable} variant of {@link Field#getGenericType()}.
6981
*/
@@ -133,15 +145,17 @@ public static <T extends Type> T unwrap(T type) {
133145

134146
/**
135147
* Return a {@link Serializable} {@link Type} backed by a {@link TypeProvider} .
148+
* <p>If type artifacts are generally not serializable in the current runtime
149+
* environment, this delegate will simply return the original {@code Type} as-is.
136150
*/
137151
@Nullable
138152
static Type forTypeProvider(final TypeProvider provider) {
139-
Assert.notNull(provider, "Provider must not be null");
153+
Assert.notNull(provider, "TypeProvider must not be null");
140154
Type providedType = provider.getType();
141155
if (providedType == null) {
142156
return null;
143157
}
144-
if (providedType instanceof Serializable) {
158+
if (!javaLangClassSerializable || providedType instanceof Serializable) {
145159
return providedType;
146160
}
147161
Type cached = cache.get(providedType);

0 commit comments

Comments
 (0)