felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1739126 - in /felix/trunk/converter/src: main/java/org/apache/felix/converter/impl/ConvertingImpl.java test/java/org/apache/felix/converter/impl/ConverterTest.java
Date Thu, 14 Apr 2016 15:35:29 GMT
Author: davidb
Date: Thu Apr 14 15:35:29 2016
New Revision: 1739126

URL: http://svn.apache.org/viewvc?rev=1739126&view=rev
Log:
Felix Converter Service - support Enums.

Modified:
    felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
    felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java

Modified: felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java?rev=1739126&r1=1739125&r2=1739126&view=diff
==============================================================================
--- felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
(original)
+++ felix/trunk/converter/src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
Thu Apr 14 15:35:29 2016
@@ -102,6 +102,8 @@ public class ConvertingImpl implements C
             return false;
         } else if (cls.equals(Class.class)) {
             return null;
+        } else if (Enum.class.isAssignableFrom(cls)) {
+            return null;
         } else {
             return 0;
         }
@@ -154,6 +156,24 @@ public class ConvertingImpl implements C
                     throw new RuntimeException(e);
                 }
             }
+        } else if (Enum.class.isAssignableFrom(targetCls)) {
+            if (object instanceof Boolean) {
+                try {
+                    Method m = targetCls.getMethod("valueOf", String.class);
+                    return m.invoke(null, object.toString().toUpperCase());
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            } else if (object instanceof Number) {
+                try {
+                    Method m = targetCls.getMethod("values");
+                    Object[] values = (Object[]) m.invoke(null);
+                    return values[((Number) object).intValue()];
+                } catch (Exception e) {
+                    throw new RuntimeException(e);
+                }
+            }
+
         }
         return null;
     }
@@ -163,7 +183,7 @@ public class ConvertingImpl implements C
         try {
             Method m = cls.getDeclaredMethod("valueOf", String.class);
             if (m != null) {
-                return (T) m.invoke(null, object);
+                return (T) m.invoke(null, object.toString());
             }
         } catch (Exception e) {
             return null;

Modified: felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java?rev=1739126&r1=1739125&r2=1739126&view=diff
==============================================================================
--- felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
(original)
+++ felix/trunk/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
Thu Apr 14 15:35:29 2016
@@ -92,6 +92,17 @@ public class ConverterTest {
         assertEquals(Double.valueOf("12.3"), converter.convert("12.3").to(Double.class));
     }
 
+    enum TestEnum { FOO, BAR, BLAH, FALSE, X};
+    @Test
+    public void testEnums() {
+        assertEquals(TestEnum.BLAH, converter.convert("BLAH").to(TestEnum.class));
+        assertEquals(TestEnum.X, converter.convert('X').to(TestEnum.class));
+        assertEquals(TestEnum.FALSE, converter.convert(false).to(TestEnum.class));
+        assertEquals(TestEnum.BAR, converter.convert(1).to(TestEnum.class));
+        assertNull(converter.convert(null).to(TestEnum.class));
+        assertNull(converter.convert(Collections.emptySet()).to(TestEnum.class));
+    }
+
     @Test
     public void testIdentialTarget() {
         Object o = new Object();



Mime
View raw message