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/10/30 21:16:39 UTC
svn commit: r1537266 - in
/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils:
ConvertUtils.java converters/AbstractConverter.java
Author: oheger
Date: Wed Oct 30 20:16:39 2013
New Revision: 1537266
URL: http://svn.apache.org/r1537266
Log:
Extracted a method for converting primitive types to wrapper types.
This functionality is needed by both normal and locale converters. Therefore,
it is useful to have it centrally available.
Modified:
commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtils.java
commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/AbstractConverter.java
Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtils.java?rev=1537266&r1=1537265&r2=1537266&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtils.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtils.java Wed Oct 30 20:16:39 2013
@@ -352,4 +352,42 @@ public class ConvertUtils {
ConvertUtilsBean.getInstance().register(converter, clazz);
}
+
+ /**
+ * Change primitive Class types to the associated wrapper class. This is
+ * useful for concrete converter implementations which typically treat
+ * primitive types like their corresponding wrapper types.
+ *
+ * @param type The class type to check.
+ * @return The converted type.
+ * @since 1.9
+ */
+ // All type casts are safe because the TYPE members of the wrapper types
+ // return their own class.
+ @SuppressWarnings("unchecked")
+ public static <T> Class<T> primitiveToWrapper(Class<T> type) {
+ if (type == null || !type.isPrimitive()) {
+ return type;
+ }
+
+ if (type == Integer.TYPE) {
+ return (Class<T>) Integer.class;
+ } else if (type == Double.TYPE) {
+ return (Class<T>) Double.class;
+ } else if (type == Long.TYPE) {
+ return (Class<T>) Long.class;
+ } else if (type == Boolean.TYPE) {
+ return (Class<T>) Boolean.class;
+ } else if (type == Float.TYPE) {
+ return (Class<T>) Float.class;
+ } else if (type == Short.TYPE) {
+ return (Class<T>) Short.class;
+ } else if (type == Byte.TYPE) {
+ return (Class<T>) Byte.class;
+ } else if (type == Character.TYPE) {
+ return (Class<T>) Character.class;
+ } else {
+ return type;
+ }
+ }
}
Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/AbstractConverter.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/AbstractConverter.java?rev=1537266&r1=1537265&r2=1537266&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/AbstractConverter.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/converters/AbstractConverter.java Wed Oct 30 20:16:39 2013
@@ -21,6 +21,7 @@ import java.util.Collection;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConversionException;
+import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -133,7 +134,7 @@ public abstract class AbstractConverter
}
Class<?> sourceType = value == null ? null : value.getClass();
- Class<T> targetType = primitive(type);
+ Class<T> targetType = ConvertUtils.primitiveToWrapper(type);
if (log().isDebugEnabled()) {
log().debug("Converting"
@@ -408,40 +409,6 @@ public abstract class AbstractConverter
}
/**
- * Change primitive Class types to the associated wrapper class.
- * @param type The class type to check.
- * @return The converted type.
- */
- // All type casts are safe because the TYPE members of the wrapper types
- // return their own class.
- @SuppressWarnings("unchecked")
- <T> Class<T> primitive(Class<T> type) {
- if (type == null || !type.isPrimitive()) {
- return type;
- }
-
- if (type == Integer.TYPE) {
- return (Class<T>) Integer.class;
- } else if (type == Double.TYPE) {
- return (Class<T>) Double.class;
- } else if (type == Long.TYPE) {
- return (Class<T>) Long.class;
- } else if (type == Boolean.TYPE) {
- return (Class<T>) Boolean.class;
- } else if (type == Float.TYPE) {
- return (Class<T>) Float.class;
- } else if (type == Short.TYPE) {
- return (Class<T>) Short.class;
- } else if (type == Byte.TYPE) {
- return (Class<T>) Byte.class;
- } else if (type == Character.TYPE) {
- return (Class<T>) Character.class;
- } else {
- return type;
- }
- }
-
- /**
* Provide a String representation of a <code>java.lang.Class</code>.
* @param type The <code>java.lang.Class</code>.
* @return The String representation.