commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ohe...@apache.org
Subject svn commit: r1516574 - in /commons/proper/configuration/trunk/src: main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
Date Thu, 22 Aug 2013 19:59:46 GMT
Author: oheger
Date: Thu Aug 22 19:59:46 2013
New Revision: 1516574

URL: http://svn.apache.org/r1516574
Log:
Fixed a problem with conversions to primitive data types.

In this case, the Class.cast() method cannot be used.

Modified:
    commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
    commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java

Modified: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java?rev=1516574&r1=1516573&r2=1516574&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
(original)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/convert/DefaultConversionHandler.java
Thu Aug 22 19:59:46 2013
@@ -280,8 +280,12 @@ public class DefaultConversionHandler im
             return null;
         }
 
-        return targetCls.cast(PropertyConverter.to(targetCls, src,
-                this));
+        // This is a safe cast because PropertyConverter either returns an
+        // object of the correct class or throws an exception.
+        @SuppressWarnings("unchecked")
+        T result = (T) PropertyConverter.to(targetCls, src,
+               this);
+        return result;
     }
 
     /**

Modified: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java?rev=1516574&r1=1516573&r2=1516574&view=diff
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
(original)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/convert/TestDefaultConversionHandler.java
Thu Aug 22 19:59:46 2013
@@ -339,4 +339,15 @@ public class TestDefaultConversionHandle
         assertEquals("Wrong minute", 17, cal.get(Calendar.MINUTE));
         assertEquals("Wrong second", 22, cal.get(Calendar.SECOND));
     }
+
+    /**
+     * Tests a conversion to a primitive type.
+     */
+    @Test
+    public void testToPrimitive()
+    {
+        Long value = 20130819214935L;
+        Object result = handler.to(value.toString(), Long.TYPE, null);
+        assertEquals("Wrong conversion result", value, result);
+    }
 }



Mime
View raw message