Is there a better way to get object fields other than the java reflection api, or am I abusing PropertyDescriptor getReadMethod?

Context:

I am creating a generic Excel document with data retrieved from a SOAP service endpoint. I am receiving data as a list and I have a model (JavaBeans) for each object I receive according to the method called. So I am setting the first row of the sheet as the title from the object fields (getDeclaredFields). Then I continue to populate the columns row by row with values ​​from the list of objects.

Problem:

I haven't found a workable way to get the field values ​​of an object. I have tried using getters with the reflection java API with something like this answer on stackoverflow https://stackoverflow.com/questions/2306371/findGetterName, findGetter, however the getName PropertyDescriptor is sometimes a different literal case from the field name as obtained from the getDeclaredFields class ...

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

Considering the fact that the fields are not available outside of the object package, therefore only by calling getters.

Is there a better way to get the fields of an object object or am I missing something with getter methods?


Update: Spring BeanUtils seems to be better for getting properties with it. GetPropertyDescriptors is better than the java class of the getDeclaredFields class when JavaBean properties are mapped to XML elements.

This captures the situation with various letters. However, it doesn't find its readMethod unless the get prefix is ​​used.


Edited - Show an example of getReadMethod without finding the getter prefix as requested by Laszlo Lugosi.

Simple class:

        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;}
        }

      

Run this with 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();
                    }
                }
}

      


Edited 2 While I could just write getReadMethod according to Laszlo Lugosi's convention, I preferred to find an API to handle accessory.
0


source to share


1 answer


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



+1


source







All Articles