18

I have the following bean class:

public class A{
        private String field;

        public String getField() {
            return field;
        }

        private String setField(String field) {
            this.field = field;
        }

    }

And the following class:

    public class B{

         public static void main(String[] args){
             A a = new A();
             //do stuff
             String f = //get a's field value
         }
    }

How can I get the value returned by the getter of a particular object of class A? Of course, I can invoke method with Method.invoke(Object obj, Object... args) but I wouldn't like to write "get" prefix manually. Is it possible to avoid that?

user3663882
  • 6,957
  • 10
  • 51
  • 92

2 Answers2

57

How about using java.beans.PropertyDescriptor (assuming your property has correct setter and getter - so in your case you need to correct your private String setField(String field) setter since (a) its return type should be void not String, (b) it should be public not private).

Object f = new PropertyDescriptor("field", A.class).getReadMethod().invoke(a);

or little longer version (which does exactly same as previous one)

PropertyDescriptor pd = new PropertyDescriptor("field", A.class);
Method getter = pd.getReadMethod();
Object f = getter.invoke(a);

PropertyDescriptor allows us to do many things, for instance its getReadMethod()

Gets the method that should be used to read the property value.

So we can get instance of java.reflect.Method representing getter for field. All we need to do now is simply invoke it on bean from which we want to get result.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Great, thank you. Couldn't you amplify your answer with some normative refernces. – user3663882 Feb 24 '15 at 13:57
  • Read the PropertyDescriptor documentation : `PropertyDescriptor(String propertyName, Class> beanClass)` *Constructs a PropertyDescriptor for a property that follows the standard Java convention by having getFoo and setFoo accessor methods*. – Jean-Baptiste Yunès Feb 24 '15 at 14:02
  • 1
    @Jean-BaptisteYunès Will the getter be invoked if we don't have a corresponding filed and setter, just `getField()`? – user3663882 Feb 24 '15 at 15:39
  • Yes. You can have readonly, writeonly, readwrite properties. Only methods are necessary, you may not have an associated field. – Jean-Baptiste Yunès Feb 24 '15 at 15:43
  • Superb example. I was wandering everywhere to find the most simplistic solution. This helped me at last. – AnirbanDebnath Aug 17 '16 at 16:32
  • Very simple, succinct and complete. Thank you. – shivesh verma Mar 08 '18 at 08:24
  • 1
    This is a good solution but as @user3663882 suspected, the constructor throws an IntrospectionException if any of the getter/setter is absent. A better solution is to use `Introspector.getBeanInfo(...).getPropertyDescriptors()` as suggested [here](https://stackoverflow.com/a/2638662) - it works fine even when only either is present. – ManSamVampire Apr 14 '23 at 14:11
8

Another easy way is to use the basic java reflection.

Method fieldGetter = A.getClass().getMethod("getField");
String f = fieldGetter.invoke(A).toString();

As simple as that. Cheers !!

AnirbanDebnath
  • 990
  • 16
  • 26