You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Mingfai Ma <mi...@hongkong.com> on 2003/02/25 12:37:03 UTC
[beanutils] handle both simple object and array/list
hi,
I want to perform a "set" to JavaBean property without knowing whether it is
a simple object or an array/list of objects. e.g. if the property is a
String, then it calls setSimpleProperty, but if the property is a String[],
then it calls setIndexedProperty. Does the PropertyUtils intelligent enough
to do this task? which method should I use?
if not, am I correct that, in the current PropertyUtils, the way to achieve
my objective is to use getPropertyType, and then call two different set
methods?
do you think this is a useful addition to BeanUtils? (or am I too lazy to
wish this feature! :-) )
a test case as example is given at the bottom.
Regards,
mingfai
Example for illustrating the case:
Bean
----
public class MyBean{
private String[] myString1;
private String myString2;
//getters & setters here
}
Application
-----------
...
public void testMyBeanArrayMethod(){
MyBean myBean = new MyBean();
String strTest = "HELLO WORLD";
PropertyUtils.setSIMPLEorARRAYProperty( myBean, "myString1", strTest);
//add one more item to the array
String result = PropertyUtils.getSIMPLEorARRAYProperty( myBean,
"myString1"); //get from the last item
assertEquals( strTest, result);
}
public void testMyBeanSimpleMethod(){
MyBean myBean = new MyBean();
String strTest = "HELLO WORLD";
PropertyUtils.setSIMPLEorARRAYProperty( myBean, "myString2", strTest);
//simple setProperty
String result = PropertyUtils.getSIMPLEorARRAYProperty( myBean,
"myString2"); //simple getProperty
assertEquals( strTest, result);
}