I have a method that returns a List<Object>
from any given Object
containing its values. It doesn't get the values of any static
or final
field in the class.
public List<Object> objectData(Object object) {
Field[] fields = object.getClass().getDeclaredFields();
Set<String> realFields = new LinkedHashSet<String>();
for(Field field : fields) {
if (!(java.lang.reflect.Modifier.isStatic(field.getModifiers()) ||
java.lang.reflect.Modifier.isFinal(field.getModifiers()))) {
realFields.add(field.getName());
}
}
List<Object> row = new ArrayList<Object>();
for(String realField : realFields) {
Field currentField = object.getClass().getDeclaredField(realField);
currentField.setAccessible(true);
row.add(currentField.get(object));
}
return row;
}
Is this the right approach? Isn't something else I can be missing?