Search in sources :

Example 6 with OtpErlangString

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

the class TypeConverter method cvtArray.

private static Object cvtArray(final OtpErlangObject obj, final Class<?> cls) throws SignatureException {
    OtpErlangObject[] els = null;
    if (obj instanceof OtpErlangList) {
        els = ((OtpErlangList) obj).elements();
    }
    if (obj instanceof OtpErlangTuple) {
        els = ((OtpErlangTuple) obj).elements();
    }
    if (els != null) {
        final Object arr = Array.newInstance(cls.getComponentType(), els.length);
        for (int i = 0; i < els.length; i++) {
            Array.set(arr, i, TypeConverter.erlang2java(els[i], cls.getComponentType()));
        }
        return arr;
    }
    if (obj instanceof OtpErlangString) {
        final byte[] s = ((OtpErlangString) obj).stringValue().getBytes();
        final Object arr = Array.newInstance(cls.getComponentType(), s.length);
        for (int i = 0; i < s.length; i++) {
            Array.set(arr, i, s[i]);
        }
        return arr;
    }
    return new Object[0];
}
Also used : OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 7 with OtpErlangString

use of com.ericsson.otp.erlang.OtpErlangString 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 8 with OtpErlangString

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

the class IndexedErlangValue method checkList.

private OtpErlangList checkList(final OtpErlangObject theValue) {
    if (theValue instanceof OtpErlangString) {
        final OtpErlangString os = (OtpErlangString) theValue;
        final String s = os.stringValue();
        // TODO real encoding?
        final byte[] b = s.getBytes(Charsets.ISO_8859_1);
        if (!IndexedErlangValue.looksLikeAscii(b)) {
            return new OtpErlangList(s);
        }
    }
    return null;
}
Also used : OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 9 with OtpErlangString

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

the class ErlangDebugTarget method distributeDebuggerCode.

private void distributeDebuggerCode() {
    final List<String> debuggerModules = getDebuggerModules();
    final List<OtpErlangTuple> modules = new ArrayList<>(debuggerModules.size());
    final String ver = backend.getRuntime().getVersion().asMajor().toString().toLowerCase();
    for (final String module : debuggerModules) {
        final OtpErlangBinary b = getDebuggerBeam(module, "org.erlide.kernel");
        if (b != null) {
            final OtpErlangString filename = new OtpErlangString(module + ".erl");
            final OtpErlangTuple t = OtpErlang.mkTuple(new OtpErlangAtom(module), filename, b);
            modules.add(t);
        } else {
            ErlLogger.warn("Could not find debugger module %s (%s)", module, ver);
        }
    }
    ErlideDebug.distributeDebuggerCode(backend.getOtpRpc(), modules);
}
Also used : ArrayList(java.util.ArrayList) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 10 with OtpErlangString

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

the class ErlangCompletionService method addFunctionProposalsWithDoc.

void addFunctionProposalsWithDoc(final int offset, final String aprefix, final List<CompletionData> result, final OtpErlangObject res, final IErlImport erlImport, final boolean arityOnly) {
    if (res instanceof OtpErlangList) {
        final OtpErlangList resl = (OtpErlangList) res;
        for (final OtpErlangObject i : resl) {
            // {FunWithArity, FunWithParameters, [{Offset, Length}], Doc}
            final OtpErlangTuple f = (OtpErlangTuple) i;
            final String funWithArity = ((OtpErlangString) f.elementAt(0)).stringValue();
            if (!ErlangCompletionService.filterImported(erlImport, funWithArity)) {
                continue;
            }
            String funWithParameters = arityOnly ? funWithArity : ((OtpErlangString) f.elementAt(1)).stringValue();
            final OtpErlangList parOffsets = (OtpErlangList) f.elementAt(2);
            String docStr = null;
            if (f.arity() > 3) {
                final OtpErlangObject elt = f.elementAt(3);
                if (elt instanceof OtpErlangString) {
                    docStr = Util.stringValue(elt);
                }
            }
            funWithParameters = funWithParameters.substring(aprefix.length());
            final List<Location> offsetsAndLengths = new ArrayList<>();
            if (!arityOnly) {
                addOffsetsAndLengths(parOffsets, offset, offsetsAndLengths);
            }
            addFunctionCompletion(offset, result, funWithArity, docStr, funWithParameters, offsetsAndLengths);
        }
    }
}
Also used : OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) ArrayList(java.util.ArrayList) OtpErlangTuple(com.ericsson.otp.erlang.OtpErlangTuple) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) Location(org.erlide.engine.services.codeassist.Location)

Aggregations

OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)56 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)36 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)31 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)22 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)15 ArrayList (java.util.ArrayList)9 RpcResult (org.erlide.runtime.rpc.RpcResult)8 OtpErlangLong (com.ericsson.otp.erlang.OtpErlangLong)7 RpcException (org.erlide.runtime.rpc.RpcException)7 Test (org.junit.Test)7 OtpErlangRangeException (com.ericsson.otp.erlang.OtpErlangRangeException)6 OtpErlangBinary (com.ericsson.otp.erlang.OtpErlangBinary)5 IPath (org.eclipse.core.runtime.IPath)5 ErlModelException (org.erlide.engine.model.ErlModelException)5 IErlElement (org.erlide.engine.model.IErlElement)5 WranglerRpcParsingException (org.erlide.wrangler.refactoring.exception.WranglerRpcParsingException)5 IErlMemberSelection (org.erlide.wrangler.refactoring.selection.IErlMemberSelection)5 CoreException (org.eclipse.core.runtime.CoreException)4 WranglerException (org.erlide.wrangler.refactoring.exception.WranglerException)4 Map (java.util.Map)3