Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

105
LINES

< > BotCompany Repo | #1011700 // quickExport (optimizing) - export a structure to another bot - supports some structures

JavaX fragment (include)

static O quickExport(O o, O dest) {
  ret quickExport_impl(o, dest, new IdentityHashMap, new HashMap);
}

static O quickExport(O o, O dest, int expectedNumberOfObjects) {
  ret quickExport_impl(o, dest, new IdentityHashMap(expectedNumberOfObjects), new HashMap);
}

sclass quickExport_info {
  Constructor ctor;
  Field[] fields; // source and dest field, interleaving
}

static O quickExport_impl(O o, O dest, IdentityHashMap seen, HashMap<Class, quickExport_info> classMap) ctex {
  if (o == null || o instanceof String || o instanceof Number) return o;
  
  O oo = seen.get(o);
  if (oo != null) ret oo;
  
  if (o instanceof O[]) {
    O[] l = (O[]) o;
    O[] destO = new O[l.length];
    seen.put(o, destO); 
    for (int i = 0; i < l.length; i++)
      destO[i] = quickExport_impl(l[i], dest, seen, classMap);
    ret destO;
  }
  
  if (o instanceof List) {
    List l = cast o;
    List destO = new ArrayList(l.size());
    seen.put(o, destO); 
    for (int i = 0; i < l.size(); i++)
      destO.add(quickExport_impl(l.get(i), dest, seen, classMap));
    return destO;
  }
  
  if (o instanceof Map) {
    Map m = cast o;
    Map destO = new HashMap();
    seen.put(o, destO);
    for (Object e : ((Map) o).entrySet())
      destO.put(
        quickExport_impl(((Map.Entry) e).getKey(), dest, seen, classMap),
        quickExport_impl(((Map.Entry) e).getValue(), dest, seen, classMap));
    return destO;
  }
  
  Class c = o.getClass();
  S className = c.getName();
  if (className.startsWith("main$")) {
    quickExport_info info = classMap.get(c);
    if (info == null)
      classMap.put(c, info = quickExport_makeInfo(c, className, dest));
    if (info.ctor == null) ret o; // Class not found in target realm, keep object as is

    // actually make a new object, copy fields
    
    O destO = info.ctor.newInstance();
    seen.put(o, destO);
    
    Field[] fields = info.fields;
    int n = l(fields);
    for (int i = 0; i < n; i += 2)
      fields[i+1].set(destO,
        quickExport_impl(fields[i].get(o), dest, seen, classMap));

    ret destO;
  }
  
  // assume it's a shared library object
  ret o;
}

static quickExport_info quickExport_makeInfo(Class c, S className, O dest) {
  new quickExport_info info;
  Class destClass = null;
  if (!isAnonymousClassName(className))
    destClass = getClass_vmName(dest, className);
  
  if (c == destClass) destClass = null; // no export necessary
  
  if (destClass != null) {
    info.ctor = nuObjectWithoutArguments_findConstructor(destClass);
    Map<S, Field> destFields = nonStaticFieldsMap(destClass);
    new L<Field> l;
    while (c != O.class) {
      Field[] fields = c.getDeclaredFields();
      for (Field field : fields) {
        if ((field.getModifiers() & Modifier.STATIC) != 0) continue;
        Field destField = destFields.get(field.getName());
        if (destField != null) {
          field.setAccessible(true);
          destField.setAccessible(true);
          l.add(field);
          l.add(destField);
        }
      }
      c = c.getSuperclass();
    }
    info.fields = l.toArray(new Field[l(l)]);
  }

  ret info;
}

Author comment

Began life as a copy of #1002777

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1011700
Snippet name: quickExport (optimizing) - export a structure to another bot - supports some structures
Eternal ID of this version: #1011700/12
Text MD5: 89ce0f56f269d14d8eb0172bb31cc462
Author: stefan
Category: eleu / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-11-02 08:17:34
Source code size: 3216 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 340 / 572
Version history: 11 change(s)
Referenced in: [show references]