递归读取任何 java 对象并将复杂类型提取到哈希映射中

发布于 2024-10-07 17:33:35 字数 519 浏览 0 评论 0原文

我需要编写一个实用程序,它接受一个空白的 HashMap 和任何对象作为参数,并返回 HashMap。

public HashMap returnMap(HashMap map,Object parseThisObject){

//logic to strip all children, children of children...... and place it in HashMap
//return map

}

这个对象包含很多对象,其中的对象有很多子对象,并且血统继续下去。

我的实用程序必须足够通用,才能递归地读取所有子对象,直到它命中每个对象中的基元,将每个对象放入搭扣映射中并将其返回。 这就像父母会出现在地图上一样。但各个孩子也会作为地图中的后续条目出现。

我是java反射的新手,我在网上浏览了一些教程和示例。对如何进行不太有信心。我相信这是这里的专家和专业人士可能面临的常见要求之一。

请帮助我了解这一点。是否有任何开源 Bean 实用程序可用于执行此操作?如果是这样,请告诉我。

I need to write a utility program which would accept a blank HashMap and any object as arguments and return the HashMap

public HashMap returnMap(HashMap map,Object parseThisObject){

//logic to strip all children, children of children...... and place it in HashMap
//return map

}

This object contains a lot of objects within it and those objects within has a lot of children and the lineage goes on.

My utility must be generic enough to recursively read through all children until it hits primitives in each object, place each of those objects in the hasp map and return it back.
This is something like the parent would be there in the map. but the individual children also would be there as sub sequent entries in the map.

I am new to java reflection and I went through some tutorials and examples on the net. Not very confident about how to proceed. I believe this is one of the frequent requirements experts and professionals over here might have faced.

Plese help me with a starting point on this. If there is any bean utilities open source available to do this? if so please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌吟 2024-10-14 17:33:35

像这样的事情应该可以做到:

public void fillMap(HashMap<String, Object> map, Object bean) {
    try {
        BeanInfo info = Introspector.getBeanInfo(bean.getClass());
        PropertyDescriptor[] props = info.getPropertyDescriptors();
        for (int i = 0; i < props.length; i++) {
            Method reader = props[i].getReadMethod();
            if (reader != null && !props[i].getName().equals("class")) {
                Object invoke = reader.invoke(bean, new Object[] {});
                if (invoke != null) {
                    if (!reader.getReturnType().isPrimitive()) {
                        fillMap(map, invoke);
                    } else {
                        map.put(props[i].getName(), invoke);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这当然会将所有对象的所有字段放入一张地图中。如果您想要子映射,您可能必须为每个递归步骤创建一个映射。如果您需要的话,我也可以给您该代码。

没有返回映射,因为传递给该方法的映射已被填充。

Something like this should do it:

public void fillMap(HashMap<String, Object> map, Object bean) {
    try {
        BeanInfo info = Introspector.getBeanInfo(bean.getClass());
        PropertyDescriptor[] props = info.getPropertyDescriptors();
        for (int i = 0; i < props.length; i++) {
            Method reader = props[i].getReadMethod();
            if (reader != null && !props[i].getName().equals("class")) {
                Object invoke = reader.invoke(bean, new Object[] {});
                if (invoke != null) {
                    if (!reader.getReturnType().isPrimitive()) {
                        fillMap(map, invoke);
                    } else {
                        map.put(props[i].getName(), invoke);
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

this of course puts all fields from all objects into one map. You might have to create a map for each recursion step if you want submaps for children. I can give you that code as well if you need it.

There is no return of a map, because the one that is passed to the method is filled.

想挽留 2024-10-14 17:33:35

查看 Apache Commons BeanUtils。它已经完成了您需要的大部分工作,甚至可以一次性完成所有工作。

Have a look at Apache Commons BeanUtils. It already does a large chunk of what you need, and may even do the whole lot in one go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文