2
\$\begingroup\$

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?


EDIT: The method above was adapted from this one. I'm assuming Collection<T> data is a valid non-empty collection.

public List<List<Object>> getData(Collection<T> data) {
    try {
        Field[] fields = data.iterator().next().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<List<Object>> realData = new ArrayList<List<Object>>();
        for(T element : data) {
            List<Object> row = new ArrayList<Object>();
            for(String realField : realFields) {
                Field currentField = element.getClass().getDeclaredField(realField);
                currentField.setAccessible(true);
                row.add(currentField.get(element));
            }
            realData.add(row);
        }
    } catch (Exception e) {
        //basic error handling, this will be improved;
        throw new RuntimeException(e);
    }
}
\$\endgroup\$
1
\$\begingroup\$

In principle, your code is correct (provided you really do not want to deal with fields in superclass). But it can be optimized: inside the first loop, you already have fields so there is no need to save their names in a collection and retrieve later - just use them in place.

\$\endgroup\$
  • \$\begingroup\$ The above method was adapted from another method that returns a List<List<Object> from a given Collection<Object>. I'll update the code to show the other method. \$\endgroup\$ – Luiggi Mendoza Sep 23 '13 at 19:10
  • \$\begingroup\$ Question updated. \$\endgroup\$ – Luiggi Mendoza Sep 23 '13 at 19:13
  • \$\begingroup\$ Then what? Another method is also suboptimal, though in a different way. \$\endgroup\$ – Alexei Kaigorodov Sep 23 '13 at 19:20
  • \$\begingroup\$ Can you explain please? \$\endgroup\$ – Luiggi Mendoza Sep 23 '13 at 19:34
  • \$\begingroup\$ realFields should be of type Field[], not Set<String>. \$\endgroup\$ – Alexei Kaigorodov Sep 24 '13 at 4:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.