Tuesday, July 27, 2010

Dynamically Populating Java Beans with some data

Very often one needs to populate a bean with some dummy data. It's EXTREMELY boring to do it manually.

This is only the first cut of a utility to do it in an automated way:



TAGD result = new TAGD();

Map properties = BeanUtils.describe(result);

for (Object property : properties.keySet()) {

BeanUtils.setProperty(result, property.toString(), property.toString());

}

return result;



This works only if you have only strings in the bean.

BeanUtils is from Apache Commons.

1 comment:

Mahmoud Ben Hassine said...

There is jPopulator that can do that easily :

https://github.com/benas/jPopulator

Hope it helps.