Skip to content

Commit de18f74

Browse files
seonwooj0810sbrannen
authored andcommitted
Expose ClassLoader from DefaultDeserializer
Add a public accessor for the ClassLoader configured on a DefaultDeserializer instance so that callers no longer need to read the private field via reflection in order to forward it to a ConfigurableObjectInputStream subclass. See gh-36827 Closes gh-36833 Signed-off-by: seonwoo_jung <laborlawseon@kap.kr> (cherry picked from commit f7be796)
1 parent 89ecaea commit de18f74

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

spring-core/src/main/java/org/springframework/core/serializer/DefaultDeserializer.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ public DefaultDeserializer(@Nullable ClassLoader classLoader) {
5959
}
6060

6161

62+
/**
63+
* Return the {@link ClassLoader} to use for deserialization, or {@code null}
64+
* to use the "latest user-defined ClassLoader" of {@link ObjectInputStream}.
65+
* @since 7.1
66+
* @see ConfigurableObjectInputStream#ConfigurableObjectInputStream(InputStream, ClassLoader)
67+
*/
68+
@Nullable
69+
public ClassLoader getClassLoader() {
70+
return this.classLoader;
71+
}
72+
73+
6274
/**
6375
* Read from the supplied {@code InputStream} and deserialize the contents
6476
* into an object.

spring-core/src/test/java/org/springframework/core/serializer/SerializerTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ public String deserialize(InputStream inputStream) {
7878
assertThat(deserializer.expectedInputStream).isNotNull();
7979
}
8080

81+
@Test
82+
void defaultDeserializerExposesNullClassLoaderByDefault() {
83+
assertThat(new DefaultDeserializer().getClassLoader()).isNull();
84+
}
85+
86+
@Test
87+
void defaultDeserializerExposesConfiguredClassLoader() {
88+
ClassLoader classLoader = getClass().getClassLoader();
89+
assertThat(new DefaultDeserializer(classLoader).getClassLoader()).isSameAs(classLoader);
90+
}
91+
8192
@Test
8293
void serializationDelegateWithExplicitSerializerAndDeserializer() throws IOException {
8394
SerializationDelegate delegate = new SerializationDelegate(new DefaultSerializer(), new DefaultDeserializer());

0 commit comments

Comments
 (0)