Skip to content

Commit

Permalink
Implement superclass traversal for all JOhm operations.
Browse files Browse the repository at this point in the history
This patch is a fix for Issue xetorthio#11 (https://github.com/xetorthio/johm/issues#issue/11);
Support mapping of attributes in all classes of the inheritance hierarchy.
  • Loading branch information
gsharma authored and Jonathan Leibiusky committed Dec 26, 2010
1 parent d928201 commit e8ae80e
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 372 deletions.
25 changes: 4 additions & 21 deletions src/main/java/redis/clients/johm/JOhm.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -64,11 +63,7 @@ public static <T> T get(Class<?> clazz, int id) {
JOhmUtils.initCollections(newInstance, nest);

Map<String, String> hashedObject = nest.cat(id).hgetAll();
for (Field field : clazz.getDeclaredFields()) {
fillField(hashedObject, newInstance, field);
fillArrayField(nest, newInstance, field);
}
for (Field field : clazz.getSuperclass().getDeclaredFields()) {
for (Field field : JOhmUtils.gatherAllFields(clazz)) {
fillField(hashedObject, newInstance, field);
fillArrayField(nest, newInstance, field);
}
Expand Down Expand Up @@ -159,14 +154,10 @@ public static <T> T save(final Object model, boolean saveChildren) {
final Nest nest = initIfNeeded(model);

final Map<String, String> hashedObject = new HashMap<String, String>();
List<Field> fields = new ArrayList<Field>();
fields.addAll(Arrays.asList(model.getClass().getDeclaredFields()));
fields.addAll(Arrays.asList(model.getClass().getSuperclass()
.getDeclaredFields()));
Map<RedisArray<Object>, Object[]> pendingArraysToPersist = null;
try {
String fieldName = null;
for (Field field : fields) {
for (Field field : JOhmUtils.gatherAllFields(model.getClass())) {
field.setAccessible(true);
if (JOhmUtils.detectJOhmCollection(field)
|| field.isAnnotationPresent(Id.class)) {
Expand Down Expand Up @@ -273,11 +264,7 @@ public static boolean delete(Class<?> clazz, int id, boolean deleteIndexes,
// think about promoting deleteChildren as default behavior so
// that this field lookup gets folded into that
// if-deleteChildren block
List<Field> fields = new ArrayList<Field>();
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
fields.addAll(Arrays.asList(clazz.getSuperclass()
.getDeclaredFields()));
for (Field field : fields) {
for (Field field : JOhmUtils.gatherAllFields(clazz)) {
if (field.isAnnotationPresent(Indexed.class)) {
field.setAccessible(true);
Object fieldValue = null;
Expand All @@ -300,11 +287,7 @@ public static boolean delete(Class<?> clazz, int id, boolean deleteIndexes,
}
}
if (deleteChildren) {
List<Field> fields = new ArrayList<Field>();
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
fields.addAll(Arrays.asList(clazz.getSuperclass()
.getDeclaredFields()));
for (Field field : fields) {
for (Field field : JOhmUtils.gatherAllFields(clazz)) {
if (field.isAnnotationPresent(Reference.class)) {
field.setAccessible(true);
try {
Expand Down
Loading

0 comments on commit e8ae80e

Please sign in to comment.