use of com.ericsson.otp.erlang.OtpErlangMap in project erlide_eclipse by erlang.
the class TypeConverterTest method cvtMapOk_1.
@Test
public void cvtMapOk_1() throws SignatureException {
final Map<OtpErlangObject, OtpErlangObject> map = new HashMap<>();
test(map, "m", new OtpErlangMap(new OtpErlangObject[0], new OtpErlangObject[0]));
}
use of com.ericsson.otp.erlang.OtpErlangMap in project erlide_eclipse by erlang.
the class TypeConverter method erlang2java.
@SuppressWarnings("boxing")
public static Object erlang2java(final OtpErlangObject obj, final Class<?> cls) throws SignatureException {
try {
if (cls == obj.getClass()) {
return obj;
}
// if the conversion method exists, use it
try {
final Method method = cls.getMethod("fromErlangObject", new Class<?>[] { OtpErlangObject.class });
method.setAccessible(true);
final Object o = method.invoke(null, obj);
return o;
} catch (final NoSuchMethodException e) {
// ignore, continue
}
if (cls.isArray()) {
return TypeConverter.cvtArray(obj, cls);
}
if (cls == String.class) {
return TypeConverter.cvtString(obj);
}
if (TypeConverter.isNumericClass(cls)) {
if (obj instanceof OtpErlangLong) {
final long res = ((OtpErlangLong) obj).longValue();
if (cls == char.class || cls == Character.class) {
return (char) res;
}
if (cls == int.class || cls == Integer.class) {
return (int) res;
}
if (cls == byte.class || cls == Byte.class) {
return (byte) res;
}
if (cls == short.class || cls == Short.class) {
return (short) res;
}
if (cls == long.class || cls == Long.class) {
return res;
}
}
throw new SignatureException(TypeConverter.WRONG_ARG_TYPE + obj.getClass().getName() + TypeConverter.CANT_CONVERT_TO + cls.getCanonicalName());
}
if (cls == boolean.class || cls == Boolean.class) {
if (obj instanceof OtpErlangAtom) {
final String s = ((OtpErlangAtom) obj).atomValue();
if ("true".equals(s)) {
return true;
}
if ("false".equals(s)) {
return false;
}
}
throw new SignatureException(TypeConverter.WRONG_ARG_TYPE + obj.getClass().getName() + TypeConverter.CANT_CONVERT_TO + cls.getCanonicalName());
}
if (Map.class.isAssignableFrom(cls)) {
if (obj instanceof OtpErlangMap) {
final Map<Object, Object> result = Maps.newHashMap();
final OtpErlangMap map = (OtpErlangMap) obj;
for (final OtpErlangObject key : map.keys()) {
final OtpErlangObject value = map.get(key);
result.put(TypeConverter.erlang2java(key, key.getClass()), TypeConverter.erlang2java(value, value.getClass()));
}
return result;
}
throw new SignatureException(TypeConverter.WRONG_ARG_TYPE + obj.getClass().getName() + TypeConverter.CANT_CONVERT_TO + cls.getCanonicalName());
}
if (Collection.class.isAssignableFrom(cls)) {
if (obj instanceof OtpErlangList) {
final OtpErlangObject[] list = ((OtpErlangList) obj).elements();
final Object[] olist = new Object[list.length];
for (int i = 0; i < list.length; i++) {
olist[i] = TypeConverter.erlang2java(list[i], list[i].getClass());
}
return Arrays.asList(olist);
}
throw new SignatureException(TypeConverter.WRONG_ARG_TYPE + obj.getClass().getName() + TypeConverter.CANT_CONVERT_TO + cls.getCanonicalName());
}
if (obj instanceof OtpErlangRef) {
throw new SignatureException(TypeConverter.WRONG_ARG_TYPE + obj.getClass().getName() + TypeConverter.CANT_CONVERT_TO + cls.getCanonicalName());
}
return obj;
} catch (final SignatureException e) {
throw e;
} catch (final Exception e) {
throw new SignatureException(e);
}
}
Aggregations