Skip to content

Commit f7be796

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>
1 parent 1e843fd commit f7be796

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ 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+
public @Nullable ClassLoader getClassLoader() {
69+
return this.classLoader;
70+
}
71+
72+
6273
/**
6374
* Read from the supplied {@code InputStream} and deserialize the contents
6475
* 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)