rdonkin 2002/06/05 13:46:38 Modified: beanutils/src/java/org/apache/commons/beanutils BeanUtils.java beanutils/src/test/org/apache/commons/beanutils BeanUtilsTestCase.java Log: Committed 'NoSuchMethod for read only properties and NullPointer when populating primitives' patch submitted by Tomas Viberg. this is one of those patches that i thought long and hard about committing. this isn't a part of beanutils that i'm particularly familiar with but no one spoke up on the list when i asked so i'm going to back my judgement. if i've got it wrong, i'm sure someone will be kind enough to commit a correction. this patch changes the behaviour of two problematic setProperty issues. when a setProperty was called on a read only method, the previous behaviour was to throw a InvocationTargetException. this patch now returns (after logging). when an primitive property was set with a null, a NullPointerException was throw. now, the null is converted. Revision Changes Path 1.23 +25 -7 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java Index: BeanUtils.java =================================================================== RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v retrieving revision 1.22 retrieving revision 1.23 diff -u -r1.22 -r1.23 --- BeanUtils.java 17 May 2002 07:25:50 -0000 1.22 +++ BeanUtils.java 5 Jun 2002 20:46:38 -0000 1.23 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v 1.22 2002/05/17 07:25:50 jstrachan Exp $ - * $Revision: 1.22 $ - * $Date: 2002/05/17 07:25:50 $ + * $Header: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtils.java,v 1.23 2002/06/05 20:46:38 rdonkin Exp $ + * $Revision: 1.23 $ + * $Date: 2002/06/05 20:46:38 $ * * ==================================================================== * @@ -87,7 +87,7 @@ * @author Chris Audley * @author Rey François * @author Gregor Raıman - * @version $Revision: 1.22 $ $Date: 2002/05/17 07:25:50 $ + * @version $Revision: 1.23 $ $Date: 2002/06/05 20:46:38 $ */ public class BeanUtils { @@ -509,8 +509,14 @@ /** - * Set the specified property value, performing type conversions as - * required to conform to the type of the destination property. + * <p>Set the specified property value, performing type conversions as + * required to conform to the type of the destination property.</p> + * + * <p>If the property is read only then the method returns + * without throwing an exception.</p> + * + * <p>If <code>null</code> is passed into a property expecting a primitive value, + * then this will be converted as if it were a <code>null</code> string.</p> * * @param bean Bean on which setting is to be performed * @param name Property name (can be nested/indexed/mapped/combo) @@ -619,12 +625,24 @@ return; // Skip this property setter } if (descriptor instanceof MappedPropertyDescriptor) { + if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null) { + log.debug("Skipping read-only property"); + return; // Read-only, skip this property setter + } type = ((MappedPropertyDescriptor) descriptor). getMappedPropertyType(); } else if (descriptor instanceof IndexedPropertyDescriptor) { + if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null) { + log.debug("Skipping read-only property"); + return; // Read-only, skip this property setter + } type = ((IndexedPropertyDescriptor) descriptor). getIndexedPropertyType(); } else { + if (descriptor.getWriteMethod() == null) { + log.debug("Skipping read-only property"); + return; // Read-only, skip this property setter + } type = descriptor.getPropertyType(); } } @@ -652,7 +670,7 @@ newValue = value; } } else { // Value into scalar - if (value instanceof String) { + if (value instanceof String || (value == null && type.isPrimitive())) { newValue = ConvertUtils.convert((String) value, type); } else if (value instanceof String[]) { newValue = ConvertUtils.convert(((String[]) value)[0], 1.10 +11 -7 jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java Index: BeanUtilsTestCase.java =================================================================== RCS file: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v retrieving revision 1.9 retrieving revision 1.10 diff -u -r1.9 -r1.10 --- BeanUtilsTestCase.java 27 Apr 2002 23:11:23 -0000 1.9 +++ BeanUtilsTestCase.java 5 Jun 2002 20:46:38 -0000 1.10 @@ -1,7 +1,7 @@ /* - * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v 1.9 2002/04/27 23:11:23 craigmcc Exp $ - * $Revision: 1.9 $ - * $Date: 2002/04/27 23:11:23 $ + * $Header: /home/cvs/jakarta-commons/beanutils/src/test/org/apache/commons/beanutils/BeanUtilsTestCase.java,v 1.10 2002/06/05 20:46:38 rdonkin Exp $ + * $Revision: 1.10 $ + * $Date: 2002/06/05 20:46:38 $ * * ==================================================================== * @@ -95,7 +95,7 @@ * </ul> * * @author <a href="mailto:[EMAIL PROTECTED]">Geir Magnusson Jr.</a> - * @version $Revision: 1.9 $ + * @version $Revision: 1.10 $ */ public class BeanUtilsTestCase extends TestCase { @@ -472,10 +472,11 @@ map.put("doubleProperty", "432.0"); // floatProperty is left at 123.0 map.put("intProperty", "543"); - // longProperty is left at 321 + map.put("longProperty", null); map.put("shortProperty", "654"); // stringProperty is left at "This is a string" map.put("writeOnlyProperty", "New writeOnlyProperty value"); + map.put("readOnlyProperty", "New readOnlyProperty value"); BeanUtils.populate(bean, map); @@ -489,8 +490,8 @@ (float) 0.005); assertEquals("intProperty is 543", 543, bean.getIntProperty()); - assertEquals("longProperty is 321", - (long) 321, bean.getLongProperty()); + assertEquals("longProperty is 0", + (long) 0, bean.getLongProperty()); assertEquals("shortProperty is 654", (short) 654, bean.getShortProperty()); assertEquals("stringProperty is \"This is a string\"", @@ -498,6 +499,9 @@ assertEquals("writeOnlyProperty is \"New writeOnlyProperty value\"", "New writeOnlyProperty value", bean.getWriteOnlyPropertyValue()); + assertEquals("readOnlyProperty is \"Read Only String Property\"", + "Read Only String Property", + bean.getReadOnlyProperty()); } catch (IllegalAccessException e) { fail("IllegalAccessException");
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>