I don't think we should do this in production code...
private static Map<String, Object> extractTheStuffIWanted(Object obj, String className, String... keys) {
Map<String, Object> resultMap = new HashMap<String, Object>();
List<Object> search = new LinkedList<Object>();
search.add(obj);
while (!search.isEmpty()) {
Object o = search.remove(0);
if (o.getClass().getName().contains(className)) {
for (String key : keys) {
try {
Field f = o.getClass().getDeclaredField(key);
f.setAccessible(true);
resultMap.put(key, f.get(o));
} catch (IllegalAccessException e) {
// nom nom nom
} catch (NoSuchFieldException e) {
// nom nom nom
}
}
break;
}
for (Field f : o.getClass().getDeclaredFields()) {
try {
f.setAccessible(true);
Object value = f.get(o);
if (value != null &&
!f.getType().isPrimitive()) {
search.add(value);
}
} catch (IllegalAccessException e) {
// nom nom nom
}
}
}
return resultMap;
}
No comments:
Post a Comment