使用反射获取所有Integer数据类型字段

时间:2014-08-28 10:31:22

标签: java reflection

我想使用其名称查找字段的数据类型。 我从\

获得了Type
Class<?> type = f.getType()

但无法确定哪个是整数或字符串或其他。 比较类型如何是整数或字符串或其他。

2 个答案:

答案 0 :(得分:2)

您可以使用<class>.class结构并比较类型。您可以使用getClass方法获取类型。

Class<?> type = f.getClass();
if (type == Integer.class) {
    // integer 
} else if (type == String.class) {
    // string
} else {
    // other
} 

答案 1 :(得分:0)

试试这个:

       if (f.getType().getName().equals(String.class.getName())) {
           System.out.println("String");
       }
       if (f.getType().getName().equals(Integer.class.getName())) {
           System.out.println("Integer");
       }