有没有比java反射api更好的获取对象的字段getter的方法,或者我在使用PropertyDescriptor的getReadMethod? [英] Is there a better way of obtaining an object's field getters other than java reflection api or i am misusing PropertyDescriptor's getReadMethod?

查看:161
本文介绍了有没有比java反射api更好的获取对象的字段getter的方法,或者我在使用PropertyDescriptor的getReadMethod?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上下文:

我正在使用从SOAP服务端点接收的数据以通用方式构建Excel文档。我以List的形式接收数据,并根据调用的方法为我收到的每个Object提供模型(JavaBeans)。
所以我将工作表的第一行设置为对象字段(getDeclaredFields)的标题。
然后我继续用对象列表中的值逐行填充列。

I am building an Excel document in a generic way with data i receive from a SOAP service endpoint. I receive the data as a List and i have the model (JavaBeans) for every Object i receive according to the method called. So I set the first row of the sheet as the header from the object's fields (getDeclaredFields). Then i go on filling up the column row by row with values from the list of objects.

问题:

我还没有找到一种可行的方法来获取对象的字段值。
我尝试过使用带有java反射API的getter,其答案如下: https://stackoverflow.com/a/ 5503534/4807777 findGetterName,findGetter但是PropertyDescriptor的getName有时是与从类的getDeclaredFields获得的字段名称不同的字母大小写。

I haven't found a workable way of getting the object's field values. I have tried using the getters with the java reflection API with something like this answer's https://stackoverflow.com/a/5503534/4807777 findGetterName , findGetter however the PropertyDescriptor's getName sometimes is a different letter case from the field name as obtained from the class's getDeclaredFields.

假设我通过大写两个名称来克服这个问题,getReadMethod stil失败 - 似乎找不到使用is前缀的字段的getter(即布尔字段) 。我不知道我是否在滥用它或者它是一个错误(调试getReadMethod似乎只能使用get前缀,即使它似乎处理boole的is前缀情况)。

Let's say i overcome this by capitalizing both names, the getReadMethod stil fails - doesn't seem to find getters for the fields which use the is prefix (i.e boolean fields). I don't know if i am misusing it or it is a bug (debugging the getReadMethod appears to only work with the get prefix, even though it appears to handle the is prefix case for booleans).

考虑到这个字段在对象包之外是不可访问的,因此只能通过调用getter来实现。

Considering the fact the fields aren't accesible outside of the object's package, therefore solely through invoking getters.

是否有更好的获取方式对象的字段getter或我缺少getter方法的东西?

Is there a better way of obtaining the object's field getters or i am missing something with the getter methods?



更新:Spring的BeanUtils似乎更适合用它来获取属性当JavaBean属性映射到XML元素时, getPropertyDescriptors 优于java Class的 getDeclaredFields


Update: Spring's BeanUtils seems to be better for getting the properties with it's getPropertyDescriptors is better than java Class's getDeclaredFields, when the JavaBean properties are mapped to XML elements.

这修复了不同的信件情况。然而,当不使用get前缀时,它不会发现它是readMethod。

This fixes the different letter cases situation. However it stil doesn't find it's readMethod when not using the get prefix.



已编辑 - 显示getReadMethod未找到的示例Laszlo Lugosi要求的前缀getter。


Edited - to show an example of getReadMethod not finding the is prefixed getter, as Laszlo Lugosi requested.

一个简单的类:

        class Test {
            private String assignmentType;
            private Boolean conserved;
            public String getAssignmentType() {return assignmentType;}
            public void setAssignmentType(String assignmentType) {this.assignmentType = assignmentType;}
            public Boolean isConserved() {return conserved;}
            public void setConserved(Boolean conserved) {this.conserved = conserved;}
        }

使用上面链接的答案中的findGetter和findGetterName运行此命令:

Run this with the findGetter and findGetterName written in the answer linked above:

{
    Test obj = new Test();
                obj.setAssignmentType("someType");
                obj.setConserved(true);
                Field[] fields = obj.getClass().getDeclaredFields();
                String fieldName;
                for (int i=0;i<fields.length;i++){
                    fieldName = fields[i].getName();
                    java.lang.reflect.Method method;
                    Object val = null;
                    try {
                        method = obj.getClass().getMethod(findGetterName(obj.getClass(),fieldName));
                        val = method.invoke(obj);
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                }
}



编辑2
虽然我可以简单地按照约定写一个getReadMethod Laszlo Lugosi突出显示我更喜欢找到一个用于处理访问器的API。


Edited 2 While i could simply write a getReadMethod following the convention Laszlo Lugosi highlighted i do prefer finding an API for handling accessors.

推荐答案

由于您只知道对象字段名称,并且JavaBean具有约定,因此您可以轻松地找出getter。如果field是boolean,则规则是getUpperfieldname()和isUpperfieldname。您还可以从对象字段中找到返回类型。

As you know only the object field name, and JavaBean has convention, you can figure out the getters easily. The rules are getUpperfieldname() and isUpperfieldname if field is boolean. And you can find out the return type as well from the object field.

这篇关于有没有比java反射api更好的获取对象的字段getter的方法,或者我在使用PropertyDescriptor的getReadMethod?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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