Search in sources :

Example 1 with OtpErlangRef

use of com.ericsson.otp.erlang.OtpErlangRef in project erlide_eclipse by erlang.

the class TypeConverter method java2erlang.

@SuppressWarnings("boxing")
public static OtpErlangObject java2erlang(final Object obj, final Signature type) throws SignatureException {
    if (type.kind == 'x') {
        return TypeConverter.java2erlang(obj);
    }
    if (obj instanceof String) {
        return TypeConverter.cvtString(obj, type);
    }
    if (obj instanceof Character) {
        if (type.kind == 'i') {
            return new OtpErlangChar((Character) obj);
        }
        TypeConverter.failConversion(obj, type);
    }
    if (obj instanceof Number) {
        return TypeConverter.cvtNumber(obj, type);
    }
    if (obj instanceof Boolean) {
        if (type.kind == 'o') {
            return new OtpErlangAtom((Boolean) obj ? "true" : "false");
        }
        TypeConverter.failConversion(obj, type);
    }
    if (obj instanceof Collection<?>) {
        if (type.kind == 'l') {
            final Object[] v = ((Collection<?>) obj).toArray(new Object[] {});
            final OtpErlangObject[] vv = new OtpErlangObject[v.length];
            for (int i = 0; i < v.length; i++) {
                vv[i] = TypeConverter.java2erlang(v[i], type.content[0]);
            }
            return new OtpErlangList(vv);
        }
        TypeConverter.failConversion(obj, type);
    }
    if (obj instanceof Map<?, ?>) {
        if (type.kind == 'm') {
            @SuppressWarnings("unchecked") final Map<OtpErlangObject, OtpErlangObject> map = (Map<OtpErlangObject, OtpErlangObject>) obj;
            final int size = map.keySet().size();
            final OtpErlangObject[] keys = map.keySet().toArray(new OtpErlangObject[size]);
            final OtpErlangObject[] values = new OtpErlangObject[size];
            for (int i = 0; i < size; i++) {
                values[i] = map.get(keys[i]);
            }
            return new OtpErlangMap(keys, values);
        }
        TypeConverter.failConversion(obj, type);
    }
    if (obj instanceof OtpErlangPid) {
        return (OtpErlangPid) obj;
    }
    if (obj instanceof OtpErlangRef) {
        return (OtpErlangObject) obj;
    }
    if (obj instanceof OtpErlangBinary) {
        return (OtpErlangObject) obj;
    }
    if (obj instanceof OtpErlangObject) {
        TypeConverter.checkConversion(obj);
        return (OtpErlangObject) obj;
    }
    if (obj instanceof IConvertible) {
        return ((IConvertible) obj).toErlangObject();
    }
    if (obj != null && obj.getClass().isArray()) {
        final int len = Array.getLength(obj);
        // Class<?> component = obj.getClass().getComponentType();
        if (type.kind == 'b') {
            // TODO we can convert more things to binaries
            return new OtpErlangBinary(obj);
        }
        if (type.kind == 'l') {
            final OtpErlangObject[] vv = new OtpErlangObject[len];
            for (int i = 0; i < len; i++) {
                vv[i] = TypeConverter.java2erlang(Array.get(obj, i), type.content[0]);
            }
            return new OtpErlangList(vv);
        } else if (type.kind == 't') {
            final OtpErlangObject[] vv = new OtpErlangObject[len];
            for (int i = 0; i < len; i++) {
                vv[i] = TypeConverter.java2erlang(Array.get(obj, i), type.content[i]);
            }
            return new OtpErlangTuple(vv);
        } else {
            TypeConverter.failConversion(obj, type);
        }
    }
    if (type.kind == 's' && obj != null) {
        return new OtpErlangString(obj.toString());
    }
    if (type.kind == 'b' && obj != null) {
        return new OtpErlangBinary(obj.toString().getBytes());
    }
    TypeConverter.failConversion(obj, type);
    return null;
}
Also used : OtpErlangChar(com.ericsson.otp.erlang.OtpErlangChar) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary) OtpErlangPid(com.ericsson.otp.erlang.OtpErlangPid) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) Collection(java.util.Collection) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangRef(com.ericsson.otp.erlang.OtpErlangRef) Map(java.util.Map) OtpErlangMap(com.ericsson.otp.erlang.OtpErlangMap) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 2 with OtpErlangRef

use of com.ericsson.otp.erlang.OtpErlangRef in project erlide_eclipse by erlang.

the class OtpRpc method sendRpcCall.

private synchronized RpcFuture sendRpcCall(final OtpNode node, final String peer, final boolean logCalls, final OtpErlangObject gleader, final String module, final String fun, final String signature, final Object... args0) throws SignatureException {
    final OtpErlangObject[] args = convertArgs(signature, args0);
    OtpErlangObject res = null;
    final OtpMbox mbox = node.createMbox();
    res = buildRpcCall(mbox.self(), gleader, module, fun, args);
    if (logCalls) {
        final Object[] args01 = { module, fun, argString(args) };
        ErlLogger.debug("call -> %s:%s(%s)", args01);
    }
    // 
    final OtpErlangRef ref = RpcMonitor.recordRequest(node, peer, module, fun, args, OtpErlang.sizeOf(res));
    // 
    mbox.send("rex", peer, res);
    if (OtpRpc.CHECK_RPC) {
        ErlLogger.debug("RPC " + mbox.hashCode() + "=> " + res);
    }
    return new RpcFuture(ref, mbox, module + ":" + fun + "/" + args0.length, logCalls, this);
}
Also used : OtpMbox(com.ericsson.otp.erlang.OtpMbox) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) RpcFuture(org.erlide.runtime.rpc.RpcFuture) OtpErlangRef(com.ericsson.otp.erlang.OtpErlangRef)

Example 3 with OtpErlangRef

use of com.ericsson.otp.erlang.OtpErlangRef in project erlide_eclipse by erlang.

the class RpcMonitor method recordRequest.

public static OtpErlangRef recordRequest(final OtpNode node, final String peer, final String module, final String fun, final OtpErlangObject[] args, final long callSize) {
    RpcMonitor.callCount++;
    if (RpcMonitor.DISABLED) {
        return null;
    }
    final RpcData data = new RpcData(System.currentTimeMillis(), peer, module, fun, args, callSize);
    final OtpErlangRef ref = node.createRef();
    RpcMonitor.ongoing.put(ref, data);
    return ref;
}
Also used : OtpErlangRef(com.ericsson.otp.erlang.OtpErlangRef)

Example 4 with OtpErlangRef

use of com.ericsson.otp.erlang.OtpErlangRef 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

OtpErlangRef (com.ericsson.otp.erlang.OtpErlangRef)4 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)3 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)2 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)2 OtpErlangMap (com.ericsson.otp.erlang.OtpErlangMap)2 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)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 OtpMbox (com.ericsson.otp.erlang.OtpMbox)1 Method (java.lang.reflect.Method)1 BigInteger (java.math.BigInteger)1 Collection (java.util.Collection)1 Map (java.util.Map)1 RpcFuture (org.erlide.runtime.rpc.RpcFuture)1