[
https://issues.apache.org/jira/browse/FELIX-5325?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15420938#comment-15420938
]
David Leangen commented on FELIX-5325:
--------------------------------------
Index: src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
===================================================================
--- src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
(revision 1756057)
+++ src/main/java/org/apache/felix/converter/impl/ConvertingImpl.java
(working copy)
@@ -222,7 +222,7 @@
@SuppressWarnings({ "rawtypes", "unchecked" })
private <T> T convertToDTO(Class<T> targetCls) {
- Map m = mapView(object);
+ Map m = mapView(object, converter);
try {
T dto = targetCls.newInstance();
@@ -230,7 +230,11 @@
for (Map.Entry entry : (Set<Map.Entry>) m.entrySet()) {
try {
Field f = targetCls.getField(entry.getKey().toString());
- f.set(dto, entry.getValue());
+ Object fVal = entry.getValue();
+ if(DTO.class.isAssignableFrom( f.getType()))
+ fVal = converter.convert(fVal).to(f.getType());
+ // TODO convert other embedded objects that require
conversion
+ f.set(dto, fVal);
} catch (NoSuchFieldException e) {
}
}
@@ -243,7 +247,7 @@
@SuppressWarnings({ "rawtypes", "unchecked" })
private Map convertToMap(Class<?> targetCls, Type[] typeArguments) {
- Map m = mapView(object);
+ Map m = mapView(object, converter);
if (m == null)
return null;
Class<?> targetKeyType = null, targetValueType = null;
@@ -287,7 +291,7 @@
private Object createJavaBean(Class<?> targetCls) {
@SuppressWarnings("rawtypes")
- Map m = mapView(object);
+ Map m = mapView(object, converter);
try {
Object res = targetCls.getConstructor().newInstance();
for (Method setter : getSetters(targetCls)) {
@@ -308,7 +312,7 @@
@SuppressWarnings("rawtypes")
private Object createProxy(Class<?> targetCls) {
- Map m = mapView(object);
+ Map m = mapView(object, converter);
return Proxy.newProxyInstance(targetCls.getClassLoader(), new Class[]
{targetCls},
new InvocationHandler() {
@Override
@@ -476,7 +480,7 @@
}
@SuppressWarnings({ "rawtypes", "unchecked" })
- private static Map createMapFromDTO(Object obj) {
+ private static Map createMapFromDTO(Object obj, Converter converter) {
Map result = new HashMap();
for (Field f : obj.getClass().getFields()) {
@@ -484,7 +488,11 @@
continue;
try {
- result.put(f.getName(), f.get(obj)); // TODO handle escaping
+ Object fVal = f.get(obj);
+ if(fVal instanceof DTO)
+ fVal = converter.convert(fVal).to(Map.class);
+ // TODO test for other embedded types that need conversion
+ result.put(f.getName(), fVal);
} catch (Exception e) {
}
}
@@ -607,13 +615,13 @@
}
}
- private static Map<?,?> mapView(Object obj) {
+ private static Map<?,?> mapView(Object obj, Converter converter) {
if (obj instanceof Map)
return (Map<?,?>) obj;
else if (obj instanceof Dictionary)
return null; // TODO
else if (obj instanceof DTO)
- return createMapFromDTO(obj);
+ return createMapFromDTO(obj, converter);
else if (obj.getClass().getInterfaces().length > 0)
return createMapFromInterface(obj);
else
Index: src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
===================================================================
--- src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
(revision 1756057)
+++ src/test/java/org/apache/felix/converter/impl/ConverterServiceTest.java
(working copy)
@@ -40,6 +40,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;
+import org.apache.felix.converter.impl.MyDTO.Count;
+import org.apache.felix.converter.impl.MyEmbeddedDTO.Alpha;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -50,6 +52,7 @@
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
@@ -388,15 +391,29 @@
@Test
public void testDTO2Map() {
+ MyEmbeddedDTO embedded = new MyEmbeddedDTO();
+ embedded.marco = "hohoho";
+ embedded.polo = Long.MAX_VALUE;
+ embedded.alpha = Alpha.A;
+
MyDTO dto = new MyDTO();
dto.ping = "lalala";
dto.pong = Long.MIN_VALUE;
+ dto.count = Count.ONE;
+ dto.embedded = embedded;
@SuppressWarnings("rawtypes")
Map m = converter.convert(dto).to(Map.class);
- assertEquals(2, m.size());
+ assertEquals(4, m.size());
assertEquals("lalala", m.get("ping"));
assertEquals(Long.MIN_VALUE, m.get("pong"));
+ assertEquals(Count.ONE, m.get("count"));
+ assertNotNull(m.get("embedded"));
+ @SuppressWarnings("rawtypes")
+ Map e = (Map)m.get("embedded");
+ assertEquals("hohoho", e.get("marco"));
+ assertEquals(Long.MAX_VALUE, e.get("polo"));
+ assertEquals(Alpha.A, e.get("alpha"));
}
@Test
@@ -404,10 +421,21 @@
Map<String, Object> m = new HashMap<>();
m.put("ping", "abc xyz");
m.put("pong", 42L);
+ m.put("count", Count.ONE);
+ Map<String, Object> e = new HashMap<>();
+ e.put("marco", "ichi ni san");
+ e.put("polo", 64L);
+ e.put("alpha", Alpha.A);
+ m.put("embedded", e);
MyDTO dto = converter.convert(m).to(MyDTO.class);
assertEquals("abc xyz", dto.ping);
assertEquals(42L, dto.pong);
+ assertEquals(Count.ONE, dto.count);
+ assertNotNull(dto.embedded);
+ assertEquals(dto.embedded.marco, "ichi ni san");
+ assertEquals(dto.embedded.polo, 64L);
+ assertEquals(dto.embedded.alpha, Alpha.A);
}
static class MyClass2 {
Index: src/test/java/org/apache/felix/converter/impl/MyDTO.java
===================================================================
--- src/test/java/org/apache/felix/converter/impl/MyDTO.java (revision
1756057)
+++ src/test/java/org/apache/felix/converter/impl/MyDTO.java (working copy)
@@ -19,7 +19,13 @@
import org.osgi.dto.DTO;
public class MyDTO extends DTO {
+ public enum Count { ONE, TWO, THREE }
+
+ public Count count;
+
public String ping;
public long pong;
+
+ public MyEmbeddedDTO embedded;
}
> Patch for embedded DTO (in DTO)
> -------------------------------
>
> Key: FELIX-5325
> URL: https://issues.apache.org/jira/browse/FELIX-5325
> Project: Felix
> Issue Type: Improvement
> Components: Converter
> Reporter: David Leangen
> Priority: Minor
>
> This patch adds support for converting a DTO that contains an embedded DTO.
> It uses recursive calls to Converter.convert() to accomplish this.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)