From c779ee95c5da777258472bbb27173345c3284c01 Mon Sep 17 00:00:00 2001 From: Rob Austin Date: Mon, 22 Dec 2014 20:14:28 +0000 Subject: [PATCH] HCOLL-268 refactored the JSON convertor --- .../chronicle/map/ChronicleMapConvector.java | 149 ++++++++---------- .../chronicle/map/TcpReplicationSoakTest.java | 6 +- 2 files changed, 69 insertions(+), 86 deletions(-) diff --git a/src/main/java/net/openhft/chronicle/map/ChronicleMapConvector.java b/src/main/java/net/openhft/chronicle/map/ChronicleMapConvector.java index f900b8ee2..07e99f45c 100644 --- a/src/main/java/net/openhft/chronicle/map/ChronicleMapConvector.java +++ b/src/main/java/net/openhft/chronicle/map/ChronicleMapConvector.java @@ -52,11 +52,12 @@ public class ChronicleMapConvector implements Converter { @Override public boolean canConvert(Class aClass) { + //noinspection unchecked if (entrySetClass.isAssignableFrom(aClass) || writeThroughEntryClass.isAssignableFrom(aClass)) return true; - String canonicalName = aClass.getCanonicalName(); + final String canonicalName = aClass.getCanonicalName(); return canonicalName.startsWith("net.openhft.lang.values") || canonicalName.endsWith("$$Native") || @@ -67,6 +68,7 @@ public class ChronicleMapConvector implements Converter { public void marshal(Object o, HierarchicalStreamWriter writer, MarshallingContext marshallingContext) { + //noinspection unchecked if (writeThroughEntryClass.isAssignableFrom(o.getClass())) { final AbstractMap.SimpleEntry e = (AbstractMap.SimpleEntry) o; @@ -99,64 +101,64 @@ public class ChronicleMapConvector implements Converter { boolean isNative = canonicalName.endsWith("$$Native"); boolean isHeap = canonicalName.endsWith("$$Heap"); - if (isNative || isHeap) { - if (canonicalName.startsWith("net.openhft.lang.values")) { + if (!isNative && !isHeap) + return; - Method[] methods = o.getClass().getMethods(); + if (canonicalName.startsWith("net.openhft.lang.values")) { - for (Method method : methods) { + Method[] methods = o.getClass().getMethods(); - if (!method.getName().equals("getValue") || - method.getParameterTypes().length != 0) { - continue; - } + for (Method method : methods) { - try { - marshallingContext.convertAnother(method.invoke(o)); + if (!method.getName().equals("getValue") || + method.getParameterTypes().length != 0) { + continue; + } + + try { + marshallingContext.convertAnother(method.invoke(o)); + return; + } catch (Exception e) { + throw new ConversionException("class=" + canonicalName, e); + } + } + + throw new ConversionException("class=" + canonicalName); + } + + + try { + + final BeanInfo info = getBeanInfo(o.getClass()); // new BeanGenerator + + for (PropertyDescriptor p : info.getPropertyDescriptors()) { + + if (p.getName().equals("Class") || + p.getReadMethod() == null || + p.getWriteMethod() == null) + continue; + + try { + + final Method readMethod = p.getReadMethod(); + final Object value = readMethod.invoke(o); + + if (value == null) return; - } catch (Exception e) { - throw new ConversionException("class=" + canonicalName, e); - } + + writer.startNode(p.getDisplayName()); + marshallingContext.convertAnother(value); + writer.endNode(); + + } catch (Exception e) { + LOG.error("class=" + p.getName(), e); } - throw new ConversionException("class=" + canonicalName); } - - try { - - final BeanInfo info = getBeanInfo(o.getClass()); // new BeanGenerator - - for (PropertyDescriptor p : info.getPropertyDescriptors()) { - - if (p.getName().equals("Class") || - p.getReadMethod() == null || - p.getWriteMethod() == null) - continue; - - try { - - final Method readMethod = p.getReadMethod(); - final Object value = readMethod.invoke(o); - - if (value == null) - return; - - writer.startNode(p.getDisplayName()); - marshallingContext.convertAnother(value); - writer.endNode(); - - } catch (Exception e) { - LOG.error("class=" + p.getName(), e); - } - - - } - } catch (IntrospectionException e) { - throw new ConversionException("class=" + canonicalName, e); - } - + } catch (IntrospectionException e) { + throw new ConversionException("class=" + canonicalName, e); } } @@ -178,11 +180,11 @@ public class ChronicleMapConvector implements Converter { final V v; reader.moveDown(); - k = (K) deserialize(context, reader); + k = deserialize(context, reader); reader.moveUp(); reader.moveDown(); - v = (V) deserialize(context, reader); + v = deserialize(context, reader); reader.moveUp(); @@ -206,7 +208,7 @@ public class ChronicleMapConvector implements Converter { if (context.getRequiredType().getCanonicalName().startsWith ("net.openhft.lang.values")) - return toNativeValueObjects(reader, context.getRequiredType()); + return toNativeValueObjects(reader, context.getRequiredType(), context); final String nodeName = isNative ? @@ -266,26 +268,26 @@ public class ChronicleMapConvector implements Converter { } - static Object deserialize(@NotNull UnmarshallingContext unmarshallingContext, - @NotNull HierarchicalStreamReader reader) { + static E deserialize(@NotNull UnmarshallingContext unmarshallingContext, + @NotNull HierarchicalStreamReader reader) { final String clazz = reader.getNodeName(); switch (clazz) { case "java.util.Collections$EmptySet": - return Collections.EMPTY_SET; + return (E) Collections.EMPTY_SET; case "java.util.Collections$EmptyList": - return Collections.EMPTY_LIST; + return (E) Collections.EMPTY_LIST; case "java.util.Collections$EmptyMap": case "java.util.Collections.EmptyMap": - return Collections.EMPTY_MAP; + return (E) Collections.EMPTY_MAP; } - return unmarshallingContext.convertAnother(null, forName(clazz)); + return (E) unmarshallingContext.convertAnother(null, forName(clazz)); } static Class forName(String clazz) { @@ -316,7 +318,9 @@ public class ChronicleMapConvector implements Converter { } - static Object toNativeValueObjects(HierarchicalStreamReader reader, final Class aClass) { + static Object toNativeValueObjects(HierarchicalStreamReader reader, + final Class aClass, + UnmarshallingContext context) { final Object o = DataValueClasses.newDirectInstance(aClass); @@ -326,40 +330,21 @@ public class ChronicleMapConvector implements Converter { for (PropertyDescriptor p : info.getPropertyDescriptors()) { - if (!p.getName().equals("value")) { + if (!p.getName().equals("value")) continue; - } final String value = reader.getValue(); - Class parameterType = p.getPropertyType(); if (StringValue.class.isAssignableFrom(o.getClass())) { ((StringValue) o).setValue(value); return o; } - if (parameterType.isPrimitive()) { + final Class propertyType = p.getPropertyType(); - // convert the primitive to their boxed type + final Object o1 = context.convertAnother(value, propertyType); + p.getWriteMethod().invoke(o, o1); - if (parameterType == int.class) - parameterType = Integer.class; - else if (parameterType == char.class) - parameterType = Character.class; - else { - final String name = parameterType.getSimpleName(); - - final String properName = "java.lang." + Character.toString - (name.charAt(0)).toUpperCase() - + name.substring(1); - parameterType = Class.forName(properName); - } - } - - final Method valueOf = parameterType.getMethod("valueOf", String.class); - final Object boxedValue = valueOf.invoke(null, value); - - p.getWriteMethod().invoke(o, boxedValue); return o; } diff --git a/src/test/java/net/openhft/chronicle/map/TcpReplicationSoakTest.java b/src/test/java/net/openhft/chronicle/map/TcpReplicationSoakTest.java index 0a29d0892..13a4bbf25 100644 --- a/src/test/java/net/openhft/chronicle/map/TcpReplicationSoakTest.java +++ b/src/test/java/net/openhft/chronicle/map/TcpReplicationSoakTest.java @@ -5,10 +5,7 @@ import net.openhft.lang.io.ByteBufferBytes; import net.openhft.lang.model.Byteable; import net.openhft.lang.model.DataValueClasses; import net.openhft.lang.values.IntValue; -import org.junit.After; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; import java.io.Closeable; import java.io.IOException; @@ -23,6 +20,7 @@ import java.util.concurrent.TimeUnit; /** * @author Rob Austin. */ +@Ignore public class TcpReplicationSoakTest {