Skip to content

Commit

Permalink
Enable simple persistence of superclass fields - no uniqueness checki…
Browse files Browse the repository at this point in the history
…ng as yet
  • Loading branch information
fullwall committed Apr 6, 2020
1 parent 321a53b commit d87a9e8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Expand Up @@ -4,6 +4,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Iterator;
Expand Down Expand Up @@ -286,13 +287,19 @@ private static Persister<?> getDelegate(Field field, Class<?> fallback) {

private static Field[] getFields(Class<?> clazz) {
Field[] fields = fieldCache.get(clazz);
if (fields == null)
if (fields == null) {
fieldCache.put(clazz, fields = getFieldsFromClass(clazz));
}
return fields;
}

private static Field[] getFieldsFromClass(Class<?> clazz) {
List<Field> toFilter = Lists.newArrayList(clazz.getDeclaredFields());
Class<?> superClass = clazz.getSuperclass();
while (superClass != Object.class && superClass != null) {
toFilter.addAll(Arrays.asList(superClass.getDeclaredFields()));
superClass = superClass.getSuperclass();
}
Iterator<Field> itr = toFilter.iterator();
while (itr.hasNext()) {
Field field = itr.next();
Expand Down
Expand Up @@ -119,6 +119,19 @@ public void testMapReload() {
assertThat(PersistenceLoader.load(TestMap.class, root).enabled.get("trig1"), equalTo(true));
}

@Test
public void testSuperClass() {
root.setInt("integer", 5);
root.setString("superclass", "test");
SuperclassTest test = PersistenceLoader.load(SuperclassTest.class, root);
assertEquals(test.superclass, "test");
assertEquals(test.integer, 5);
root = new MemoryDataKey();
PersistenceLoader.save(test, root);
assertEquals(root.getString("superclass"), "test");
assertEquals(root.getInt("integer"), 5);
}

@Test
public void testTypeInference() {
root.setString("map.1", "1");
Expand Down Expand Up @@ -192,6 +205,16 @@ public static class SpecificCollectionClassTest {
private Set<Integer> set;
}

public static class Superclass {
@Persist
public String superclass;
}

public static class SuperclassTest extends Superclass {
@Persist
public int integer;
}

public static class TestMap {
@Persist(value = "enabled", collectionType = ConcurrentHashMap.class)
private final Map<String, Boolean> enabled = new ConcurrentHashMap<String, Boolean>();
Expand Down

0 comments on commit d87a9e8

Please sign in to comment.