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];
}
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;
}
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;
}
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);
}
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);
}
}
}
Aggregations