Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copyTransient is not global #375

Merged
merged 4 commits into from
Feb 16, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 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,24 @@ public void setCopyReferences (boolean copyReferences) {
this.copyReferences = copyReferences;
}

/**
* If false, when {@link #copy(Object)} is called all transient fields that are accessible will be ignored from
* being copied. This has to be set before registering classes with kryo for it to be used by all field
* serializers. If transient fields has to be copied for specific classes then use {@link FieldSerializer#setCopyTransient(boolean)}.
* Default is true.
*/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem with indentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it in the latest commit.

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
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public FieldSerializer (Kryo kryo, Class type) {
this.genericsUtil = new FieldSerializerGenericsUtil(this);
this.unsafeUtil = FieldSerializerUnsafeUtil.Factory.getInstance(this);
this.annotationsUtil = new FieldSerializerAnnotationsUtil(this);
this.copyTransient = kryo.getCopyTransient();
rebuildCachedFields();
}

Expand All @@ -172,6 +173,7 @@ public FieldSerializer (Kryo kryo, Class type, Class[] generics) {
this.genericsUtil = new FieldSerializerGenericsUtil(this);
this.unsafeUtil = FieldSerializerUnsafeUtil.Factory.getInstance(this);
this.annotationsUtil = new FieldSerializerAnnotationsUtil(this);
this.copyTransient = kryo.getCopyTransient();
rebuildCachedFields();
}

Expand Down
32 changes: 24 additions & 8 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. */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this file header touched at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line no: 18, 19 and EOF was missing CRLF. IntelliJ inserted the line feed automatically.

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 @@ -462,7 +459,26 @@ public void testTransients () {
HasTransients objectWithTransients2 = kryo.copy(objectWithTransients1);
assertEquals("Objects should be equal if copy includes transient fields", objectWithTransients2, objectWithTransients1);
}


public void testTransientsUsingGlobalConfig () {
kryo.setCopyTransient(false);
kryo.register(HasTransients.class);
HasTransients objectWithTransients1 = new HasTransients();
objectWithTransients1.transientField1 = "Test";
objectWithTransients1.anotherField2 = 5;
objectWithTransients1.anotherField3 = "Field2";

FieldSerializer<HasTransients> ser = (FieldSerializer<HasTransients>)kryo.getSerializer(HasTransients.class);
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);
HasTransients objectWithTransients2 = kryo.copy(objectWithTransients1);
assertEquals("Objects should be equal if copy includes transient fields", objectWithTransients2, objectWithTransients1);
}

public void testCorrectlyAnnotatedFields () {
kryo.register(int[].class);
kryo.register(long[].class);
Expand Down Expand Up @@ -1043,4 +1059,4 @@ public boolean equals(Object o) {
return true;
}
}
}
}