Skip to content

Commit 75519db

Browse files
committed
Introduce FieldSerializerConfig to encapsulate config in Kryo
The `Kryo` class is more and more used for "global" configuration of serializers like `FieldSerializer` etc (here's another PR: EsotericSoftware#365). The semantics of such settings and especially for which serializer it's applied can often only be clarified via documentation. This change introduces a `FieldSerializerConfig` class that encapsulates configuration for `FieldSerializer`. It allows to configure settings for all `FieldSerializer` instances created via `Kryo.getFieldSerializerConfig`. `Kryo.asmEnabled` might be used by different serializers, but because until now it was only used by `FieldSerializer` this setting is also kept in `FieldSerializerConfig`. `Kryo.set/getAsmEnabled` is marked as deprecated with a hint to use `getFieldSerializerConfig` instead - this is done to stay binary compatible. `Kryo.setCopyTransient` is removed because it was introduced after the latest release 3.0.3 and therefore not yet a published/released. Now `Kryo.getFieldSerializerConfig.setCopyTransient` has to be used. If this is considered to be merged then EsotericSoftware#365 should also follow this approach.
1 parent c10fd76 commit 75519db

File tree

3 files changed

+137
-83
lines changed

3 files changed

+137
-83
lines changed

src/com/esotericsoftware/kryo/Kryo.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import java.util.TreeSet;
4646

4747
import com.esotericsoftware.kryo.serializers.DefaultSerializers.URLSerializer;
48+
import com.esotericsoftware.kryo.serializers.FieldSerializer.FieldSerializerConfig;
4849
import com.esotericsoftware.kryo.serializers.OptionalSerializers;
4950
import com.esotericsoftware.kryo.serializers.GenericsResolver;
5051
import com.esotericsoftware.kryo.serializers.TimeSerializers;
@@ -143,12 +144,11 @@ public class Kryo {
143144

144145
private int copyDepth;
145146
private boolean copyShallow;
146-
private boolean copyTransient = true;
147147
private IdentityMap originalToCopy;
148148
private Object needsCopyReference;
149149
private GenericsResolver genericsResolver = new GenericsResolver();
150-
/** Tells if ASM-based backend should be used by new serializer instances created using this Kryo instance. */
151-
private boolean asmEnabled = false;
150+
151+
private FieldSerializerConfig fieldSerializerConfig = new FieldSerializerConfig();
152152

153153
private StreamFactory streamFactory;
154154

@@ -1058,22 +1058,11 @@ public void setCopyReferences (boolean copyReferences) {
10581058
this.copyReferences = copyReferences;
10591059
}
10601060

1061-
/**
1062-
* If false, when {@link #copy(Object)} is called all transient fields that are accessible will be ignored from
1063-
* being copied. This has to be set before registering classes with kryo for it to be used by all field
1064-
* serializers. If transient fields has to be copied for specific classes then use {@link FieldSerializer#setCopyTransient(boolean)}.
1065-
* Default is true.
1066-
*/
1067-
public void setCopyTransient(boolean copyTransient) {
1068-
this.copyTransient = copyTransient;
1069-
}
1070-
1071-
/**
1072-
* Returns true if copying of transient fields is enabled for {@link #copy(Object)}.
1073-
* @return true if transient field copy is enable
1074-
*/
1075-
public boolean getCopyTransient() {
1076-
return copyTransient;
1061+
/** The default configuration for {@link FieldSerializer} instances. Already existing serializer instances (e.g.
1062+
* implicitely created for already registered classes) are not affected by this configuration. You can override
1063+
* the configuration for a single {@link FieldSerializer}. */
1064+
public FieldSerializerConfig getFieldSerializerConfig() {
1065+
return fieldSerializerConfig;
10771066
}
10781067

10791068
/** Sets the reference resolver and enables references. */
@@ -1209,13 +1198,18 @@ public void setStreamFactory (StreamFactory streamFactory) {
12091198
* </p>
12101199
*
12111200
* @param flag if true, ASM-based backend will be used. Otherwise Unsafe-based backend could be used by some serializers, e.g.
1212-
* FieldSerializer */
1201+
* FieldSerializer
1202+
*
1203+
* @deprecated Use {@link #getFieldSerializerConfig()} to change the default {@link FieldSerializer} configuration. */
1204+
@Deprecated
12131205
public void setAsmEnabled (boolean flag) {
1214-
this.asmEnabled = flag;
1206+
fieldSerializerConfig.setUseAsm(flag);
12151207
}
12161208

1209+
/** @deprecated Use {@link #getFieldSerializerConfig()} to change the default {@link FieldSerializer} configuration. */
1210+
@Deprecated
12171211
public boolean getAsmEnabled () {
1218-
return asmEnabled;
1212+
return fieldSerializerConfig.isUseAsm();
12191213
}
12201214

12211215
static public class DefaultInstantiatorStrategy implements org.objenesis.strategy.InstantiatorStrategy {

0 commit comments

Comments
 (0)