Author: davidb Date: Thu Apr 14 14:52:08 2016 New Revision: 1739125 URL: http://svn.apache.org/viewvc?rev=1739125&view=rev Log: Felix Converter Service - support for conversions to Class.
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=1739125&r1=1739124&r2=1739125&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 14:52:08 2016 @@ -43,11 +43,11 @@ public class ConvertingImpl implements C boxedClasses = Collections.unmodifiableMap(m); } -// private Converter converter; + private Converter converter; private final Object object; ConvertingImpl(Converter c, Object obj) { -// converter = c; + converter = c; object = obj; } @@ -98,10 +98,13 @@ public class ConvertingImpl implements C // This is not a primitive, just return null return null; } - if (cls.equals(boolean.class)) + if (cls.equals(boolean.class)) { return false; - else + } else if (cls.equals(Class.class)) { + return null; + } else { return 0; + } } private Class<?> primitiveToBoxed(Class<?> cls) { @@ -141,6 +144,16 @@ public class ConvertingImpl implements C if (object instanceof Boolean) { return ((Boolean) object).booleanValue() ? Integer.valueOf(1) : Integer.valueOf(0); } + } else if (Class.class.equals(targetCls)) { + if (object instanceof Collection && ((Collection<?>) object).size() == 0) { + return null; + } else { + try { + return getClass().getClassLoader().loadClass(converter.convert(object).toString()); + } catch (ClassNotFoundException e) { + throw new RuntimeException(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=1739125&r1=1739124&r2=1739125&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 14:52:08 2016 @@ -16,6 +16,7 @@ */ package org.apache.felix.converter.impl; +import java.math.BigDecimal; import java.math.BigInteger; import java.util.Collections; import java.util.stream.Collectors; @@ -30,6 +31,7 @@ import org.osgi.service.converter.Conver import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; @@ -77,6 +79,11 @@ public class ConverterTest { assertEquals(Integer.valueOf(123), converter.convert("123").to(int.class)); assertEquals(1, (int) converter.convert(true).to(int.class)); + // Conversions to Class + assertEquals(BigDecimal.class, converter.convert("java.math.BigDecimal").to(Class.class)); + assertNull(converter.convert(null).to(Class.class)); + assertNull(converter.convert(Collections.emptyList()).to(Class.class)); + assertEquals(Integer.valueOf(123), converter.convert("123").to(Integer.class)); assertEquals(Long.valueOf(123), converter.convert("123").to(Long.class)); // assertEquals(Character.valueOf(123), c.convert("123").to(Character.class));