jeudi 14 mars 2019

Java - Get the Content of List

I'm trying to get all variables from my domain classes and print them all out into a single string, but most of my classes have a List in them. My code right now just outputs the List instead of its contents.

public static List<Field> getInheritedPrivateFields(Class<?> type) {
    List<Field> result = new ArrayList<Field>();

    Class<?> i = type;
    while (i != null && i != Object.class) {
        for (Field field : i.getDeclaredFields()) {
            if (!field.isSynthetic()) {
                result.add(field);
            }
        }
        i = i.getSuperclass();
    }

    String names = result.stream().map(e->e.getName()).collect(Collectors.joining(", "));
    String types = result.stream().map(e->e.getType().getSimpleName().toUpperCase()).collect(Collectors.joining(", "));

    System.out.println(names + ", " + types);

    return result;
}

Currently outputting

count, facility, technology, component, testStats, INT, STRING, STRING, STRING, LIST

What I need is to expand the list so that it outputs

facility, technology, name, count, test, failCriteria, description, target
STRING, STRING, STRINNG, INTEGER, STRING, STRING, STRING, REAL

My current idea right now is that in my for loop, I can add an IF statement that checks whether the field is a List, then if would check that the parameter is and loop again on that class so that it would output all the variables connected to the initial class.

Is there a way to get the value of the class in List? Or is there a better way to do this?





Aucun commentaire:

Enregistrer un commentaire