Search in sources :

Example 6 with OtpErlangMap

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]));
}
Also used : HashMap(java.util.HashMap) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) Test(org.junit.Test)

Example 7 with OtpErlangMap

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);
    }
}
Also used : OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) Method(java.lang.reflect.Method) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) BigInteger(java.math.BigInteger) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangByte(com.ericsson.otp.erlang.OtpErlangByte) OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangRef(com.ericsson.otp.erlang.OtpErlangRef) OtpErlangShort(com.ericsson.otp.erlang.OtpErlangShort)

Aggregations

OtpErlangMap (com.ericsson.otp.erlang.OtpErlangMap)7 Test (org.junit.Test)5 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)4 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)3 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)2 OtpErlangRef (com.ericsson.otp.erlang.OtpErlangRef)2 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)2 HashMap (java.util.HashMap)2 OtpErlangBinary (com.ericsson.otp.erlang.OtpErlangBinary)1 OtpErlangByte (com.ericsson.otp.erlang.OtpErlangByte)1 OtpErlangChar (com.ericsson.otp.erlang.OtpErlangChar)1 OtpErlangLong (com.ericsson.otp.erlang.OtpErlangLong)1 OtpErlangPid (com.ericsson.otp.erlang.OtpErlangPid)1 OtpErlangShort (com.ericsson.otp.erlang.OtpErlangShort)1 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)1 Method (java.lang.reflect.Method)1 BigInteger (java.math.BigInteger)1 Collection (java.util.Collection)1 Map (java.util.Map)1