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
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");
}
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
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)
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");
}
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.
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");
}
:-) Looks OK. Of course that way of handling exceptions won't tell you anything though ...
ASKER
If I print out I get this: public java.lang.String DataBeans.ObservationDataB
How do I then get the value of the property ?