views:

1062

answers:

2

I have method which gets a POJO as it's parameter. Now I want to programmatically get all the attributes of the POJO (because my code may not know what are all the attributes in it at run time) and need to get the values for the attributes also. Finally I'll form a string representation of the POJO.

I could use ToStringBuilder, but I want build my output string in certain format specific to my requirement.

Is it possible to do so in Beanutils !? If yes, any pointers to the method name? If no, should I write my own reflection code?

+1  A: 

Have you tried ReflectionToStringBuilder? It looks like is should do what you describe.

John Meagher
yes. it gives me the string representation of my POJO. but, i want the string format to be in different style. For example, instead of having com.demo.MyPojo@a33425[name=foo,desc=bar], I want the string to be "[name=foo;desc=bar;]"
Veera
Perhaps this version: http://commons.apache.org/lang/api-release/org/apache/commons/lang/builder/ReflectionToStringBuilder.html#toString(java.lang.Object,%20org.apache.commons.lang.builder.ToStringStyle)
John Meagher
Hmm, the link above didn't copy correctly. There is a version that takes the object and a ToStringStyle that should allow you to customize the output.
John Meagher
A: 

get all properties/variables ( just the name ) using reflection. Now use getProperty method to get the value of that variable

Rakesh Juyal
yes. i think I'll have to take this approach.
Veera