HCOLL-259 fixed JSON<->CHM to support $$Native like BondVOInterface

This commit is contained in:
Rob Austin
2014-12-21 17:28:35 +00:00
parent 59add5e42f
commit b2f6ebbba0
3 changed files with 172 additions and 39 deletions
@@ -17,27 +17,65 @@
package net.openhft.chronicle.map;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import net.openhft.lang.model.DataValueClasses;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
/**
* Created by Rob on 18/12/14.
*/
class XStreamHelper {
static Object to$$Native2(HierarchicalStreamReader reader, final Class aClass2, final boolean isKey,
ChronicleMap map, UnmarshallingContext unmarshallingContext, String type) {
static Object to$$Native(HierarchicalStreamReader reader, final Class aClass, final boolean isKey, ChronicleMap map) {
if (type.endsWith("$$Native")) {
Class<?> aClass = null;
try {
aClass = Class.forName(type);
} catch (ClassNotFoundException e) {
String clazz = type.substring(0, type.length() -
"$$Native".length());
try {
DataValueClasses.newDirectInstance(Class.forName(clazz));
} catch (ClassNotFoundException e1) {
throw new ConversionException(e1);
}
try {
aClass = Class.forName(type);
} catch (ClassNotFoundException e1) {
throw new ConversionException(e1);
}
}
return unmarshallingContext.convertAnother(null, aClass);
}
throw new ConversionException("setValue(..) method not found in class=" + type);
}
static Object to$$Native(HierarchicalStreamReader reader, final Class aClass, final boolean isKey, ChronicleMap map, UnmarshallingContext unmarshallingContext) {
final Object o = isKey ? map.newKeyInstance() : map.newValueInstance();
try {
for (Method $$nativeMethod : o.getClass().getMethods()) {
if ($$nativeMethod.getName().equals("setValue") && $$nativeMethod.getParameterTypes()
.length == 1) {
Class<?> parameterType = $$nativeMethod.getParameterTypes()[0];
String value = reader.getValue();
final BeanInfo info = Introspector.getBeanInfo(o.getClass()); // new BeanGenerator
for (PropertyDescriptor p : info.getPropertyDescriptors()) {
if (p.getName().equals("value")) {
final String value = reader.getValue();
Class<?> parameterType = p.getPropertyType();
if (parameterType.isPrimitive()) {
@@ -59,17 +97,18 @@ class XStreamHelper {
final Method valueOf = parameterType.getMethod("valueOf", String.class);
final Object invoke = valueOf.invoke(null, value);
$$nativeMethod.invoke(o, invoke);
return o;
final Object boxedValue = valueOf.invoke(null, value);
p.getWriteMethod().invoke(o, boxedValue);
return o;
}
}
} catch (Exception e) {
throw new ConversionException("class=" + aClass.getCanonicalName(), e);
//
}
throw new ConversionException("setValue(..) method not found in class=" + aClass
.getCanonicalName
());
.getCanonicalName());
}
}