camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r760877 - /camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
Date Wed, 01 Apr 2009 13:11:32 GMT
Author: davsclaus
Date: Wed Apr  1 13:11:29 2009
New Revision: 760877

URL: http://svn.apache.org/viewvc?rev=760877&view=rev
Log:
CAMEL-1508: Fixed type converter picking wrong converter when there was a more specialized
converter (eg it went down the super path to fast)

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java?rev=760877&r1=760876&r2=760877&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
(original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/converter/DefaultTypeConverter.java
Wed Apr  1 13:11:29 2009
@@ -278,44 +278,55 @@
         // make sure we have loaded the converters
         checkLoaded();
 
-        // lets try the super classes of the from type
+        return doLookup(toType, fromType, false);
+    }
+
+    private TypeConverter doLookup(Class toType, Class fromType, boolean isSuper) {
+
         if (fromType != null) {
-            Class fromSuperClass = fromType.getSuperclass();
-            if (fromSuperClass != null && !fromSuperClass.equals(Object.class)) {
+            // lets try if there is a direct match
+            TypeConverter converter = getTypeConverter(toType, fromType);
+            if (converter != null) {
+                return converter;
+            }
 
-                TypeConverter converter = getTypeConverter(toType, fromSuperClass);
-                if (converter == null) {
-                    converter = lookup(toType, fromSuperClass);
-                }
+            // try the interfaces
+            for (Class type : fromType.getInterfaces()) {
+                converter = getTypeConverter(toType, type);
                 if (converter != null) {
                     return converter;
                 }
             }
-            for (Class type : fromType.getInterfaces()) {
-                TypeConverter converter = getTypeConverter(toType, type);
+
+            // try super then
+            Class fromSuperClass = fromType.getSuperclass();
+            if (fromSuperClass != null && !fromSuperClass.equals(Object.class)) {
+                converter = doLookup(toType, fromSuperClass, true);
                 if (converter != null) {
                     return converter;
                 }
             }
+        }
 
-            // lets test for arrays
-            if (fromType.isArray() && !fromType.getComponentType().isPrimitive())
{
-                // TODO can we try walking the inheritance-tree for the element types?
-                if (!fromType.equals(Object[].class)) {
-                    fromSuperClass = Object[].class;
-
-                    TypeConverter converter = getTypeConverter(toType, fromSuperClass);
-                    if (converter == null) {
-                        converter = lookup(toType, fromSuperClass);
-                    }
-                    if (converter != null) {
-                        return converter;
+        // only do these tests as fallback and only on the target type (eg not on its super)
+        if (!isSuper) {
+            if (fromType != null && !fromType.equals(Object.class)) {
+
+                // lets try classes derived from this toType
+                Set<Map.Entry<TypeMapping, TypeConverter>> entries = typeMappings.entrySet();
+                for (Map.Entry<TypeMapping, TypeConverter> entry : entries) {
+                    TypeMapping key = entry.getKey();
+                    Class aToType = key.getToType();
+                    if (toType.isAssignableFrom(aToType)) {
+                        Class aFromType = key.getFromType();
+                        // skip Object based we do them last
+                        if (!aFromType.equals(Object.class) && aFromType.isAssignableFrom(fromType))
{
+                            return entry.getValue();
+                        }
                     }
                 }
-            }
 
-            // lets test for Object based converters
-            if (!fromType.equals(Object.class)) {
+                // lets test for Object based converters as last resort
                 TypeConverter converter = getTypeConverter(toType, Object.class);
                 if (converter != null) {
                     return converter;
@@ -323,21 +334,7 @@
             }
         }
 
-        // lets try classes derived from this toType
-        if (fromType != null) {
-            Set<Map.Entry<TypeMapping, TypeConverter>> entries = typeMappings.entrySet();
-            for (Map.Entry<TypeMapping, TypeConverter> entry : entries) {
-                TypeMapping key = entry.getKey();
-                Class aToType = key.getToType();
-                if (toType.isAssignableFrom(aToType)) {
-                    if (key.getFromType().isAssignableFrom(fromType)) {
-                        return entry.getValue();
-                    }
-                }
-            }
-        }
-
-        // TODO look at constructors of toType?
+        // none found
         return null;
     }
 



Mime
View raw message