commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mingfai Ma" <ming...@hongkong.com>
Subject [beanutils] handle both simple object and array/list
Date Tue, 25 Feb 2003 11:37:03 GMT
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);
	}


Mime
View raw message