Java - Parse List<Field> into String

John Beltran :

I'm trying to turn a List<Field> into simple strings, but I'm not sure how to parse them out of the array into a single String variable.

I'm currently using a method that goes through a class that uses a getDeclaredFields() method and outputs them into an array.

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

    Class<?> i = type;
    while (i != null && i != Object.class) {
        Collections.addAll(result, i.getDeclaredFields());
        i = i.getSuperclass();
    }

    for(Field temp : result) {
        System.out.println(temp);
    }

    return result;
}

The result I get when I output it is

[private int com.gen.e.ric.report.domain.TestCount.count,
 private java.lang.String com.gen.e.ric.report.domain.Test.facility, 
 private java.lang.String com.gen.e.ric.report.domain.Test.technology,
 private java.lang.String com.gen.e.ric.report.domain.Test.component, 
 private java.util.List com.gen.e.ric.report.domain.Test.etestStats, 
 private java.util.Date com.gen.e.ric.report.domain.Test.startDate, 
 private java.util.Date com.gen.e.ric.report.domain.Test.endDate]

What I want to get is just a long string with

int, String, String, List, Date, Date

Any help to give? Using Reflection btw. Thanks.

f1sh :

You only want to output each Field's type, so do that:

for(Field temp : result) {
    System.out.println(temp.getType());
}

or in order to put it into a String:

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

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=111212&siteId=1