简体   繁体   English

如何通过帮助反射获取通用字段类型(字符串表示形式)-Java?

[英]How to get generic field type (string representation) with help reflection - Java?

There are some fields: 有一些字段:

public class Class1 {
    private Map<String,Integer> field1 = new HashMap<String,Integer>();
    private int someField = 1;
    private int[] myIntArray = new int[]{1,2,3};
    private List<ArrayList<String>> words = null;
    private ArrayList<String>[] group = (ArrayList<String>[])new ArrayList[4];
    private List<List<List<ArrayList<List<List<String>>>>>> lists = null;
}

Is there a simple way to get string representation of each of these (and generally any) types? 是否有一种简单的方法来获取每种(通常是任何一种)类型的字符串表示形式?

Try this: 尝试这个:

for (Field field : Class1.class.getDeclaredFields())
    System.out.println(field.getGenericType());

Output: 输出:

java.util.Map<java.lang.String, java.lang.Integer>
int
class [I
java.util.List<java.util.ArrayList<java.lang.String>>
java.util.ArrayList<java.lang.String>[]
java.util.List<java.util.List<java.util.List<java.util.ArrayList<java.util.List<java.util.List<java.lang.String>>>>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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