Fixed storage so that it copies fields from the entire object hierarchy

This commit is contained in:
nathan 2014-08-22 01:49:31 +02:00
parent d1fb857cc0
commit 9dfe590a80

View File

@ -25,8 +25,6 @@ import com.esotericsoftware.kryo.io.Output;
* Nothing spectacular about this storage -- it allows for persistent storage of objects to disk. * Nothing spectacular about this storage -- it allows for persistent storage of objects to disk.
*/ */
public class Storage { public class Storage {
// TODO: add snappy compression to storage objects??
private static final Logger logger = LoggerFactory.getLogger(Storage.class); private static final Logger logger = LoggerFactory.getLogger(Storage.class);
private static Map<File, Storage> storages = new HashMap<File, Storage>(1); private static Map<File, Storage> storages = new HashMap<File, Storage>(1);
@ -49,7 +47,6 @@ public class Storage {
throw new IllegalArgumentException("loadIntoObject cannot be null!"); throw new IllegalArgumentException("loadIntoObject cannot be null!");
} }
// if we load from a NEW storage at the same location as an ALREADY EXISTING storage, // if we load from a NEW storage at the same location as an ALREADY EXISTING storage,
// without saving the existing storage first --- whoops! // without saving the existing storage first --- whoops!
synchronized (storages) { synchronized (storages) {
@ -76,7 +73,7 @@ public class Storage {
storage.objectReference = new WeakReference(loadIntoObject); storage.objectReference = new WeakReference(loadIntoObject);
} }
if (source != null) { if (source != null && source != orig) {
copyFields(source, loadIntoObject); copyFields(source, loadIntoObject);
} }
} }
@ -294,7 +291,15 @@ public class Storage {
private static void copyFields(Object source, Object dest) { private static void copyFields(Object source, Object dest) {
Class<? extends Object> sourceClass = source.getClass(); Class<? extends Object> sourceClass = source.getClass();
Field[] destFields = dest.getClass().getDeclaredFields(); Class<? extends Object> destClass = dest.getClass();
if (sourceClass != destClass) {
throw new IllegalArgumentException("Source and Dest objects are not of the same class!");
}
// have to walk up the object hierarchy.
while (destClass != Object.class) {
Field[] destFields = destClass.getDeclaredFields();
for (Field destField : destFields) { for (Field destField : destFields) {
String name = destField.getName(); String name = destField.getName();
@ -334,6 +339,10 @@ public class Storage {
logger.error("Unable to copy field: {}", name, e); logger.error("Unable to copy field: {}", name, e);
} }
} }
destClass = destClass.getSuperclass();
sourceClass = sourceClass.getSuperclass();
}
} }
public static void shutdown() { public static void shutdown() {