Search in sources :

Example 1 with OtpErlangBinary

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

the class BuilderHelper method loadModule.

public static void loadModule(@NonNull final IProject project, final String module) {
    try {
        final IBackendManager backendManager = BackendCore.getBackendManager();
        for (final IBackend b : backendManager.getExecutionBackends(project)) {
            ErlLogger.debug(":: loading %s in %s", module, b.getName());
            final IErlProject erlProject = ErlangEngine.getInstance().getModel().findProject(project);
            final IPath path = project.getLocation().append(erlProject.getProperties().getOutputDir()).append(module + ".beam");
            boolean ok = false;
            final OtpErlangBinary bin = BeamUtil.getBeamBinary(module, path);
            if (bin != null) {
                ok = BeamLoader.loadBeam(b.getOtpRpc(), module, bin);
            }
            if (!ok) {
                ErlLogger.error("Could not load %s", module);
            }
            backendManager.moduleLoaded(b, project, module);
        }
    } catch (final Exception e) {
        ErlLogger.debug(e);
    }
}
Also used : IErlProject(org.erlide.engine.model.root.IErlProject) IBackendManager(org.erlide.backend.api.IBackendManager) IPath(org.eclipse.core.runtime.IPath) IBackend(org.erlide.backend.api.IBackend) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary) CoreException(org.eclipse.core.runtime.CoreException) ErlModelException(org.erlide.engine.model.ErlModelException) RpcException(org.erlide.runtime.rpc.RpcException)

Example 2 with OtpErlangBinary

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

the class TypeConverter method cvtString.

private static String cvtString(final OtpErlangObject obj) throws SignatureException {
    if (obj instanceof OtpErlangString) {
        return ((OtpErlangString) obj).stringValue();
    }
    if (obj instanceof OtpErlangAtom) {
        return ((OtpErlangAtom) obj).atomValue();
    }
    if (obj instanceof OtpErlangBinary) {
        return new String(((OtpErlangBinary) obj).binaryValue());
    }
    if (obj instanceof OtpErlangList) {
        final StringBuilder res = new StringBuilder();
        for (final OtpErlangObject el : (OtpErlangList) obj) {
            if (el instanceof OtpErlangLong) {
                final long l = ((OtpErlangLong) el).longValue();
                res.append((char) (l & 0xFFFF));
            } else {
                res.append(TypeConverter.erlang2java(el, String.class));
            }
        }
        return res.toString();
    }
    throw new SignatureException(TypeConverter.WRONG_ARG_TYPE + obj.getClass().getName() + TypeConverter.CANT_CONVERT_TO + "String");
}
Also used : OtpErlangLong(com.ericsson.otp.erlang.OtpErlangLong) OtpErlangList(com.ericsson.otp.erlang.OtpErlangList) OtpErlangObject(com.ericsson.otp.erlang.OtpErlangObject) OtpErlangAtom(com.ericsson.otp.erlang.OtpErlangAtom) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary) OtpErlangString(com.ericsson.otp.erlang.OtpErlangString)

Example 3 with OtpErlangBinary

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

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

the class Backend method loadBeamsFromDir.

private void loadBeamsFromDir(final String outDir) {
    final File dir = new File(outDir);
    if (dir.isDirectory()) {
        for (final File f : dir.listFiles()) {
            final Path path = new Path(f.getPath());
            if (path.getFileExtension() != null && "beam".compareTo(path.getFileExtension()) == 0) {
                final String m = path.removeFileExtension().lastSegment();
                try {
                    boolean ok = false;
                    final OtpErlangBinary bin = BeamUtil.getBeamBinary(m, path);
                    if (bin != null) {
                        ok = BeamLoader.loadBeam(getOtpRpc(), m, bin);
                    }
                    if (!ok) {
                        ErlLogger.error("Could not load %s", m);
                    }
                } catch (final Exception ex) {
                    ErlLogger.warn(ex);
                }
            }
        }
    }
}
Also used : Path(org.eclipse.core.runtime.Path) File(java.io.File) OtpErlangBinary(com.ericsson.otp.erlang.OtpErlangBinary) DebugException(org.eclipse.debug.core.DebugException) IOException(java.io.IOException)

Example 5 with OtpErlangBinary

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

Aggregations

OtpErlangBinary (com.ericsson.otp.erlang.OtpErlangBinary)13 OtpErlangObject (com.ericsson.otp.erlang.OtpErlangObject)8 OtpErlangString (com.ericsson.otp.erlang.OtpErlangString)8 OtpErlangList (com.ericsson.otp.erlang.OtpErlangList)5 OtpErlangTuple (com.ericsson.otp.erlang.OtpErlangTuple)5 OtpErlangAtom (com.ericsson.otp.erlang.OtpErlangAtom)4 OtpErlangLong (com.ericsson.otp.erlang.OtpErlangLong)4 Test (org.junit.Test)3 RpcException (org.erlide.runtime.rpc.RpcException)2 OtpErlangChar (com.ericsson.otp.erlang.OtpErlangChar)1 OtpErlangException (com.ericsson.otp.erlang.OtpErlangException)1 OtpErlangMap (com.ericsson.otp.erlang.OtpErlangMap)1 OtpErlangPid (com.ericsson.otp.erlang.OtpErlangPid)1 OtpErlangRangeException (com.ericsson.otp.erlang.OtpErlangRangeException)1 OtpErlangRef (com.ericsson.otp.erlang.OtpErlangRef)1 Subscribe (com.google.common.eventbus.Subscribe)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1