From b5af09200a8659c6438befe88b5bcfee5377c7c0 Mon Sep 17 00:00:00 2001 From: Thomas Heigl Date: Wed, 4 Nov 2020 10:35:31 +0100 Subject: [PATCH] #774 Do not re-use serializer instances for `Compatible` and `Tagged` serializers in `ReflectField` --- .../kryo/serializers/CompatibleFieldSerializer.java | 8 ++------ .../kryo/serializers/FieldSerializer.java | 11 ++++++++--- .../kryo/serializers/ReflectField.java | 4 ++-- .../kryo/serializers/TaggedFieldSerializer.java | 6 +----- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java b/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java index 118fea06a..599787a1e 100644 --- a/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java +++ b/src/com/esotericsoftware/kryo/serializers/CompatibleFieldSerializer.java @@ -47,7 +47,7 @@ public class CompatibleFieldSerializer extends FieldSerializer { private static final int binarySearchThreshold = 32; - private CompatibleFieldSerializerConfig config; + private final CompatibleFieldSerializerConfig config; public CompatibleFieldSerializer (Kryo kryo, Class type) { this(kryo, type, new CompatibleFieldSerializerConfig()); @@ -56,6 +56,7 @@ public CompatibleFieldSerializer (Kryo kryo, Class type) { public CompatibleFieldSerializer (Kryo kryo, Class type, CompatibleFieldSerializerConfig config) { super(kryo, type, config); this.config = config; + setReuseSerializers(false); } @Override @@ -242,11 +243,6 @@ else if (compare > 0) return fields; } - @Override - boolean supportsReuse() { - return !config.readUnknownFieldData; - } - public CompatibleFieldSerializerConfig getCompatibleFieldSerializerConfig () { return config; } diff --git a/src/com/esotericsoftware/kryo/serializers/FieldSerializer.java b/src/com/esotericsoftware/kryo/serializers/FieldSerializer.java index ffb6957e9..7d73c044c 100644 --- a/src/com/esotericsoftware/kryo/serializers/FieldSerializer.java +++ b/src/com/esotericsoftware/kryo/serializers/FieldSerializer.java @@ -63,6 +63,7 @@ public class FieldSerializer extends Serializer { final FieldSerializerConfig config; final CachedFields cachedFields; private final GenericsHierarchy genericsHierarchy; + private boolean reuseSerializers = true; public FieldSerializer (Kryo kryo, Class type) { this(kryo, type, new FieldSerializerConfig()); @@ -211,9 +212,13 @@ protected T createCopy (Kryo kryo, T original) { return (T)kryo.newInstance(original.getClass()); } - /** If true, the serializer supports re-use of serializers for all instances of a field */ - boolean supportsReuse() { - return true; + boolean getReuseSerializers() { + return reuseSerializers; + } + + /** If true, {@link ReflectField}s re-use serializers for all instances of a field if the concrete value class is known */ + void setReuseSerializers(boolean reuseSerializers) { + this.reuseSerializers = reuseSerializers; } @Override diff --git a/src/com/esotericsoftware/kryo/serializers/ReflectField.java b/src/com/esotericsoftware/kryo/serializers/ReflectField.java index c19d04510..2d4a23f84 100644 --- a/src/com/esotericsoftware/kryo/serializers/ReflectField.java +++ b/src/com/esotericsoftware/kryo/serializers/ReflectField.java @@ -73,7 +73,7 @@ public void write (Output output, Object object) { if (serializer == null) { serializer = kryo.getSerializer(concreteType); // The concrete type of the field is known, always use the same serializer. - if (valueClass != null && this.fieldSerializer.supportsReuse()) this.serializer = serializer; + if (valueClass != null && this.fieldSerializer.getReuseSerializers()) this.serializer = serializer; } kryo.getGenerics().pushGenericType(genericType); if (canBeNull) { @@ -121,7 +121,7 @@ public void read (Input input, Object object) { if (serializer == null) { serializer = kryo.getSerializer(concreteType); // The concrete type of the field is known, always use the same serializer. - if (valueClass != null && this.fieldSerializer.supportsReuse()) this.serializer = serializer; + if (valueClass != null && this.fieldSerializer.getReuseSerializers()) this.serializer = serializer; } kryo.getGenerics().pushGenericType(genericType); if (canBeNull) diff --git a/src/com/esotericsoftware/kryo/serializers/TaggedFieldSerializer.java b/src/com/esotericsoftware/kryo/serializers/TaggedFieldSerializer.java index 4df4187f8..abdf77282 100644 --- a/src/com/esotericsoftware/kryo/serializers/TaggedFieldSerializer.java +++ b/src/com/esotericsoftware/kryo/serializers/TaggedFieldSerializer.java @@ -72,6 +72,7 @@ public TaggedFieldSerializer (Kryo kryo, Class type, TaggedFieldSerializerConfig super(kryo, type, config); this.config = config; setAcceptsNull(true); + setReuseSerializers(false); } @Override @@ -242,11 +243,6 @@ public T read (Kryo kryo, Input input, Class type) { return object; } - @Override - boolean supportsReuse() { - return !config.readUnknownTagData; - } - public TaggedFieldSerializerConfig getTaggedFieldSerializerConfig () { return config; }