简体   繁体   中英

Java - Parse List<Field> into String

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.

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(","));

The output format is like that as Field class toString method implemented like below:

public String toString() {
    int mod = getModifiers();
    return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
        + getType().getTypeName() + " "
        + getDeclaringClass().getTypeName() + "."
        + getName());
}

Note: Class & Field class both are final so you can't override it also.

So, you can't change the toString definition of Field. But what you can build your own String like below:

String s = result.stream().map(e -> e.getDeclaringClass().getSimpleName()).collect(Collectors.joining(", "));
System.out.println(s);

You can make use of the getSimpleName() . For eg:

String.class.getSimpleName();

In your example all you need to do is :

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

So your method will now look like :

public static String 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();
    }

    return result.stream()
                 .map(field -> field.getType().getSimpleName())
                 .collect(Collectors.joining(", "));
}

You can use Field.getType() to get the Class of the Field , then use Class.getSimpleName() in order to get only the part you want :

List<String> types = new ArrayList<>();
Field[] declaredFields = Person.class.getDeclaredFields();
for (Field declaredField : declaredFields) {
    // Will give [class java.lang.String, class java.lang.String, int]
    // types.add(declaredField.getType().toString()); 

    // Will give [String, String, int]
    types.add(declaredField.getType().getSimpleName()); 
}
System.out.println(types);

[String, String, int]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM