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/08/28 16:22:41 UTC
svn commit: r1806472 - in /felix/trunk/converter/converter/src:
main/java/org/apache/felix/converter/impl/ConvertingImpl.java
test/java/org/apache/felix/converter/impl/ConverterMapTest.java
Author: davidb
Date: Mon Aug 28 16:22:41 2017
New Revision: 1806472
URL: http://svn.apache.org/viewvc?rev=1806472&view=rev
Log:
Felix Converter - additional fix for getProperties() handling
Modified:
felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java
Modified: felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1806472&r1=1806471&r2=1806472&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java Mon Aug 28 16:22:41 2017
@@ -375,6 +375,8 @@ public class ConvertingImpl extends Abst
return MapDelegate.forDTO(object, this);
} else if (sourceAsJavaBean) {
return MapDelegate.forBean(object, this);
+ } else if (hasGetProperties(sourceClass)) {
+ return null; // Handled in convertToMap()
}
// Assume it's an interface
@@ -795,12 +797,37 @@ public class ConvertingImpl extends Abst
Map<?,?> m = createMapFromBeanAccessors(obj, sourceCls);
if (m.size() > 0)
return m;
+ } else if (hasGetProperties(sourceCls)) {
+ return getPropertiesDelegate(obj, sourceCls);
} else if (typeProhibitedFromMapView(sourceCls))
throw new ConversionException("No map view for " + obj);
return createMapFromInterface(obj);
}
+ private boolean hasGetProperties(Class<?> cls) {
+ try {
+ Method m = cls.getDeclaredMethod("getProperties");
+ if (m == null)
+ m = cls.getMethod("getProperties");
+ return m != null;
+ } catch (Exception e) {
+ return false;
+ }
+ }
+
+ private Map<?, ?> getPropertiesDelegate(Object obj, Class<?> cls) {
+ try {
+ Method m = cls.getDeclaredMethod("getProperties");
+ if (m == null)
+ m = cls.getMethod("getProperties");
+
+ return converter.convert(m.invoke(obj)).to(Map.class);
+ } catch (Exception e) {
+ return Collections.emptyMap();
+ }
+ }
+
private boolean typeProhibitedFromMapView(Class<?> sourceCls) {
return NO_MAP_VIEW_TYPES.contains(sourceCls);
}
Modified: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java?rev=1806472&r1=1806471&r2=1806472&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java (original)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterMapTest.java Mon Aug 28 16:22:41 2017
@@ -492,6 +492,54 @@ public class ConverterMapTest {
assertEquals("hello", m.get("foo"));
}
+ @Test
+ public void testInterfaceWithGetProperties() {
+ TestInterfaceWithGetProperties tiwgp = new TestInterfaceWithGetProperties() {
+ @Override
+ public int blah() {
+ return 99;
+ }
+
+ @Override
+ public Dictionary<String, Object> getProperties() {
+ Dictionary<String, Object> d = new TestDictionary<>();
+ d.put("hi", "ha");
+ d.put("ho", "ho");
+ return d;
+ }
+ };
+
+ @SuppressWarnings("rawtypes")
+ Map m = converter.convert(tiwgp).to(Map.class);
+ assertEquals(2, m.size());
+ assertEquals("ha", m.get("hi"));
+ assertEquals("ho", m.get("ho"));
+ }
+
+ @Test
+ public void testInterfaceWithGetPropertiesCopied() {
+ TestInterfaceWithGetProperties tiwgp = new TestInterfaceWithGetProperties() {
+ @Override
+ public int blah() {
+ return 99;
+ }
+
+ @Override
+ public Dictionary<String, Object> getProperties() {
+ Dictionary<String, Object> d = new TestDictionary<>();
+ d.put("hi", "ha");
+ d.put("ho", "ho");
+ return d;
+ }
+ };
+
+ @SuppressWarnings("rawtypes")
+ Map m = converter.convert(tiwgp).copy().to(Map.class);
+ assertEquals(2, m.size());
+ assertEquals("ha", m.get("hi"));
+ assertEquals("ho", m.get("ho"));
+ }
+
private <K,V> Map.Entry<K,V> getMapEntry(Map<K,V> map) {
assertEquals("This method assumes a map of size 1", 1, map.size());
return map.entrySet().iterator().next();
@@ -504,6 +552,11 @@ public class ConverterMapTest {
Boolean za_za();
}
+ interface TestInterfaceWithGetProperties {
+ int blah();
+ Dictionary<String, Object> getProperties();
+ }
+
@interface TestAnnotation {
String foo() default "fooo!";
int bar() default 42;