File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes How to search a string value inside a Bean class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to search a string value inside a Bean class" Watch "How to search a string value inside a Bean class" New topic

How to search a string value inside a Bean class

Prasanna Kannan

Joined: Mar 07, 2005
Posts: 21

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.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
I agree. Here's the link:
subject: How to search a string value inside a Bean class
jQuery in Action, 3rd edition