Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
phamrak committed May 13, 2016
1 parent 132456e commit e0b5650
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 6 deletions.
Expand Up @@ -46,9 +46,17 @@ public class CompatibleFieldSerializer<T> extends FieldSerializer<T> {
/* For object with more than BINARY_SEARCH_THRESHOLD fields, use binary search instead of iterative search */
private static final int THRESHOLD_BINARY_SEARCH = 32;

private boolean extendedFieldNaming;

public CompatibleFieldSerializer (Kryo kryo, Class type) {
super(kryo, type);
}
};

public CompatibleFieldSerializer (Kryo kryo, Class type, boolean extendedFieldNaming) {
this(kryo, type);
this.extendedFieldNaming = extendedFieldNaming;
};


public void write (Kryo kryo, Output output, T object) {
CachedField[] fields = getFields();
Expand All @@ -58,7 +66,7 @@ public void write (Kryo kryo, Output output, T object) {
if (TRACE) trace("kryo", "Write " + fields.length + " field names.");
output.writeVarInt(fields.length, true);
for (int i = 0, n = fields.length; i < n; i++)
output.writeString(fields[i].field.getName());
output.writeString(getFieldName(fields[i]));
}

OutputChunked outputChunked = new OutputChunked(output, 1024);
Expand All @@ -68,6 +76,15 @@ public void write (Kryo kryo, Output output, T object) {
}
}

@Override
protected String getFieldName(CachedField cachedField) {
if (extendedFieldNaming) {
return cachedField.field.getDeclaringClass().getSimpleName() + "." + cachedField.field.getName();
} else {
return cachedField.field.getName();
}
}

public T read (Kryo kryo, Input input, Class<T> type) {
T object = create(kryo, input, type);
kryo.reference(object);
Expand All @@ -88,7 +105,7 @@ public T read (Kryo kryo, Input input, Class<T> type) {
for (int i = 0; i < length; i++) {
String schemaName = names[i];
for (int ii = 0, nn = allFields.length; ii < nn; ii++) {
if (allFields[ii].field.getName().equals(schemaName)) {
if (getFieldName(allFields[ii]).equals(schemaName)) {
fields[i] = allFields[ii];
continue outer;
}
Expand All @@ -108,7 +125,7 @@ public T read (Kryo kryo, Input input, Class<T> type) {

while (low <= high) {
mid = (low + high) >>> 1;
String midVal = allFields[mid].field.getName();
String midVal = getFieldName(allFields[mid]);
compare = schemaName.compareTo(midVal);

if (compare < 0) {
Expand Down Expand Up @@ -137,7 +154,7 @@ else if (compare > 0) {
// Generic type used to instantiate this field could have
// been changed in the meantime. Therefore take the most
// up-to-date definition of a field
cachedField = getField(cachedField.field.getName());
cachedField = getField(getFieldName(cachedField));
}
if (cachedField == null) {
if (TRACE) trace("kryo", "Skip obsolete field.");
Expand Down
Expand Up @@ -542,10 +542,14 @@ protected T create (Kryo kryo, Input input, Class<T> type) {
/** Allows specific fields to be optimized. */
public CachedField getField (String fieldName) {
for (CachedField cachedField : fields)
if (cachedField.field.getName().equals(fieldName)) return cachedField;
if (getFieldName(cachedField).equals(fieldName)) return cachedField;
throw new IllegalArgumentException("Field \"" + fieldName + "\" not found on class: " + type.getName());
}

protected String getFieldName(CachedField cachedField) {
return cachedField.field.getName();
}

/** Removes a field so that it won't be serialized. */
public void removeField (String fieldName) {
for (int i = 0; i < fields.length; i++) {
Expand Down
@@ -0,0 +1,148 @@
package com.esotericsoftware.kryo;

import com.esotericsoftware.kryo.serializers.CompatibleFieldSerializer;

import java.io.FileNotFoundException;

/**
* Created by phamrak on 13.5.2016.
*/
public class CompatibleFieldSerializerInheritanceTest extends KryoTestCase {
{
supportsCopy = true;
}

public void testAddedField () throws FileNotFoundException {
TestClass2 test2 = new TestClass2();
test2.setText1("someText1");
test2.setText2("someText2");
test2.setMoo1(1);
test2.setMoo2(2l);

CompatibleFieldSerializer serializer = new CompatibleFieldSerializer(kryo, TestClass2.class, true);
kryo.register(TestClass2.class, serializer);
roundTrip(147, 147, test2);

Object object2 = kryo.readClassAndObject(input);
assertEquals(test2, object2);
}

static public class TestClass1 {
private String text1;
private int moo1;
private long moo2;

public String getText1() {
return text1;
}

public void setText1(String text1) {
this.text1 = text1;
}

public int getMoo1() {
return moo1;
}

public void setMoo1(int moo1) {
this.moo1 = moo1;
}

public long getMoo2() {
return moo2;
}

public void setMoo2(long moo2) {
this.moo2 = moo2;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

TestClass1 that = (TestClass1) o;

if (moo1 != that.moo1) return false;
if (moo2 != that.moo2) return false;
return text1 != null ? text1.equals(that.text1) : that.text1 == null;

}

@Override
public int hashCode() {
int result = text1 != null ? text1.hashCode() : 0;
result = 31 * result + moo1;
result = 31 * result + (int) (moo2 ^ (moo2 >>> 32));
return result;
}
}

static public class TestClass2 extends TestClass1 {
private String text1;
private String text2;
private int moo1;
private long moo2;

@Override
public String getText1() {
return text1;
}

@Override
public void setText1(String text1) {
this.text1 = text1;
}

public String getText2() {
return text2;
}

public void setText2(String text2) {
this.text2 = text2;
}

@Override
public int getMoo1() {
return moo1;
}

@Override
public void setMoo1(int moo1) {
this.moo1 = moo1;
}

@Override
public long getMoo2() {
return moo2;
}

@Override
public void setMoo2(long moo2) {
this.moo2 = moo2;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

TestClass2 that = (TestClass2) o;

if (moo1 != that.moo1) return false;
if (moo2 != that.moo2) return false;
if (text1 != null ? !text1.equals(that.text1) : that.text1 != null) return false;
return text2 != null ? text2.equals(that.text2) : that.text2 == null;

}

@Override
public int hashCode() {
int result = text1 != null ? text1.hashCode() : 0;
result = 31 * result + (text2 != null ? text2.hashCode() : 0);
result = 31 * result + moo1;
result = 31 * result + (int) (moo2 ^ (moo2 >>> 32));
return result;
}
}
}

0 comments on commit e0b5650

Please sign in to comment.