File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to search a string value inside a Bean class

 
Prasanna Kannan
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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.

Thanks,
Prasanna
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic