Skip to content

Commit

Permalink
copyTransient is not global
Browse files Browse the repository at this point in the history
  • Loading branch information
prasanthj committed Dec 9, 2015
1 parent ba67801 commit 8414444
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
17 changes: 17 additions & 0 deletions src/com/esotericsoftware/kryo/Kryo.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public class Kryo {

private int copyDepth;
private boolean copyShallow;
private boolean copyTransient = true;
private IdentityMap originalToCopy;
private Object needsCopyReference;
private GenericsResolver genericsResolver = new GenericsResolver();
Expand Down Expand Up @@ -1046,6 +1047,22 @@ public void setCopyReferences (boolean copyReferences) {
this.copyReferences = copyReferences;
}

/**
* If true, when {@link #copy(Object)} is called all transient fields that are accessible will be ignored from
* being copied. Default is true.
*/
public void setCopyTransient(boolean copyTransient) {
this.copyTransient = copyTransient;
}

/**
* Returns true if copying of transient fields is enabled for {@link #copy(Object)}.
* @return true if transient field copy is enable
*/
public boolean getCopyTransient() {
return copyTransient;
}

/** Sets the reference resolver and enables references. */
public void setReferenceResolver (ReferenceResolver referenceResolver) {
if (referenceResolver == null) throw new IllegalArgumentException("referenceResolver cannot be null.");
Expand Down
14 changes: 1 addition & 13 deletions src/com/esotericsoftware/kryo/serializers/FieldSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,6 @@ public class FieldSerializer<T> extends Serializer<T> implements Comparator<Fiel
* </p> */
private boolean useMemRegions = false;

/** If set, transient fields will be copied */
private boolean copyTransient = true;

/** If set, transient fields will be serialized */
private final boolean serializeTransient = false;

Expand Down Expand Up @@ -489,11 +486,6 @@ public void setUseAsm (boolean setUseAsm) {
rebuildCachedFields();
}

// Enable/disable copying of transient fields
public void setCopyTransient (boolean setCopyTransient) {
copyTransient = setCopyTransient;
}

/** This method can be called for different fields having the same type. Even though the raw type is the same, if the type is
* generic, it could happen that different concrete classes are used to instantiate it. Therefore, in case of different
* instantiation parameters, the fields analysis should be repeated.
Expand Down Expand Up @@ -655,10 +647,6 @@ public boolean getUseMemRegions () {
return useMemRegions;
}

public boolean getCopyTransient () {
return copyTransient;
}

/** Used by {@link #copy(Kryo, Object)} to create the new object. This can be overridden to customize object creation, eg to
* call a constructor with arguments. The default implementation uses {@link Kryo#newInstance(Class)}. */
protected T createCopy (Kryo kryo, T original) {
Expand All @@ -670,7 +658,7 @@ public T copy (Kryo kryo, T original) {
kryo.reference(copy);

// Copy transient fields
if (copyTransient) {
if (kryo.getCopyTransient()) {
for (int i = 0, n = transientFields.length; i < n; i++)
transientFields[i].copy(original, copy);
}
Expand Down
17 changes: 6 additions & 11 deletions test/com/esotericsoftware/kryo/FieldSerializerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.esotericsoftware.kryo;

import java.util.ArrayList;
Expand All @@ -26,22 +26,19 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.objenesis.strategy.StdInstantiatorStrategy;

import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.CollectionSerializer.BindCollection;
import com.esotericsoftware.kryo.serializers.DefaultArraySerializers.IntArraySerializer;
import com.esotericsoftware.kryo.serializers.DefaultArraySerializers.LongArraySerializer;
import com.esotericsoftware.kryo.serializers.DefaultArraySerializers.ObjectArraySerializer;
import com.esotericsoftware.kryo.serializers.DefaultSerializers.StringSerializer;
import com.esotericsoftware.kryo.serializers.FieldSerializer;
import com.esotericsoftware.kryo.serializers.FieldSerializer.Bind;
import com.esotericsoftware.kryo.serializers.FieldSerializer.Optional;
import com.esotericsoftware.kryo.serializers.MapSerializer;
import com.esotericsoftware.kryo.serializers.CollectionSerializer;
import com.esotericsoftware.kryo.serializers.MapSerializer.BindMap;

/** @author Nathan Sweet <misc@n4te.com> */
Expand Down Expand Up @@ -450,15 +447,13 @@ public void testTransients () {
objectWithTransients1.anotherField2 = 5;
objectWithTransients1.anotherField3 = "Field2";

FieldSerializer<HasTransients> ser = (FieldSerializer<HasTransients>)kryo.getSerializer(HasTransients.class);
ser.setCopyTransient(false);

kryo.setCopyTransient(false);
HasTransients objectWithTransients3 = kryo.copy(objectWithTransients1);
assertTrue("Objects should be different if copy does not include transient fields",
!objectWithTransients3.equals(objectWithTransients1));
assertEquals("transient fields should be null", objectWithTransients3.transientField1, null);

ser.setCopyTransient(true);
kryo.setCopyTransient(true);
HasTransients objectWithTransients2 = kryo.copy(objectWithTransients1);
assertEquals("Objects should be equal if copy includes transient fields", objectWithTransients2, objectWithTransients1);
}
Expand Down Expand Up @@ -1043,4 +1038,4 @@ public boolean equals(Object o) {
return true;
}
}
}
}

0 comments on commit 8414444

Please sign in to comment.