通过泛型类迭代

时间:2014-04-11 19:04:26

标签: java android reflection iteration

有没有可能打印出类中每个字段的名称和类型?现在我有以下代码

try {
   for (Field field : obj.getClass().getDeclaredFields()) {
   System.out.println("User Preferences " + field.getName()
                     + " - " + field.getType());
   }
}catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
}

显示:

User Preferences customer - class java.lang.String
User Preferences token - class java.lang.String
User Preferences class1 - class com.activity.webservice.Login$Class1

这就是我想要的(差不多)。

问题是让它进入自定义类Class1。 Class1类可以包含多个类,也可以没有类。 Class1类可以有多个字段,也可以没有字段。

我想要这样的东西打印出来

User Preferences customer - class java.lang.String
User Preferences token - class java.lang.String
User Preferences class1 - class com.activity.webservice.Login$Class1
User Preferences string1 - class java.lang.String
User Preferences class2 - class com.activity.webservice.Login$Class2
User Preferences string2 - class java.lang.String
User Preferences string3 - class java.lang.String

如何迭代未知数量的字段和类?

感谢您的帮助

修改

以下是我的Login类的结构

public class Login {

     public String token;   
     public String customerid;
     public Class1 class1;

     public class Class1 {
            public Class2 class2;
            public String string1;

            public class Class2 {
                    public int int1;
                    public String string2;
                    public String string3;
            }
     }
}

2 个答案:

答案 0 :(得分:2)

以下是如何递归执行此操作的一般概念。只需传入你想要字段的类,它也应该递归地检查所有子类的字段。

// Call this method to get the list of fields.
public static List<String> getAllFields(Class<?> clazz) {
    List<String> results = new ArrayList<String>();
    getFieldDescsForClass(clazz, results);
    return results;
}

private static void getFieldDescsForClass(Class<?> clazz, List<String> outList) {
    // Loop over all the fields and add the info for each field
    for (Field field : clazz.getDeclaredFields()) {
        // Ignore synthetic fields
        if (!field.isSynthetic()) {
            outList.add(String.format("User Preferences %s - %s", field.getName(), field.getType()));
        }
    }

    // For any internal classes, recursively call this method and add the results
    // (which will in turn do this for all of that subclass's subclasses)
    for (Class subclazz : clazz.getDeclaredClasses()) {
        getFieldDescsForClass(subclazz, outList);
    }
}

答案 1 :(得分:1)

public class Test {
static List structure = new ArrayList();

static void getClassStructure(Class clazz, List structure){
    Field[] fields = clazz.getDeclaredFields();
    for(Field f : fields){
        structure.add(f.getType());
    }

    Class[] classes = clazz.getDeclaredClasses();
    for(Class c : classes){
        getClassStructure(c, structure);
    }
}

public static void main(String[] args) {
    Class clazz = new Login().getClass();
    getClassStructure(clazz, structure);
    System.out.println(structure);

}

}

上测试过
class Login {

public String token;   
public String customerid;
public Class1 class1;

public class Class1 {
       public Class2 class2;
       public String string1;

       public class Class2 {
               public int int1;
               public String string2;
               public String string3;
       }
}

}

输出:

class java.lang.String

class java.lang.String

类登录$ Class1

课程登录$ Class1 $ Class2

class java.lang.String

课程登录

INT

class java.lang.String

class java.lang.String

类登录$ Class1