Skip to content

Commit 4788f5e

Browse files
committed
Adjust the behavior of the class so that delegates are required.
* In the current implementation, users could pass in an empty list to the composite codec. * This is an CompositeCodec and if we had no codecs, then why have the class. * All @nullable's were removed from the delegate attribute. * Null returns from the findDelegate have been removed. The default delegate will be returned in the stead. * Update the tests to require at least one codec in the delegates
1 parent 9876db3 commit 4788f5e

File tree

3 files changed

+25
-33
lines changed

3 files changed

+25
-33
lines changed

spring-integration-core/src/main/java/org/springframework/integration/codec/CompositeCodec.java

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.util.HashMap;
2424
import java.util.Map;
2525

26-
import org.jspecify.annotations.Nullable;
27-
2826
import org.springframework.integration.util.ClassUtils;
2927
import org.springframework.util.Assert;
3028

@@ -41,9 +39,20 @@ public class CompositeCodec implements Codec {
4139

4240
public CompositeCodec(Map<Class<?>, Codec> delegates, Codec defaultCodec) {
4341
this.defaultCodec = defaultCodec;
42+
Assert.notEmpty(delegates, "delegates must not be empty");
4443
this.delegates = new HashMap<Class<?>, Codec>(delegates);
4544
}
4645

46+
/**
47+
* @since 7.0
48+
*
49+
* @deprecated This constructor is deprecated because the delegates map must contain at least one entry.
50+
* Creating a {@code CompositeCodec} without any delegates is not a valid use case, as there would be
51+
* no codecs to delegate to for encoding or decoding operations. Please use the constructor that accepts
52+
* a non-empty delegates map to ensure proper delegation behavior.
53+
*
54+
*/
55+
@Deprecated
4756
public CompositeCodec(Codec defaultCodec) {
4857
this(Map.of(), defaultCodec);
4958
}
@@ -52,52 +61,35 @@ public CompositeCodec(Codec defaultCodec) {
5261
public void encode(Object object, OutputStream outputStream) throws IOException {
5362
Assert.notNull(object, "cannot encode a null object");
5463
Assert.notNull(outputStream, "'outputStream' cannot be null");
55-
Codec codec = findDelegate(object.getClass());
56-
if (codec != null) {
57-
codec.encode(object, outputStream);
58-
}
59-
else {
60-
this.defaultCodec.encode(object, outputStream);
61-
}
64+
findDelegate(object.getClass()).encode(object, outputStream);
6265
}
6366

6467
@Override
6568
public byte[] encode(Object object) throws IOException {
6669
Assert.notNull(object, "cannot encode a null object");
67-
Codec codec = findDelegate(object.getClass());
68-
if (codec != null) {
69-
return codec.encode(object);
70-
}
71-
else {
72-
return this.defaultCodec.encode(object);
73-
}
70+
return findDelegate(object.getClass()).encode(object);
7471
}
7572

7673
@Override
7774
public <T> T decode(InputStream inputStream, Class<T> type) throws IOException {
7875
Assert.notNull(inputStream, "'inputStream' cannot be null");
7976
Assert.notNull(type, "'type' cannot be null");
80-
Codec codec = findDelegate(type);
81-
if (codec != null) {
82-
return codec.decode(inputStream, type);
83-
}
84-
else {
85-
return this.defaultCodec.decode(inputStream, type);
86-
}
77+
return findDelegate(type).decode(inputStream, type);
8778
}
8879

8980
@Override
9081
public <T> T decode(byte[] bytes, Class<T> type) throws IOException {
9182
return decode(new ByteArrayInputStream(bytes), type);
9283
}
9384

94-
private @Nullable Codec findDelegate(Class<?> type) {
95-
if (this.delegates.isEmpty()) {
96-
return null;
97-
}
98-
85+
private Codec findDelegate(Class<?> type) {
86+
Assert.state(!this.delegates.isEmpty(), "Delegates must not be empty");
9987
Class<?> clazz = ClassUtils.findClosestMatch(type, this.delegates.keySet(), false);
100-
return this.delegates.get(clazz);
88+
Codec codec = this.delegates.get(clazz);
89+
if (codec == null) {
90+
codec = this.defaultCodec;
91+
}
92+
return codec;
10193
}
10294

10395
}

spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.integration.codec.kryo;
1818

1919
import java.io.IOException;
20-
import java.util.HashMap;
2120
import java.util.Map;
2221

2322
import org.junit.jupiter.api.BeforeEach;
@@ -38,7 +37,8 @@ public class CompositeCodecTests {
3837

3938
@BeforeEach
4039
public void setup() {
41-
Map<Class<?>, Codec> codecs = new HashMap<>();
40+
PojoCodec pojoCodec = new PojoCodec();
41+
Map<Class<?>, Codec> codecs = Map.of(pojoCodec.getClass(), pojoCodec);
4242
this.codec = new CompositeCodec(codecs, new PojoCodec(
4343
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
4444
}

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpMessageMapperTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.net.InetAddress;
2222
import java.net.Socket;
2323
import java.util.Collections;
24-
import java.util.HashMap;
2524
import java.util.Map;
2625

2726
import javax.net.SocketFactory;
@@ -67,7 +66,8 @@ public class TcpMessageMapperTests {
6766

6867
@BeforeEach
6968
public void setup() {
70-
Map<Class<?>, Codec> codecs = new HashMap<>();
69+
MessageCodec messageCodec = new MessageCodec();
70+
Map<Class<?>, Codec> codecs = Map.of(messageCodec.getClass(), messageCodec);
7171
this.codec = new CompositeCodec(codecs, new MessageCodec());
7272
}
7373

0 commit comments

Comments
 (0)