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

90
LINES

< > BotCompany Repo | #1002777 // quickExport - export a structure to another bot - supports some structures

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (2831L/18K).

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 {
  Class destClass;
  Field[] fields;
}*/

static O quickExport_impl(O o, O dest, IdentityHashMap seen, HashMap<Class> 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 = newObjectArrayOfSameType(l, 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 Cl) {
    Cl l = cast o;
    int n = l.size();
    List destO = new ArrayList(n);
    seen.put(o, destO);
    Iterator it = l.iterator();
    for i to n:
      destO.add(quickExport_impl(it.next(), dest, seen, classMap));
    ret 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();
  className = loadableUtilsClassNameToMain(className);
  if (className.startsWith("main$")) {
    Class destClass = classMap.get(c);
    if (destClass == null) {
      if (!classMap.containsKey(c)) {
        if (!isAnonymousClassName(className))
          destClass = getClass_vmName_withLoadableUtils(dest, className);
        classMap.put(c, destClass);
      }
      if (destClass == null) ret o; // Class not found in target realm, keep object as is
    }
    
    if (c == destClass) ret o; // no export necessary
      
    // actually make a new object, copy fields
    
    O destO = nuObjectWithoutArguments(destClass);
    seen.put(o, destO);
    
    while (c != O.class) {
      // TODO: cache fields and destination fields
      Field[] fields = c.getDeclaredFields();
      for (Field field : fields) {
        if ((field.getModifiers() & Modifier.STATIC) != 0)
          continue;
        field.setAccessible(true);
        Object value = field.get(o);
        setOpt(destO, field.getName(), quickExport_impl(value, dest, seen, classMap));
      }
      c = c.getSuperclass();
    }
    
    return destO;
  }
  
  // assume it's a shared library object
  ret o;
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1002777
Snippet name: quickExport - export a structure to another bot - supports some structures
Eternal ID of this version: #1002777/18
Text MD5: 84ce4dd0c021e9a45abf538bac5351f5
Transpilation MD5: f0dca9bfae977dd6b5a3a7e4ee009f5e
Author: stefan
Category: eleu / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-11-02 10:56:46
Source code size: 2729 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 824 / 1130
Version history: 17 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1011700 - quickExport (optimizing) - export a structure to another bot - supports some structures
#1021089 - shallowExport - export an object to another realm (copy first level only)
#1021590 - quickExport2 - export a structure to another realm - supports more options
#1030885 - RealmCopy - successor of quickExport
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)