Skip to content

Commit fcb8caf

Browse files
committed
Improve Kryo Codec for registrations
# Conflicts: # spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java
1 parent 35ea268 commit fcb8caf

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@ public abstract class AbstractKryoCodec implements Codec {
4444
protected AbstractKryoCodec() {
4545
KryoFactory factory = () -> {
4646
Kryo kryo = new Kryo();
47+
kryo.setRegistrationRequired(true);
4748
// configure Kryo instance, customize settings
4849
configureKryoInstance(kryo);
4950
return kryo;

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

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

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.List;
2122

2223
import org.springframework.util.Assert;
@@ -39,6 +40,13 @@ public class KryoClassListRegistrar extends AbstractKryoRegistrar {
3940

4041
private int initialValue = 50;
4142

43+
/**
44+
* @param classes the vararg of classes to validateRegistration
45+
*/
46+
public KryoClassListRegistrar(Class<?>... classes) {
47+
this(Arrays.asList(classes));
48+
}
49+
4250
/**
4351
* @param classes the list of classes to validateRegistration
4452
*/
@@ -52,8 +60,7 @@ public KryoClassListRegistrar(List<Class<?>> classes) {
5260
* @param initialValue the initial value
5361
*/
5462
public void setInitialValue(int initialValue) {
55-
Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE,
56-
"'initialValue' must be >= " + MIN_REGISTRATION_VALUE);
63+
Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE, "'initialValue' must be >= " + MIN_REGISTRATION_VALUE);
5764
this.initialValue = initialValue;
5865
}
5966

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,8 +39,9 @@ public class CompositeCodecTests {
3939

4040
@Before
4141
public void setup() {
42-
Map<Class<?>, Codec> codecs = new HashMap<Class<?>, Codec>();
43-
this.codec = new CompositeCodec(codecs, new PojoCodec());
42+
Map<Class<?>, Codec> codecs = new HashMap<>();
43+
this.codec = new CompositeCodec(codecs, new PojoCodec(
44+
new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)));
4445
}
4546

4647
@Test

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015-2019 the original author or authors.
2+
* Copyright 2015-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -63,7 +63,7 @@ public void testSerializationWithStreams() throws IOException {
6363

6464
@Test
6565
public void testPojoSerialization() throws IOException {
66-
PojoCodec codec = new PojoCodec();
66+
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class));
6767
SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("foo", 123);
6868
ByteArrayOutputStream bos = new ByteArrayOutputStream();
6969
codec.encode(foo, bos);
@@ -98,12 +98,12 @@ public void testPrimitiveSerialization() throws IOException {
9898

9999
@Test
100100
public void testMapSerialization() throws IOException {
101-
PojoCodec codec = new PojoCodec();
101+
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(HashMap.class));
102102
Map<String, Integer> map = new HashMap<String, Integer>();
103103
map.put("one", 1);
104104
map.put("two", 2);
105105
ByteArrayOutputStream bos = new ByteArrayOutputStream();
106-
codec.encode(map, bos);
106+
codec.encode(map, bos);4
107107
Map<?, ?> m2 = (Map<?, ?>) codec.decode(bos.toByteArray(), HashMap.class);
108108
assertEquals(2, m2.size());
109109
assertEquals(1, m2.get("one"));
@@ -112,7 +112,7 @@ public void testMapSerialization() throws IOException {
112112

113113
@Test
114114
public void testComplexObjectSerialization() throws IOException {
115-
PojoCodec codec = new PojoCodec();
115+
PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(Foo.class));
116116
Foo foo = new Foo();
117117
foo.put("one", 1);
118118
foo.put("two", 2);

0 commit comments

Comments
 (0)