I have a list of beans stored in an ArrayList and I'm iterating the list to get each bean out of it. I have a String value to be checked inside the bean. I don't know what the field name of the String value to search in the bean. For eg: The bean might have some three fields say AA, BB, CC.
My string value might be matching to bean.getAA()/bean.getBB() or bean.getCC(). Now, since I don't know the name of the fields inside the bean, I cannot use getter methods.
I just stored this bean object to a Collection reference and executed a reference.contains(String value). But, I'm not sure this method is going to check the value.
Let me know if my solution would work fine or any other thoughts/comments.
posted 8 years ago
With raw reflection you can get a list of all the methods on an object. You can find the ones that start with "get" and return a String and call them. It's mildly tricky but not that hard. Start with
Class c = object.getClass();
then look in the JavaDoc for the Class class for methods like getMethod() (that's not doubletalk, honest).
There are also some utilities built around Beans that I've personally never used. Look up Introspector and BeanInfo. Those may be significantly easier than the reflection path.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi