Skip to content

Commit 481ddf4

Browse files
committed
get copyTransient from kryo in FieldSerializer constructor
1 parent 8414444 commit 481ddf4

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/com/esotericsoftware/kryo/serializers/FieldSerializer.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ public class FieldSerializer<T> extends Serializer<T> implements Comparator<Fiel
102102
* </p> */
103103
private boolean useMemRegions = false;
104104

105+
/** If set, transient fields will be copied */
106+
private boolean copyTransient = true;
107+
105108
/** If set, transient fields will be serialized */
106109
private final boolean serializeTransient = false;
107110

@@ -149,6 +152,7 @@ public FieldSerializer (Kryo kryo, Class type) {
149152
this.genericsUtil = new FieldSerializerGenericsUtil(this);
150153
this.unsafeUtil = FieldSerializerUnsafeUtil.Factory.getInstance(this);
151154
this.annotationsUtil = new FieldSerializerAnnotationsUtil(this);
155+
this.copyTransient = kryo.getCopyTransient();
152156
rebuildCachedFields();
153157
}
154158

@@ -486,6 +490,11 @@ public void setUseAsm (boolean setUseAsm) {
486490
rebuildCachedFields();
487491
}
488492

493+
// Enable/disable copying of transient fields
494+
public void setCopyTransient (boolean setCopyTransient) {
495+
copyTransient = setCopyTransient;
496+
}
497+
489498
/** This method can be called for different fields having the same type. Even though the raw type is the same, if the type is
490499
* generic, it could happen that different concrete classes are used to instantiate it. Therefore, in case of different
491500
* instantiation parameters, the fields analysis should be repeated.
@@ -647,6 +656,10 @@ public boolean getUseMemRegions () {
647656
return useMemRegions;
648657
}
649658

659+
public boolean getCopyTransient () {
660+
return copyTransient;
661+
}
662+
650663
/** Used by {@link #copy(Kryo, Object)} to create the new object. This can be overridden to customize object creation, eg to
651664
* call a constructor with arguments. The default implementation uses {@link Kryo#newInstance(Class)}. */
652665
protected T createCopy (Kryo kryo, T original) {
@@ -658,7 +671,7 @@ public T copy (Kryo kryo, T original) {
658671
kryo.reference(copy);
659672

660673
// Copy transient fields
661-
if (kryo.getCopyTransient()) {
674+
if (copyTransient) {
662675
for (int i = 0, n = transientFields.length; i < n; i++)
663676
transientFields[i].copy(original, copy);
664677
}

test/com/esotericsoftware/kryo/FieldSerializerTest.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1616
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
1717
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
18-
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
19-
18+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
19+
2020
package com.esotericsoftware.kryo;
2121

2222
import java.util.ArrayList;
@@ -26,19 +26,22 @@
2626
import java.util.Iterator;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.Map.Entry;
2930

3031
import org.objenesis.strategy.StdInstantiatorStrategy;
3132

3233
import com.esotericsoftware.kryo.io.Input;
3334
import com.esotericsoftware.kryo.io.Output;
34-
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
3535
import com.esotericsoftware.kryo.serializers.CollectionSerializer.BindCollection;
3636
import com.esotericsoftware.kryo.serializers.DefaultArraySerializers.IntArraySerializer;
3737
import com.esotericsoftware.kryo.serializers.DefaultArraySerializers.LongArraySerializer;
38+
import com.esotericsoftware.kryo.serializers.DefaultArraySerializers.ObjectArraySerializer;
3839
import com.esotericsoftware.kryo.serializers.DefaultSerializers.StringSerializer;
3940
import com.esotericsoftware.kryo.serializers.FieldSerializer;
4041
import com.esotericsoftware.kryo.serializers.FieldSerializer.Bind;
4142
import com.esotericsoftware.kryo.serializers.FieldSerializer.Optional;
43+
import com.esotericsoftware.kryo.serializers.MapSerializer;
44+
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
4245
import com.esotericsoftware.kryo.serializers.MapSerializer.BindMap;
4346

4447
/** @author Nathan Sweet <[email protected]> */
@@ -447,13 +450,15 @@ public void testTransients () {
447450
objectWithTransients1.anotherField2 = 5;
448451
objectWithTransients1.anotherField3 = "Field2";
449452

450-
kryo.setCopyTransient(false);
453+
FieldSerializer<HasTransients> ser = (FieldSerializer<HasTransients>)kryo.getSerializer(HasTransients.class);
454+
ser.setCopyTransient(false);
455+
451456
HasTransients objectWithTransients3 = kryo.copy(objectWithTransients1);
452457
assertTrue("Objects should be different if copy does not include transient fields",
453458
!objectWithTransients3.equals(objectWithTransients1));
454459
assertEquals("transient fields should be null", objectWithTransients3.transientField1, null);
455460

456-
kryo.setCopyTransient(true);
461+
ser.setCopyTransient(true);
457462
HasTransients objectWithTransients2 = kryo.copy(objectWithTransients1);
458463
assertEquals("Objects should be equal if copy includes transient fields", objectWithTransients2, objectWithTransients1);
459464
}
@@ -1038,4 +1043,4 @@ public boolean equals(Object o) {
10381043
return true;
10391044
}
10401045
}
1041-
}
1046+
}

0 commit comments

Comments
 (0)