如何查找Java bean中包含的所有成员变量的字段 [英] how find fields of all member variables contained in java bean

查看:291
本文介绍了如何查找Java bean中包含的所有成员变量的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Java制作GUI,用户可以在其中选择一个bean,编辑其字段,然后将创建的bean的实例添加到队列中.我的问题是关于访问字段.我有一个继承自MyParentObject的类MyCompositeObject. MyParentObject由多个bean组成,每个bean由更多的bean组成.类MyCompositeObject也由bean组成.我想从MyCompositeObject中找到所有可访问的字段.

I want to make a GUI using Java in which a user can select a bean, edit its fields, and then add an instance of the created bean to a queue. My question though is about accessing the fields. I have a class MyCompositeObject that inherits from MyParentObject. The MyParentObject is composed of multiple beans, each being composed of more beans. The class MyCompositeObject is also composed of beans. I want to find all accessible fields from MyCompositeObject.

Class MyParentObject
{
    MyObjectOne fieldOne;
    MyObjectTwo fieldTwo;
    String name;
  ...
 }

 Class MyCompositeObject extends MyParentObject
 {
    MyObjectThree fieldThree;
    Integer number;
   ...
 }

 Class MyObjectThree
 {
     boolean aBoolean;
     MyObjectFour fieldFour;
   ...
 }

我一直在尝试使用BeanUtils api,但在尝试获取所有成员bean的字段时遇到了麻烦.我想象的是对可以从MyCompositeObject实例访问的所有字段进行深度优先搜索.例如,这将包括但不限于以下字段:MyCompositeObject.fieldOne,MyCompositeObject.number,MyCompositeObject.fieldThree.aBoolean.

I have been trying to use the BeanUtils api, but I'm getting stuck trying to get the fields of all the member beans. What I am imagining is a depth first search of all fields that could be accessed from an instance of MyCompositeObject. For example, this would include, but not be limited to, the fields: MyCompositeObject.fieldOne, MyCompositeObject.number, MyCompositeObject.fieldThree.aBoolean.

我意识到当我尝试过:

Fields[] allFields = BeanUtils.getFields(myCompositeObject);

我在头顶上.到目前为止,我的研究还没有发现任何可以完成我所描述的方法的预置方法.请让我知道可以执行此操作的任何API方法,或者告诉我如何进行自己的构建.谢谢.

that I was in over my head. My research has so far not turned up any prebuilt methods that could do what I describe. Please let me know of any API methods that can do this or tell me how I can go about building my own. Thanks.

推荐答案

这有点痛苦,但您必须在两个方面进行尝试

It's kind of a pain but you have to go in two dimensions

yourBeanClass.getSuperclass(); (and recursively get all superclasses until Object)

然后您可以获取每个字段的内容

and then you can get the fields of each one

eachClass.getDeclaredFields() NOT getFields so you can get all the private fields

一旦您拥有每个字段

field.getType() which returns the Class of that field

然后,当然,您需要再次上载超类链,以确保您获得了该类的所有字段,包括超类中的所有字段

then of course, you need to go up that dudes superclass chain again to make sure you get ALL the fields of the class including the ones in the superclass

一旦有了该字段的类链,您就可以通过重复上面的操作来获取它的字段....是的,jdk令这很有趣!!!我真希望他们有一个getAllDeclaredFields方法,这样我就不必上超类继承.

Once you have that chain of classes for that field, you can then get it's fields by repeating the above....yes, the jdk made this fun!!!! I wish to god they had a getAllDeclaredFields method so I didn't have to go up the superclass heirarchy.

重要提示:您需要调用field.setAccessible(true),以便当它是私有字段时可以对其进行读写!!!

IMPORTANT: you need to call field.setAccessible(true) so you can read and write to it when it is a private field by the way!!!

这是获取一个类的所有字段(包括超类)的代码.

Here is code that gets all the fields for a Class including the superclasses..

private static List<Field> findAllFields(Class<?> metaClass) {
    List<Field[]> fields = new ArrayList<Field[]>();
    findFields(metaClass, fields);

    List<Field> allFields = new ArrayList<Field>();
    for(Field[] f : fields) {
        List<Field> asList = Arrays.asList(f);
        allFields.addAll(asList);
    }
    return allFields;
}

private static void findFields(Class metaClass2, List<Field[]> fields) {
    Class next = metaClass2;
    while(true) {
        Field[] f = next.getDeclaredFields();
        fields.add(f);
        next = next.getSuperclass();
        if(next.equals(Object.class))
            return;
    }
}

后来, 院长

这篇关于如何查找Java bean中包含的所有成员变量的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆