You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/11/30 17:22:52 UTC
svn commit: r1546736 - in /commons/proper/beanutils/trunk/src:
main/java/org/apache/commons/beanutils/BeanUtilsBean.java
test/java/org/apache/commons/beanutils/bugs/Jira454TestCase.java
Author: oheger
Date: Sat Nov 30 16:22:52 2013
New Revision: 1546736
URL: http://svn.apache.org/r1546736
Log:
[BEANUTILS-454] BeanUtilsBean.copyProperties() handles null values differently.
No longer a ConversionException is thrown for null values for certain data
types. A special handling for null values was added in the copy method.
Added:
commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira454TestCase.java
Modified:
commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java
Modified: commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java?rev=1546736&r1=1546735&r2=1546736&view=diff
==============================================================================
--- commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java (original)
+++ commons/proper/beanutils/trunk/src/main/java/org/apache/commons/beanutils/BeanUtilsBean.java Sat Nov 30 16:22:52 2013
@@ -410,7 +410,7 @@ public class BeanUtilsBean {
// Convert the specified value to the required type and store it
if (index >= 0) { // Destination must be indexed
- value = convert(value, type.getComponentType());
+ value = convertForCopy(value, type.getComponentType());
try {
getPropertyUtils().setIndexedProperty(target, propName,
index, value);
@@ -430,7 +430,7 @@ public class BeanUtilsBean {
(e, "Cannot set " + propName);
}
} else { // Destination must be simple
- value = convert(value, type);
+ value = convertForCopy(value, type);
try {
getPropertyUtils().setSimpleProperty(target, propName, value);
} catch (NoSuchMethodException e) {
@@ -1076,6 +1076,19 @@ public class BeanUtilsBean {
}
/**
+ * Performs a type conversion of a property value before it is copied to a target
+ * bean. This method delegates to {@link #convert(Object, Class)}, but <b>null</b>
+ * values are not converted. This causes <b>null</b> values to be copied verbatim.
+ *
+ * @param value the value to be converted and copied
+ * @param type the target type of the conversion
+ * @return the converted value
+ */
+ private Object convertForCopy(Object value, Class<?> type) {
+ return (value != null) ? convert(value, type) : value;
+ }
+
+ /**
* Returns a <code>Method<code> allowing access to
* {@link Throwable#initCause(Throwable)} method of {@link Throwable},
* or <code>null</code> if the method
Added: commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira454TestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira454TestCase.java?rev=1546736&view=auto
==============================================================================
--- commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira454TestCase.java (added)
+++ commons/proper/beanutils/trunk/src/test/java/org/apache/commons/beanutils/bugs/Jira454TestCase.java Sat Nov 30 16:22:52 2013
@@ -0,0 +1,50 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.beanutils.bugs;
+
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.beanutils.BeanUtils;
+
+/**
+ * copyProperties() throws a ConversionException : No value specified for 'Date'
+ * when the field is a java.util.Date with a null value
+ *
+ * @version $Id: $
+ */
+public class Jira454TestCase extends TestCase {
+ public void testCopyProperties() throws Exception {
+ TestBean bean = new TestBean();
+ TestBean b2 = new TestBean();
+ BeanUtils.copyProperties(b2, bean);
+ assertNull("Got a creation date", b2.getCreatedAt());
+ }
+
+ public static class TestBean {
+ private Date createdAt;
+
+ public Date getCreatedAt() {
+ return createdAt;
+ }
+
+ public void setCreatedAt(Date createdAt) {
+ this.createdAt = createdAt;
+ }
+ }
+}