You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by da...@apache.org on 2017/11/16 21:48:11 UTC

svn commit: r1815515 - in /felix/trunk/converter/converter/src: main/java/org/osgi/util/converter/ test/java/org/osgi/util/converter/

Author: davidb
Date: Thu Nov 16 21:48:11 2017
New Revision: 1815515

URL: http://svn.apache.org/viewvc?rev=1815515&view=rev
Log:
Fix live map conversion

Added:
    felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO9.java
Modified:
    felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
    felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/MapDelegate.java
    felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java?rev=1815515&r1=1815514&r2=1815515&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java Thu Nov 16 21:48:11 2017
@@ -41,6 +41,9 @@ import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
 
+/**
+ * @author $Id: d83ff45e4ee09ebafe6704e71cc66c53d75583dd $
+ */
 class ConvertingImpl extends AbstractSpecifying<Converting> implements Converting, InternalConverting {
     private static final Map<Class<?>, Class<?>> INTERFACE_IMPLS;
     static {
@@ -336,31 +339,39 @@ class ConvertingImpl extends AbstractSpe
         return instance;
     }
 
-    Object convertMapValue(Object value) {
-        Type targetValueType = null;
-        if (typeArguments != null && typeArguments.length > 1) {
-            targetValueType = typeArguments[1];
-        }
+	Object convertMapKey(Object key) {
+		return convertMapElement(key, 0);
+	}
 
-        if (value != null) {
-            if (targetValueType != null) {
-                value = converter.convert(value).to(targetValueType);
-            } else {
-                Class<?> cls = value.getClass();
-                if (isCopyRequiredType(cls)) {
-                    cls = getConstructableType(cls);
-                }
+    Object convertMapValue(Object value) {
+		return convertMapElement(value, 1);
+	}
 
-                if (sourceAsDTO || DTOUtil.isDTOType(cls))
-                    value = converter.convert(value).sourceAsDTO().to(cls);
-                else
-                    value = converter.convert(value).to(cls);
-            }
-        }
-        return value;
-    }
+	private Object convertMapElement(Object element, int typeIdx) {
+		Type targetType = null;
+		if (typeArguments != null && typeArguments.length > typeIdx) {
+			targetType = typeArguments[typeIdx];
+		}
+
+		if (element != null) {
+			if (targetType != null) {
+				element = converter.convert(element).to(targetType);
+			} else {
+				Class< ? > cls = element.getClass();
+				if (isCopyRequiredType(cls)) {
+					cls = getConstructableType(cls);
+				}
+
+				if (sourceAsDTO || DTOUtil.isDTOType(cls))
+					element = converter.convert(element).sourceAsDTO().to(cls);
+				else
+					element = converter.convert(element).to(cls);
+			}
+		}
+		return element;
+	}
 
-    @SuppressWarnings({ "unchecked", "rawtypes" })
+	@SuppressWarnings({ "unchecked", "rawtypes" })
     private Map convertToMapDelegate() {
         if (Map.class.isAssignableFrom(sourceClass)) {
             return MapDelegate.forMap((Map) object, this);
@@ -864,5 +875,4 @@ class ConvertingImpl extends AbstractSpe
         }
         return setters;
     }
-
 }

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/MapDelegate.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/MapDelegate.java?rev=1815515&r1=1815514&r2=1815515&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/MapDelegate.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/MapDelegate.java Thu Nov 16 21:48:11 2017
@@ -25,6 +25,9 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
+/**
+ * @author $Id: 12cb021a8b0bff80df438cdb40cefaa04be26437 $
+ */
 class MapDelegate<K, V> implements Map<K, V> {
     private final ConvertingImpl convertingImpl;
     Map<K, V> delegate;
@@ -54,8 +57,17 @@ class MapDelegate<K, V> implements Map<K
         return new MapDelegate<>(converting, new DynamicInterfaceFacade(obj, converting));
     }
 
+    @Override
     public int size() {
-        return delegate.size();
+        // Need to convert the entire map to get the size
+        Set<Object> keys = new HashSet<>();
+
+        Set<K> ks = delegate.keySet();
+        for (K key : ks) {
+            keys.add(getConvertedKey(key));
+        }
+
+        return keys.size();
     }
 
     public boolean isEmpty() {
@@ -85,10 +97,14 @@ class MapDelegate<K, V> implements Map<K
         if (val == null)
             return null;
         else
-            return (V) getConvertedValue(key, val);
+			return (V) getConvertedValue(val);
     }
 
-    private Object getConvertedValue(Object key, Object val) {
+	private Object getConvertedKey(Object key) {
+		return convertingImpl.convertMapKey(key);
+	}
+
+	private Object getConvertedValue(Object val) {
         return convertingImpl.convertMapValue(val);
     }
 
@@ -161,7 +177,7 @@ class MapDelegate<K, V> implements Map<K
         Set<Map.Entry<K,V>> result = new HashSet<>();
         for (Map.Entry<?,?> entry : delegate.entrySet()) {
             K key = (K) findConvertedKey(internalKeySet(), entry.getKey());
-            V val = (V) getConvertedValue(key, entry.getValue());
+			V val = (V) getConvertedValue(entry.getValue());
             result.add(new MapEntry<K,V>(key, val));
         }
         return result;

Modified: felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java?rev=1815515&r1=1815514&r2=1815515&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java (original)
+++ felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterTest.java Thu Nov 16 21:48:11 2017
@@ -890,6 +890,17 @@ public class ConverterTest {
     }
 
     @Test
+    public void testLiveMapFromDTO2() {
+        MyDTO9 dto = new MyDTO9();
+        dto.key1 = "value1";
+        dto.key2 = "value2";
+
+        Map<Character, Character> m = converter.convert(dto).to(new TypeReference<Map<Character, Character>>() {});
+        assertEquals(1, m.size());
+        assertEquals('v', (char) m.get('k'));
+    }
+
+    @Test
     public void testLiveMapFromDictionary() throws URISyntaxException {
         URI testURI = new URI("http://foo");
         Hashtable<String, Object> d = new Hashtable<>();

Added: felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO9.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO9.java?rev=1815515&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO9.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/MyDTO9.java Thu Nov 16 21:48:11 2017
@@ -0,0 +1,22 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.osgi.util.converter;
+
+public class MyDTO9 {
+    public String key1;
+    public String key2;
+}