output.writeInt(fields.length, true);
for (int i = 0, n = fields.length; i < n; i++)
output.writeString(fields[i].field.getName());
}
OutputChunked outputChunked = new OutputChunked(output, 1024);
for (int i = 0, n = fields.length; i < n; i++) {
CachedField cachedField = fields[i];
try {
if (TRACE) trace("kryo", "Write field: " + cachedField + " (" + object.getClass().getName() + ")");
Object value = cachedField.get(object);
if (value == null) {
kryo.writeClass(outputChunked, null);
outputChunked.endChunks();
continue;
}
Serializer serializer = cachedField.serializer;
if (cachedField.valueClass == null) {
Registration registration = kryo.writeClass(outputChunked, value.getClass());
if (serializer == null) serializer = registration.getSerializer();
if (cachedField.generics != null) serializer.setGenerics(kryo, cachedField.generics);
kryo.writeObject(outputChunked, value, serializer);
} else {
if (serializer == null) cachedField.serializer = serializer = kryo.getSerializer(cachedField.valueClass);
if (cachedField.generics != null) serializer.setGenerics(kryo, cachedField.generics);
if (cachedField.canBeNull)
kryo.writeObjectOrNull(outputChunked, value, serializer);
else {
if (value == null) {
throw new KryoException("Field value is null but canBeNull is false: " + cachedField + " ("
+ object.getClass().getName() + ")");
}
kryo.writeObject(outputChunked, value, serializer);
}
}
outputChunked.endChunks();
} catch (IllegalAccessException ex) {
throw new KryoException("Error accessing field: " + cachedField + " (" + object.getClass().getName() + ")", ex);
} catch (KryoException ex) {
ex.addTrace(cachedField + " (" + object.getClass().getName() + ")");
throw ex;