Skip to content

Commit d8aafda

Browse files
garyrussellartembilan
authored andcommitted
Remove usage of obsolete NestedIOException
Related to spring-projects/spring-framework#28198 Replaces its usage with the standard `IOException` which has supported a root cause since Java 6.
1 parent 0ccfcbe commit d8aafda

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

spring-amqp/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,17 @@
2222
import java.io.ObjectInputStream;
2323
import java.io.ObjectStreamClass;
2424
import java.io.UnsupportedEncodingException;
25+
import java.nio.charset.StandardCharsets;
2526

2627
import org.springframework.amqp.core.Message;
2728
import org.springframework.amqp.core.MessageProperties;
2829
import org.springframework.beans.DirectFieldAccessor;
2930
import org.springframework.core.ConfigurableObjectInputStream;
30-
import org.springframework.core.NestedIOException;
3131
import org.springframework.core.serializer.DefaultDeserializer;
3232
import org.springframework.core.serializer.DefaultSerializer;
3333
import org.springframework.core.serializer.Deserializer;
3434
import org.springframework.core.serializer.Serializer;
35+
import org.springframework.lang.Nullable;
3536

3637
/**
3738
* Implementation of {@link MessageConverter} that can work with Strings or native objects
@@ -47,10 +48,11 @@
4748
*
4849
* @author Dave Syer
4950
* @author Gary Russell
51+
* @author Artem Bilan
5052
*/
5153
public class SerializerMessageConverter extends AllowedListDeserializingMessageConverter {
5254

53-
public static final String DEFAULT_CHARSET = "UTF-8";
55+
public static final String DEFAULT_CHARSET = StandardCharsets.UTF_8.name();
5456

5557
private volatile String defaultCharset = DEFAULT_CHARSET;
5658

@@ -80,7 +82,7 @@ public void setIgnoreContentType(boolean ignoreContentType) {
8082
*
8183
* @param defaultCharset The default charset.
8284
*/
83-
public void setDefaultCharset(String defaultCharset) {
85+
public void setDefaultCharset(@Nullable String defaultCharset) {
8486
this.defaultCharset = (defaultCharset != null) ? defaultCharset : DEFAULT_CHARSET;
8587
}
8688

@@ -182,7 +184,7 @@ protected Class<?> resolveClass(ObjectStreamClass classDesc)
182184
return objectInputStream.readObject();
183185
}
184186
catch (ClassNotFoundException ex) {
185-
throw new NestedIOException("Failed to deserialize object type", ex);
187+
throw new IOException("Failed to deserialize object type", ex);
186188
}
187189
}
188190

spring-amqp/src/main/java/org/springframework/amqp/utils/SerializationUtils.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.Set;
2727

2828
import org.springframework.core.ConfigurableObjectInputStream;
29-
import org.springframework.core.NestedIOException;
3029
import org.springframework.util.ObjectUtils;
3130
import org.springframework.util.PatternMatchUtils;
3231

@@ -35,6 +34,7 @@
3534
*
3635
* @author Dave Syer
3736
* @author Gary Russell
37+
* @author Artem Bilan
3838
*/
3939
public final class SerializationUtils {
4040

@@ -111,22 +111,22 @@ public static Object deserialize(InputStream inputStream, Set<String> allowedLis
111111
throws IOException {
112112

113113
try (
114-
ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, classLoader) {
114+
ObjectInputStream objectInputStream = new ConfigurableObjectInputStream(inputStream, classLoader) {
115115

116-
@Override
117-
protected Class<?> resolveClass(ObjectStreamClass classDesc)
118-
throws IOException, ClassNotFoundException {
119-
Class<?> clazz = super.resolveClass(classDesc);
120-
checkAllowedList(clazz, allowedListPatterns);
121-
return clazz;
122-
}
116+
@Override
117+
protected Class<?> resolveClass(ObjectStreamClass classDesc)
118+
throws IOException, ClassNotFoundException {
119+
Class<?> clazz = super.resolveClass(classDesc);
120+
checkAllowedList(clazz, allowedListPatterns);
121+
return clazz;
122+
}
123123

124-
}) {
124+
}) {
125125

126126
return objectInputStream.readObject();
127127
}
128128
catch (ClassNotFoundException ex) {
129-
throw new NestedIOException("Failed to deserialize object type", ex);
129+
throw new IOException("Failed to deserialize object type", ex);
130130
}
131131
}
132132

spring-amqp/src/test/java/org/springframework/amqp/support/converter/SerializerMessageConverterTests.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,35 @@
2323

2424
import java.io.ByteArrayInputStream;
2525
import java.io.ByteArrayOutputStream;
26+
import java.io.IOException;
2627
import java.io.InputStream;
2728
import java.io.ObjectInputStream;
2829
import java.io.ObjectOutputStream;
30+
import java.nio.charset.StandardCharsets;
2931

3032
import org.junit.jupiter.api.Test;
3133
import org.mockito.Mockito;
3234

3335
import org.springframework.amqp.core.Message;
3436
import org.springframework.amqp.core.MessageProperties;
3537
import org.springframework.amqp.utils.test.TestUtils;
36-
import org.springframework.core.NestedIOException;
3738
import org.springframework.core.serializer.DefaultDeserializer;
3839
import org.springframework.core.serializer.Deserializer;
3940

4041
/**
4142
* @author Mark Fisher
4243
* @author Gary Russell
44+
* @author Artem Bilan
4345
*/
4446
public class SerializerMessageConverterTests extends AllowedListDeserializingMessageConverterTests {
4547

4648
@Test
47-
public void bytesAsDefaultMessageBodyType() throws Exception {
49+
public void bytesAsDefaultMessageBodyType() {
4850
SerializerMessageConverter converter = new SerializerMessageConverter();
4951
Message message = new Message("test".getBytes(), new MessageProperties());
5052
Object result = converter.fromMessage(message);
5153
assertThat(result.getClass()).isEqualTo(byte[].class);
52-
assertThat(new String((byte[]) result, "UTF-8")).isEqualTo("test");
54+
assertThat(new String((byte[]) result, StandardCharsets.UTF_8)).isEqualTo("test");
5355
}
5456

5557
@Test
@@ -65,7 +67,7 @@ public void messageToString() {
6567
@Test
6668
public void messageToBytes() {
6769
SerializerMessageConverter converter = new SerializerMessageConverter();
68-
Message message = new Message(new byte[] { 1, 2, 3 }, new MessageProperties());
70+
Message message = new Message(new byte[]{ 1, 2, 3 }, new MessageProperties());
6971
message.getMessageProperties().setContentType(MessageProperties.CONTENT_TYPE_BYTES);
7072
Object result = converter.fromMessage(message);
7173
assertThat(result.getClass()).isEqualTo(byte[].class);
@@ -126,7 +128,7 @@ public void stringToMessage() throws Exception {
126128
@Test
127129
public void bytesToMessage() throws Exception {
128130
SerializerMessageConverter converter = new SerializerMessageConverter();
129-
Message message = converter.toMessage(new byte[] { 1, 2, 3 }, new MessageProperties());
131+
Message message = converter.toMessage(new byte[]{ 1, 2, 3 }, new MessageProperties());
130132
String contentType = message.getMessageProperties().getContentType();
131133
byte[] body = message.getBody();
132134
assertThat(contentType).isEqualTo("application/octet-stream");
@@ -168,7 +170,7 @@ public void testDefaultDeserializerClassLoader() throws Exception {
168170
}
169171

170172
@Test
171-
public void messageConversionExceptionForClassNotFound() throws Exception {
173+
public void messageConversionExceptionForClassNotFound() {
172174
SerializerMessageConverter converter = new SerializerMessageConverter();
173175
TestBean testBean = new TestBean("foo");
174176
Message message = converter.toMessage(testBean, new MessageProperties());
@@ -177,8 +179,8 @@ public void messageConversionExceptionForClassNotFound() throws Exception {
177179
byte[] body = message.getBody();
178180
body[10] = 'z';
179181
assertThatThrownBy(() -> converter.fromMessage(message))
180-
.isExactlyInstanceOf(MessageConversionException.class)
181-
.hasCauseExactlyInstanceOf(NestedIOException.class);
182+
.isExactlyInstanceOf(MessageConversionException.class)
183+
.hasCauseExactlyInstanceOf(IOException.class);
182184
}
183185

184186
}

0 commit comments

Comments
 (0)