Question

For example, I have some classes with several fields, one of them is as below, I want to output all fields of its instance, however, I must get the mapping between the member name with getter methods, can I get it by reflection ?

public class MappingMemberWithGetterOrSetter {
private String memberOne;
private int memberTwo;

public String getMemberOne() {
    return memberOne;
}

public void setMemberOne(String memberOne) {
    this.memberOne = memberOne;
}

public int getMemberTwo() {
    return memberTwo;
}

public void setMemberTwo(int memberTwo) {
    this.memberTwo = memberTwo;
}
}
Was it helpful?

Solution

Maybe this is what you want. With the Introspector you can get the BeanInfo and this gives you access to the bean's PropertyDescriptors.

BeanInfo beanInfo = Introspector.getBeanInfo(MappingMemberWithGetterOrSetter.class, Object.class);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
    String propertyName = propertyDescriptor.getName();
    Method getter = propertyDescriptor.getReadMethod();
    Method setter = propertyDescriptor.getWriteMethod();
    Field field = MappingMemberWithGetterOrSetter.class
                .getDeclaredField(propertyName);
    System.out.println("Property " + propertyName + " field:  " + field);
    System.out.println("Getter " + getter);
    System.out.println("Setter " + setter);
}

OTHER TIPS

Is this what you want?

Class aClass = ...//obtain class object
Field[] methods = aClass.getFields(); 

Field field = ... //obtain field object
String fieldName = field.getName();

After that you can access getter and setter of the field via

Object value = field.get(objectInstance);

field.set(objetInstance, value);

where objectInstance is an instance of the class that owns the field.

A property in Java is defined by getter and/or setter. If you have a getLength method without parameter and with a return value then you have a readable property named length. If you have a method called setLength(Type value) then you have a writeable property called length. If you have both then the property is both readable and writable.

Fields are not involved in the definition of a Java property. How and if you back your properties with fields is up to you.

public long getDoubleLength() { return getLength()*2; }

constitutes a read-only property of name doubleLength. No field exists for this property.

So what you want is not possible in general. If you want to do it for specific own classes where you know this mapping exists or if you want to test other classes for this pattern then you can get methods and fields via reflection and check if fields with the property names exists.

But this may not work all the time. Some people use conventions like _member or mField for the field names. This is completely on par with the Java Beans definition where properties are defined by getters and setters but will break with the naive approach of matching names.

public void getMethods(Class<?> className)
{                   
         Method[] methods = className.getMethods();             
         for(Method method : methods)
            {
           if(method.getDeclaringClass().getName().equalsIgnoreCase("java.lang.Object") )
             {
                 //Do nothing
             }
             else
             {
                if(StringUtils.startsWith( method.getName(),"get"))
                    {   
                        //Here u ll get all get methods                         
                    }
             }                              
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top