i have a generic method in java to get fields value by reflection.
private static Object getFieldValue(Object object, final String fieldName) {
if (null == object) {
return null;
}
Class<?> clazz = object.getClass();
while (clazz != null) {
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
try {
return field.get(object);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
} catch (NoSuchFieldException e) {
clazz = clazz.getSuperclass();
}
}
return null;
}
it works fine for simple fields name, exemple getfieldValue (project, "title")
, i want to get fields values of related objects for exemple
getfieldValue (project, "task.title")
Aucun commentaire:
Enregistrer un commentaire