commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1104457 - in /incubator/ognl/trunk/src/main/java/org/apache/commons/ognl: DefaultTypeConverter.java TypeConverter.java
Date Tue, 17 May 2011 18:48:34 GMT
Author: simonetripodi
Date: Tue May 17 18:48:33 2011
New Revision: 1104457

URL: http://svn.apache.org/viewvc?rev=1104457&view=rev
Log:
used generics in target type to auto-magic-cast converted values

Modified:
    incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/DefaultTypeConverter.java
    incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/TypeConverter.java

Modified: incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/DefaultTypeConverter.java?rev=1104457&r1=1104456&r2=1104457&view=diff
==============================================================================
--- incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/DefaultTypeConverter.java (original)
+++ incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/DefaultTypeConverter.java Tue
May 17 18:48:33 2011
@@ -19,6 +19,8 @@ package org.apache.commons.ognl;
  * under the License.
  */
 
+import static java.lang.String.format;
+
 import java.lang.reflect.Member;
 import java.util.Map;
 
@@ -36,13 +38,26 @@ public class DefaultTypeConverter
         super();
     }
 
-    public Object convertValue( Map context, Object value, Class toType )
+    public <T> T convertValue( Map context, Object value, Class<T> toType )
     {
-        return OgnlOps.convertValue( value, toType );
+        Object ret = OgnlOps.convertValue( value, toType );
+
+        if ( ret == null )
+        {
+            return null;
+        }
+
+        if ( !toType.isAssignableFrom( ret.getClass() ) )
+        {
+            throw new ClassCastException( format( "value '%s' can not be coverted to class
'%s'",
+                                                  ( value != null ? value : "null" ), toType.getName()
) );
+        }
+
+        return toType.cast( ret );
     }
 
-    public Object convertValue( Map context, Object target, Member member, String propertyName,
Object value,
-                                Class toType )
+    public <T> T convertValue( Map context, Object target, Member member, String propertyName,
Object value,
+                                Class<T> toType )
     {
         return convertValue( context, value, toType );
     }

Modified: incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/TypeConverter.java
URL: http://svn.apache.org/viewvc/incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/TypeConverter.java?rev=1104457&r1=1104456&r2=1104457&view=diff
==============================================================================
--- incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/TypeConverter.java (original)
+++ incubator/ognl/trunk/src/main/java/org/apache/commons/ognl/TypeConverter.java Tue May
17 18:48:33 2011
@@ -45,6 +45,6 @@ public interface TypeConverter
      * @return Converted value of type toType or TypeConverter.NoConversionPossible to indicate
that the conversion was
      *         not possible.
      */
-    Object convertValue( Map context, Object target, Member member, String propertyName,
Object value, Class toType );
+    <T> T convertValue( Map context, Object target, Member member, String propertyName,
Object value, Class<T> toType );
 
 }



Mime
View raw message