Skip to content

Commit

Permalink
Some progress on #149: Make ObjectField versions for primitive types …
Browse files Browse the repository at this point in the history
…work in the same way as AsmCacheField and UnsafeCacheField. All of them optimize for speed and ignore any custom registered serializers for primitive types for now.
  • Loading branch information
romix committed Nov 27, 2013
1 parent adf0576 commit a137238
Showing 1 changed file with 264 additions and 3 deletions.
267 changes: 264 additions & 3 deletions src/com/esotericsoftware/kryo/serializers/ObjectField.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void setField (Object object, Object value) throws IllegalArgumentExcepti
field.set(object, value);
}

final public void write (Output output, Object object) {
public void write (Output output, Object object) {
try {
// if(typeVar2concreteClass != null) {
// // Push a new scope for generics
Expand Down Expand Up @@ -88,7 +88,7 @@ final public void write (Output output, Object object) {
}
}

final public void read (Input input, Object object) {
public void read (Input input, Object object) {
try {
if (TRACE) trace("kryo", "Read field: " + this + " (" + type.getName() + ")" + " pos=" + input.position());
Object value;
Expand Down Expand Up @@ -154,71 +154,332 @@ final static class ObjectIntField extends ObjectField {
public ObjectIntField (FieldSerializer fieldSerializer) {
super(fieldSerializer);
}

public Object getField (Object object) throws IllegalArgumentException, IllegalAccessException {
return field.getInt(object);
}

public void write (Output output, Object object) {
try {
if (varIntsEnabled)
output.writeInt(field.getInt(object), false);
else
output.writeInt(field.getInt(object));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void read (Input input, Object object) {
try {
if (varIntsEnabled)
field.setInt(object, input.readInt(false));
else
field.setInt(object, input.readInt());
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void copy (Object original, Object copy) {
try {
field.setInt(copy, field.getInt(original));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}
}

final static class ObjectFloatField extends ObjectField {
public ObjectFloatField (FieldSerializer fieldSerializer) {
super(fieldSerializer);
}

public Object getField (Object object) throws IllegalArgumentException, IllegalAccessException {
return field.getFloat(object);
}

public void write (Output output, Object object) {
try {
output.writeFloat(field.getFloat(object));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void read (Input input, Object object) {
try {
field.setFloat(object, input.readFloat());
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void copy (Object original, Object copy) {
try {
field.setFloat(copy, field.getFloat(original));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}
}

final static class ObjectShortField extends ObjectField {
public ObjectShortField (FieldSerializer fieldSerializer) {
super(fieldSerializer);
}

public Object getField (Object object) throws IllegalArgumentException, IllegalAccessException {
return field.getShort(object);
}

public void write (Output output, Object object) {
try {
output.writeShort(field.getShort(object));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void read (Input input, Object object) {
try {
field.setShort(object, input.readShort());
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void copy (Object original, Object copy) {
try {
field.setShort(copy, field.getShort(original));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}
}

final static class ObjectByteField extends ObjectField {
public ObjectByteField (FieldSerializer fieldSerializer) {
super(fieldSerializer);
}

public Object getField (Object object) throws IllegalArgumentException, IllegalAccessException {
return field.getByte(object);
}

public void write (Output output, Object object) {
try {
output.writeByte(field.getByte(object));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void read (Input input, Object object) {
try {
field.setByte(object, input.readByte());
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void copy (Object original, Object copy) {
try {
field.setByte(copy, field.getByte(original));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}
}

final static class ObjectBooleanField extends ObjectField {
public ObjectBooleanField (FieldSerializer fieldSerializer) {
super(fieldSerializer);
}

public Object getField (Object object) throws IllegalArgumentException, IllegalAccessException {
return field.getBoolean(object);
}

public void write (Output output, Object object) {
try {
output.writeBoolean(field.getBoolean(object));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void read (Input input, Object object) {
try {
field.setBoolean(object, input.readBoolean());
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void copy (Object original, Object copy) {
try {
field.setBoolean(copy, field.getBoolean(original));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}
}

final static class ObjectCharField extends ObjectField {
public ObjectCharField (FieldSerializer fieldSerializer) {
super(fieldSerializer);
}

public Object getField (Object object) throws IllegalArgumentException, IllegalAccessException {
return field.getChar(object);
}

public void write (Output output, Object object) {
try {
output.writeChar(field.getChar(object));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void read (Input input, Object object) {
try {
field.setChar(object, input.readChar());
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void copy (Object original, Object copy) {
try {
field.setChar(copy, field.getChar(original));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}
}

final static class ObjectLongField extends ObjectField {
public ObjectLongField (FieldSerializer fieldSerializer) {
super(fieldSerializer);
}

public Object getField (Object object) throws IllegalArgumentException, IllegalAccessException {
return field.getLong(object);
}

public void write (Output output, Object object) {
try {
if (varIntsEnabled)
output.writeLong(field.getLong(object), false);
else
output.writeLong(field.getLong(object));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void read (Input input, Object object) {
try {
if (varIntsEnabled)
field.setLong(object, input.readLong(false));
else
field.setLong(object, input.readLong());
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void copy (Object original, Object copy) {
try {
field.setLong(copy, field.getLong(original));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}
}

final static class ObjectDoubleField extends ObjectField {
public ObjectDoubleField (FieldSerializer fieldSerializer) {
super(fieldSerializer);
}

public Object getField (Object object) throws IllegalArgumentException, IllegalAccessException {
return field.getDouble(object);
}

public void write (Output output, Object object) {
try {
output.writeDouble(field.getDouble(object));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void read (Input input, Object object) {
try {
field.setDouble(object, input.readDouble());
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}

public void copy (Object original, Object copy) {
try {
field.setDouble(copy, field.getDouble(original));
} catch (Exception e) {
KryoException ex = new KryoException(e);
ex.addTrace(this + " (" + type.getName() + ")");
throw ex;
}
}
}
}

0 comments on commit a137238

Please sign in to comment.