Skip to content

Commit 4783a0c

Browse files
DATAREDIS-427 - Polishing.
Use ShadowingClassLoader instead of a self written one. Original Pull Request: #179
1 parent 77630d0 commit 4783a0c

File tree

2 files changed

+9
-45
lines changed

2 files changed

+9
-45
lines changed

src/main/java/org/springframework/data/redis/serializer/JdkSerializationRedisSerializer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
package org.springframework.data.redis.serializer;
1717

1818
import org.springframework.core.convert.converter.Converter;
19+
import org.springframework.core.serializer.DefaultDeserializer;
20+
import org.springframework.core.serializer.DefaultSerializer;
1921
import org.springframework.core.serializer.support.DeserializingConverter;
2022
import org.springframework.core.serializer.support.SerializingConverter;
2123
import org.springframework.util.Assert;
@@ -45,6 +47,7 @@ public JdkSerializationRedisSerializer() {
4547
* Creates a new {@link JdkSerializationRedisSerializer} using a {@link ClassLoader}.
4648
*
4749
* @param classLoader
50+
* @since 1.7
4851
*/
4952
public JdkSerializationRedisSerializer(ClassLoader classLoader) {
5053
this(new SerializingConverter(), new DeserializingConverter(classLoader));
@@ -56,6 +59,7 @@ public JdkSerializationRedisSerializer(ClassLoader classLoader) {
5659
*
5760
* @param serializer must not be {@literal null}
5861
* @param deserializer must not be {@literal null}
62+
* @since 1.7
5963
*/
6064
public JdkSerializationRedisSerializer(Converter<Object, byte[]> serializer, Converter<byte[], Object> deserializer) {
6165

src/test/java/org/springframework/data/redis/serializer/SimpleRedisSerializerTests.java

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,16 @@
1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
2020

21-
import java.io.IOException;
22-
import java.io.InputStream;
2321
import java.io.Serializable;
24-
import java.net.URL;
2522
import java.util.UUID;
2623

2724
import org.junit.After;
2825
import org.junit.Before;
2926
import org.junit.Test;
3027
import org.springframework.data.redis.Address;
3128
import org.springframework.data.redis.Person;
29+
import org.springframework.instrument.classloading.ShadowingClassLoader;
3230
import org.springframework.oxm.xstream.XStreamMarshaller;
33-
import org.springframework.util.StreamUtils;
3431

3532
/**
3633
* @author Jennifer Hickey
@@ -125,10 +122,13 @@ private void verifySerializedObjects(Object... objects) {
125122
}
126123
}
127124

125+
/**
126+
* @see DATAREDIS-427
127+
*/
128128
@Test
129129
public void jdkSerializerShouldUseCustomClassLoader() throws ClassNotFoundException {
130130

131-
ClassLoader customClassLoader = new CustomClassLoader();
131+
ClassLoader customClassLoader = new ShadowingClassLoader(ClassLoader.getSystemClassLoader());
132132

133133
JdkSerializationRedisSerializer serializer = new JdkSerializationRedisSerializer(customClassLoader);
134134
SerializableDomainClass domainClass = new SerializableDomainClass();
@@ -178,44 +178,4 @@ public void testJsonSerializer() throws Exception {
178178
assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
179179
assertEquals(p1, serializer.deserialize(serializer.serialize(p1)));
180180
}
181-
182-
/**
183-
* Custom class loader that loads class files from the test's class path. This {@link ClassLoader} does not delegate
184-
* to a parent class loader to truly load classes that are defined by this class loader and not interfere with any
185-
* parent class loader. The class loader uses simple class definition which is fine for the test but do not use this
186-
* as sample for production class loaders.
187-
*/
188-
private static class CustomClassLoader extends ClassLoader {
189-
190-
public CustomClassLoader() {
191-
super(null);
192-
}
193-
194-
@Override
195-
protected Class<?> findClass(String name) throws ClassNotFoundException {
196-
197-
URL resource = SimpleRedisSerializerTests.class.getResource("/" + name.replace('.', '/') + ".class");
198-
199-
InputStream is = null;
200-
try {
201-
202-
is = resource.openStream();
203-
byte[] bytes = StreamUtils.copyToByteArray(is);
204-
return defineClass(name, bytes, 0, bytes.length);
205-
} catch (IOException o_O) {
206-
throw new ClassNotFoundException("Cannot read class file", o_O);
207-
} finally {
208-
209-
if (is != null) {
210-
try {
211-
is.close();
212-
} catch (IOException e) {
213-
// ignore
214-
}
215-
}
216-
}
217-
218-
}
219-
220-
}
221181
}

0 commit comments

Comments
 (0)