Link to home
Create AccountLog in
Avatar of COMPSUPP
COMPSUPP

asked on

PropertyDescriptor getReadMethod and then use that method

Hello,

I am trying to use PropertyDescriptor to find the getter method for a bean property given the property name as a String.

The code below seems to do this. Is it possible to then use the getter method in question to retrieve its value for the bean (if its value is not null?)

Is there an easy tutorial on PropertyDescriptor and its uses out there somewhere ? Could someone please point me in the right direction?

thanks
try{
          PropertyDescriptor p = new PropertyDescriptor("propertyNameAsAString", MyDataBean.class);
          if(p.getReadMethod() != null){
              System.out.println("answer " + p.getReadMethod());
              
          }
         }catch(Exception ex){
             System.err.println("caught");
         }

Open in new window

SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of COMPSUPP
COMPSUPP

ASKER

okay, this might sound really silly, but how do I then use that method?

If I print out I get this: public java.lang.String DataBeans.ObservationDataBean.getPropertyNameHere()

How do I then get the value of the property ?

ASKER CERTIFIED SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Can I get the method using Method m = p.getReadMethod(); (as below)

And use that to get my String from its getter?

How can I invoke Method m ? (or do I have totally the wrong end of the stick here)
         try{
          PropertyDescriptor p = new PropertyDescriptor("fledglingUnsexed", BTObservationDataBean.class);
          if(p.getReadMethod() != null){
              System.out.println("answer " + p.getReadMethod());
              Method m = p.getReadMethod();
              // I want to use Method m to return the String from the getter
          }
         }catch(Exception ex){
             System.err.println("caught");
         }

Open in new window

I think I kind of figured it out. Is there anything hideously wrong (ie bad programming) about the code below?

CEHJ thanks for pointing me in the right direction.
         try{
          PropertyDescriptor p = new PropertyDescriptor("fledglingUnsexed", BTObservationDataBean.class);
          if(p.getReadMethod() != null){
              System.out.println("answer " + p.getReadMethod());
              Method m = p.getReadMethod();
              Object result = m.invoke(this, new Object[0]);
                  String value = (String)result;
                  System.out.println("value is " + value);
 
              
          }
         }catch(Exception ex){
             System.err.println("caught");
         }

Open in new window

:-)  Looks OK. Of course that way of handling exceptions won't tell you anything though ...