1. opensymphony
  2. xwork

Commits

cameronbraid  committed c73635a

Issue number: WW-271
Bug in converter that used String.class instead of sourceObject.class when converting to string, causing sourceObject.toString() to be called instead of using the sourceObject.class converter.
Addition of programatic registration of converters.
Addition of programatic specification of a default converter to use as the fallback converter - when a converter isn't found in the registry.

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@49e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits a864a10
  • Branches master

Comments (0)

Files changed (1)

File src/java/com/opensymphony/xwork/util/XWorkConverter.java

View file
  • Ignore whitespace
         //
         // Process the conversion using the default mappings, if one exists
         //
-        if (defaultMappings.containsKey(toClass.getName())) {
-            TypeConverter tc = null;
+        TypeConverter tc = null;
 
-            if (toClass.equals(String.class) && !(value.getClass().equals(String.class) || value.getClass().equals(String[].class))) {
-                // converting ToString from NON String 
+        if (toClass.equals(String.class) && !(value.getClass().equals(String.class) || value.getClass().equals(String[].class))) {
+            // when converting to a string, use the source object's class's converter
+            if (defaultMappings.containsKey(value.getClass().getName())) {
                 tc = (TypeConverter) defaultMappings.get(value.getClass().getName());
-            } else {
+            }
+        } else {
+            // when converting from a string, use the toClass's converter
+            if (defaultMappings.containsKey(toClass.getName())) {
                 //	converting from String
                 tc = (TypeConverter) defaultMappings.get(toClass.getName());
             }
+        }
 
-            if (tc != null) {
-                try {
-                    return tc.convertValue(context, object, member, property, value, toClass);
-                } catch (Exception e) {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.error("Conversion failed", e);
-                    }
-
-                    return null;
+        if (tc != null) {
+            try {
+                return tc.convertValue(context, object, member, property, value, toClass);
+            } catch (Exception e) {
+                if (LOG.isDebugEnabled()) {
+                    LOG.error("Conversion failed", e);
                 }
+
+                return null;
             }
         }