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

Java

Avatar of undefined
Last Comment
CEHJ

8/22/2022 - Mon
SOLUTION
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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
CEHJ

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
COMPSUPP

ASKER
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

COMPSUPP

ASKER
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

Your help has saved me hundreds of hours of internet surfing.
fblack61
CEHJ

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