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 2018/12/21 14:07:56 UTC

svn commit: r1849480 - in /felix/trunk/converter/converter/src: main/java/org/osgi/util/converter/CustomConverterImpl.java test/java/org/osgi/util/converter/ConverterTest.java

Author: davidb
Date: Fri Dec 21 14:07:56 2018
New Revision: 1849480

URL: http://svn.apache.org/viewvc?rev=1849480&view=rev
Log:
FELIX-6006 errorHandler not consulted for conversions implemented via proxies

Provide more useful type info to the errorHandler when using the errorHandler with a proxy. The type passed in is the expected return type of the method invocation.
Unit test provided by Cristiano GaviĆ£o with many thanks!

Modified:
    felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/CustomConverterImpl.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/CustomConverterImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/CustomConverterImpl.java?rev=1849480&r1=1849479&r2=1849480&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/CustomConverterImpl.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/CustomConverterImpl.java Fri Dec 21 14:07:56 2018
@@ -180,7 +180,7 @@ class CustomConverterImpl implements Int
 
 				Object result = del.to(type);
 				if (result != null && Proxy.isProxyClass(result.getClass()) && errorHandlers.size() > 0) {
-				    return wrapErrorHandling(result, type);
+				    return wrapErrorHandling(result);
 				} else {
 				    return result;
 				}
@@ -202,7 +202,7 @@ class CustomConverterImpl implements Int
 			}
 		}
 
-		private Object wrapErrorHandling(final Object wrapped, final Type type) {
+		private Object wrapErrorHandling(final Object wrapped) {
 		    final Class<?> cls = wrapped.getClass();
 		    return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new InvocationHandler() {
                 @Override
@@ -228,7 +228,7 @@ class CustomConverterImpl implements Int
                     } catch (Exception ex) {
                         for (ConverterFunction eh : errorHandlers) {
                             try {
-                                Object handled = eh.apply(wrapped, type);
+                                Object handled = eh.apply(wrapped, method.getGenericReturnType());
                                 if (handled != ConverterFunction.CANNOT_HANDLE)
                                     return handled;
                             } catch (RuntimeException re) {

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=1849480&r1=1849479&r2=1849480&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 Fri Dec 21 14:07:56 2018
@@ -1242,6 +1242,33 @@ public class ConverterTest {
 	    	assertEquals(new HashSet<Character>(Arrays.asList('f', 'o')), converted.set());
     }
 
+    @Test
+    public void testMapToInterfaceWithOptionalValue() throws Exception {
+        ConverterBuilder cb = Converters.newConverterBuilder();
+        cb.errorHandler(new ConverterFunction() {
+            @Override
+            public Object apply(Object pObj, Type pTargetType) throws Exception {
+                if ("java.lang.Integer".equals(pTargetType.getTypeName()))
+                {
+                    return 0;
+                }
+                return ConverterFunction.CANNOT_HANDLE;
+            }
+        });
+        Converter convWithErrorHandler = cb.build();
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        map.put("code", "harley");
+        MyIntf2 inter = convWithErrorHandler.convert(map).to(MyIntf2.class);
+        assertEquals("harley", inter.code());
+        assertEquals(Integer.valueOf(0), inter.value());
+    }
+
+    static interface MyIntf2 {
+        String code();
+        Integer value();
+    }
+
     static class MyClass2 {
         private final String value;
         public MyClass2(String v) {