aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1690252 - in /aries/trunk/blueprint/blueprint-core/src: main/java/org/apache/aries/blueprint/container/AggregateConverter.java test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java
Date Fri, 10 Jul 2015 12:46:53 GMT
Author: gnodet
Date: Fri Jul 10 12:46:53 2015
New Revision: 1690252

URL: http://svn.apache.org/r1690252
Log:
[ARIES-1333] Make sure collections are not transformed if they are compatible

Modified:
    aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java
    aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java

Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java?rev=1690252&r1=1690251&r2=1690252&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java
(original)
+++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java
Fri Jul 10 12:46:53 2015
@@ -329,38 +329,51 @@ public class AggregateConverter implemen
         if (obj.getClass().isArray()) {
             for (int i = 0; i < Array.getLength(obj); i++) {
                 try {
-                    newCol.add(convert(Array.get(obj, i), valueType));
+                    Object ov = Array.get(obj, i);
+                    Object cv = convert(ov, valueType);
+                    newCol.add(cv);
                 } catch (Exception t) {
                     throw new Exception("Unable to convert from " + obj + " to " + type +
"(error converting array element)", t);
                 }
             }
+            return newCol;
         } else {
+            boolean converted = !toClass(type).isAssignableFrom(obj.getClass());
             for (Object item : (Collection) obj) {
                 try {
-                    newCol.add(convert(item, valueType));
+                    Object cv = convert(item, valueType);
+                    converted |= item != cv;
+                    newCol.add(cv);
                 } catch (Exception t) {
                     throw new Exception("Unable to convert from " + obj + " to " + type +
"(error converting collection entry)", t);
                 }
             }
+            return converted ? newCol : obj;
         }
-        return newCol;
     }
 
     private Object convertToDictionary(Object obj, ReifiedType type) throws Exception {
         ReifiedType keyType = type.getActualTypeArgument(0);
         ReifiedType valueType = type.getActualTypeArgument(1);
-        Dictionary newDic = new Hashtable();
         if (obj instanceof Dictionary) {
+            Dictionary newDic = new Hashtable();
             Dictionary dic = (Dictionary) obj;
+            boolean converted = false;
             for (Enumeration keyEnum = dic.keys(); keyEnum.hasMoreElements();) {
                 Object key = keyEnum.nextElement();
                 try {
-                    newDic.put(convert(key, keyType), convert(dic.get(key), valueType));
+                    Object nk = convert(key, keyType);
+                    Object ov = dic.get(key);
+                    Object nv = convert(ov, valueType);
+                    newDic.put(nk, nv);
+                    converted |= nk != key || nv != ov;
                 } catch (Exception t) {
                     throw new Exception("Unable to convert from " + obj + " to " + type +
"(error converting map entry)", t);
                 }
             }
+            return converted ? newDic : obj;
         } else {
+            Dictionary newDic = new Hashtable();
             for (Map.Entry e : ((Map<Object,Object>) obj).entrySet()) {
                 try {
                     newDic.put(convert(e.getKey(), keyType), convert(e.getValue(), valueType));
@@ -368,8 +381,8 @@ public class AggregateConverter implemen
                     throw new Exception("Unable to convert from " + obj + " to " + type +
"(error converting map entry)", t);
                 }
             }
+            return newDic;
         }
-        return newDic;
     }
 
     private Object convertToMap(Object obj, ReifiedType type) throws Exception {
@@ -387,16 +400,21 @@ public class AggregateConverter implemen
                     throw new Exception("Unable to convert from " + obj + " to " + type +
"(error converting map entry)", t);
                 }
             }
+            return newMap;
         } else {
+            boolean converted = false;
             for (Map.Entry e : ((Map<Object,Object>) obj).entrySet()) {
                 try {
-                    newMap.put(convert(e.getKey(), keyType), convert(e.getValue(), valueType));
+                    Object nk = convert(e.getKey(), keyType);
+                    Object nv = convert(e.getValue(), valueType);
+                    converted |= nk != e.getKey() || nv != e.getValue();
+                    newMap.put(nk, nv);
                 } catch (Exception t) {
                     throw new Exception("Unable to convert from " + obj + " to " + type +
"(error converting map entry)", t);
                 }
             }
+            return converted ? newMap : obj;
         }
-        return newMap;
     }
 
     private Object convertToArray(Object obj, ReifiedType type) throws Exception {
@@ -413,14 +431,18 @@ public class AggregateConverter implemen
             componentType = new GenericType(type.getRawClass().getComponentType());
         }
         Object array = Array.newInstance(toClass(componentType), Array.getLength(obj));
+        boolean converted = array.getClass() != obj.getClass();
         for (int i = 0; i < Array.getLength(obj); i++) {
             try {
-                Array.set(array, i, convert(Array.get(obj, i), componentType));
+                Object ov = Array.get(obj, i);
+                Object nv = convert(ov, componentType);
+                converted |= nv != ov;
+                Array.set(array, i, nv);
             } catch (Exception t) {
                 throw new Exception("Unable to convert from " + obj + " to " + type + "(error
converting array element)", t);
             }
         }
-        return array;
+        return converted ? array : obj;
     }
 
     public static boolean isAssignable(Object source, ReifiedType target) {

Modified: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java?rev=1690252&r1=1690251&r2=1690252&view=diff
==============================================================================
--- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java
(original)
+++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java
Fri Jul 10 12:46:53 2015
@@ -24,6 +24,8 @@ import java.net.URI;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Collections;
+import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Locale;
@@ -224,6 +226,34 @@ public class AggregateConverterTest exte
 
         assertNotNull(s.convert(Arrays.asList(new EuRegion() {}), new GenericType(List.class,
new GenericType(Region.class))));
     }
+
+    public void testConvertCompatibleCollections() throws Exception {
+        Object org = Arrays.asList(Arrays.asList(1, 2), Arrays.asList(3, 4));
+        Object obj = service.convert(org,
+                GenericType.parse("java.util.List<java.util.List<java.lang.Integer>>",
getClass().getClassLoader()));
+        assertSame(org, obj);
+
+        org = Collections.singletonMap("foo", 1);
+        obj = service.convert(org,
+                GenericType.parse("java.util.Map<java.lang.String,java.lang.Integer>",
getClass().getClassLoader()));
+        assertSame(org, obj);
+
+        org = new int[] { 1, 2 };
+        obj = service.convert(org,
+                GenericType.parse("int[]", getClass().getClassLoader()));
+        assertSame(org, obj);
+
+        org = new Object[] { 1, 2 };
+        obj = service.convert(org,
+                GenericType.parse("int[]", getClass().getClassLoader()));
+        assertNotSame(org, obj);
+
+        Hashtable<String, Integer> ht = new Hashtable<String, Integer>();
+        ht.put("foo", 1);
+        org = ht;
+        obj = service.convert(org, GenericType.parse("java.util.Dictionary<java.lang.String,java.lang.Integer>",
getClass().getClassLoader()));
+        assertSame(org, obj);;
+    }
     
     private interface Region {} 
     



Mime
View raw message