mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
HCOLL-259 fixed JSON<->CHM to support $$Native like BondVOInterface
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user