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/25 22:28:49 UTC

svn commit: r1535854 - in /commons/proper/beanutils/branches/java5/src: main/java/org/apache/commons/beanutils/ConvertUtilsBean.java test/java/org/apache/commons/beanutils/ConvertUtilsTestCase.java

Author: oheger
Date: Fri Oct 25 20:28:49 2013
New Revision: 1535854

URL: http://svn.apache.org/r1535854
Log:
Generified ConvertUtilsBean.

Note that the current version of the convert() methods
is not really satisfying because the generic Object type is returned.
However, we cannot return an object of the passed in target class because
the methods' behavior is to return the passed in object if no suitable
converter is found. Maybe we can add a new method which enforces the conversion
or throws an exception; this method could be type-safe.

Modified:
    commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtilsBean.java
    commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/ConvertUtilsTestCase.java

Modified: commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtilsBean.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtilsBean.java?rev=1535854&r1=1535853&r2=1535854&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtilsBean.java (original)
+++ commons/proper/beanutils/branches/java5/src/main/java/org/apache/commons/beanutils/ConvertUtilsBean.java Fri Oct 25 20:28:49 2013
@@ -145,7 +145,8 @@ public class ConvertUtilsBean {
      * The set of {@link Converter}s that can be used to convert Strings
      * into objects of a specified Class, keyed by the destination Class.
      */
-    private final WeakFastHashMap converters = new WeakFastHashMap();
+    private final WeakFastHashMap<Class<?>, Converter> converters =
+            new WeakFastHashMap<Class<?>, Converter>();
 
     /**
      * The <code>Log</code> instance for this class.
@@ -454,11 +455,11 @@ public class ConvertUtilsBean {
                 return null;
             } else {
                 Converter converter = lookup(String.class);
-                return ((String) converter.convert(String.class, value));
+                return (converter.convert(String.class, value));
             }
         } else {
             Converter converter = lookup(String.class);
-            return ((String) converter.convert(String.class, value));
+            return (converter.convert(String.class, value));
         }
 
     }
@@ -468,13 +469,14 @@ public class ConvertUtilsBean {
      * Convert the specified value to an object of the specified class (if
      * possible).  Otherwise, return a String representation of the value.
      *
+     * @param <T> The desired target type of the conversion
      * @param value Value to be converted (may be null)
      * @param clazz Java class to be converted to (must not be null)
      * @return The converted value
      *
      * @exception ConversionException if thrown by an underlying Converter
      */
-    public Object convert(String value, Class clazz) {
+    public Object convert(String value, Class<?> clazz) {
 
         if (log.isDebugEnabled()) {
             log.debug("Convert string '" + value + "' to class '" +
@@ -505,9 +507,9 @@ public class ConvertUtilsBean {
      *
      * @exception ConversionException if thrown by an underlying Converter
      */
-    public Object convert(String[] values, Class clazz) {
+    public Object convert(String[] values, Class<?> clazz) {
 
-        Class type = clazz;
+        Class<?> type = clazz;
         if (clazz.isArray()) {
             type = clazz.getComponentType();
         }
@@ -532,8 +534,9 @@ public class ConvertUtilsBean {
 
 
     /**
-     * <p>Convert the value to an object of the specified class (if
-     * possible).</p>
+     * Convert the value to an object of the specified class (if
+     * possible). If no converter for the desired target type is registered,
+     * the passed in object is returned unchanged.
      *
      * @param value Value to be converted (may be null)
      * @param targetType Class of the value to be converted to (must not be null)
@@ -541,9 +544,9 @@ public class ConvertUtilsBean {
      *
      * @exception ConversionException if thrown by an underlying Converter
      */
-    public Object convert(Object value, Class targetType) {
+    public Object convert(Object value, Class<?> targetType) {
 
-        Class sourceType = value == null ? null : value.getClass();
+        Class<?> sourceType = value == null ? null : value.getClass();
 
         if (log.isDebugEnabled()) {
             if (value == null) {
@@ -563,7 +566,7 @@ public class ConvertUtilsBean {
             }
             converted = converter.convert(targetType, value);
         }
-        if (targetType == String.class && converted != null &&
+        if (String.class.equals(targetType) && converted != null &&
                 !(converted instanceof String)) {
 
             // NOTE: For backwards compatibility, if the Converter
@@ -789,9 +792,9 @@ public class ConvertUtilsBean {
      * value used in the event of a conversion error
      * @param defaultArraySize The size of the default array
      */
-    private void registerArrayConverter(Class componentType, Converter componentConverter,
+    private void registerArrayConverter(Class<?> componentType, Converter componentConverter,
             boolean throwException, int defaultArraySize) {
-        Class arrayType = Array.newInstance(componentType, 0).getClass();
+        Class<?> arrayType = Array.newInstance(componentType, 0).getClass();
         Converter arrayConverter = null;
         if (throwException) {
             arrayConverter = new ArrayConverter(arrayType, componentConverter);
@@ -802,7 +805,7 @@ public class ConvertUtilsBean {
     }
 
     /** strictly for convenience since it has same parameter order as Map.put */
-    private void register(Class clazz, Converter converter) {
+    private void register(Class<?> clazz, Converter converter) {
         register(new ConverterFacade(converter), clazz);
     }
 
@@ -812,7 +815,7 @@ public class ConvertUtilsBean {
      *
      * @param clazz Class for which to remove a registered Converter
      */
-    public void deregister(Class clazz) {
+    public void deregister(Class<?> clazz) {
 
         converters.remove(clazz);
 
@@ -827,9 +830,9 @@ public class ConvertUtilsBean {
      * @param clazz Class for which to return a registered Converter
      * @return The registered {@link Converter} or <code>null</code> if not found
      */
-    public Converter lookup(Class clazz) {
+    public Converter lookup(Class<?> clazz) {
 
-        return ((Converter) converters.get(clazz));
+        return (converters.get(clazz));
 
     }
 
@@ -842,7 +845,7 @@ public class ConvertUtilsBean {
      * @param targetType Class of the value to be converted to
      * @return The registered {@link Converter} or <code>null</code> if not found
      */
-    public Converter lookup(Class sourceType, Class targetType) {
+    public Converter lookup(Class<?> sourceType, Class<?> targetType) {
 
         if (targetType == null) {
             throw new IllegalArgumentException("Target type is missing");
@@ -888,7 +891,7 @@ public class ConvertUtilsBean {
      * @param clazz Destination class for conversions performed by this
      *  Converter
      */
-    public void register(Converter converter, Class clazz) {
+    public void register(Converter converter, Class<?> clazz) {
 
         converters.put(clazz, converter);
 

Modified: commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/ConvertUtilsTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/ConvertUtilsTestCase.java?rev=1535854&r1=1535853&r2=1535854&view=diff
==============================================================================
--- commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/ConvertUtilsTestCase.java (original)
+++ commons/proper/beanutils/branches/java5/src/test/java/org/apache/commons/beanutils/ConvertUtilsTestCase.java Fri Oct 25 20:28:49 2013
@@ -24,11 +24,13 @@ import java.sql.Timestamp;
 import java.text.DateFormat;
 import java.text.SimpleDateFormat;
 import java.util.Locale;
-import org.apache.commons.beanutils.converters.DateConverter;
-import junit.framework.TestCase;
+
 import junit.framework.Test;
+import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
+import org.apache.commons.beanutils.converters.DateConverter;
+
 
 /**
  * <p>
@@ -614,6 +616,8 @@ public class ConvertUtilsTestCase extend
 
     }
 
+    @SuppressWarnings({ "unchecked", "rawtypes" })
+    // We need to use raw types in order to test legacy converters
     public void testConvertToString() throws Exception {
         Converter dummyConverter = new Converter() {
             public Object convert(Class type, Object value) {
@@ -658,6 +662,16 @@ public class ConvertUtilsTestCase extend
 
     }
 
+    /**
+     * Tests a conversion to an unsupported target type.
+     */
+    public void testConvertUnsupportedTargetType() {
+        ConvertUtilsBean utils = new ConvertUtilsBean();
+        Object value = "A test value";
+        assertSame("Got different object", value,
+                utils.convert(value, getClass()));
+    }
+
     // -------------------------------------------------------- Private Methods