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

88
LINES

< > BotCompany Repo | #1021590 // quickExport2 - export a structure to another realm - supports more options

JavaX fragment (include)

sclass quickExport2_Data {
  O dest;
  new IdentityHashMap seen;
  new HashMap<Class> classMap;
  bool copyTransientFields = true;
}

static O quickExport2(O o, O dest) {
  new quickExport2_Data data;
  data.dest = dest;
  ret quickExport2_impl(o, data);
}

static O quickExport2_impl(O o, quickExport2_Data d) ctex {
  if (o == null || o instanceof String || o instanceof Number) return o;
  
  O oo = d.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], d);
    ret destO;
  }
  
  if (o instanceof List) {
    List l = cast o;
    List destO = new ArrayList(l.size());
    d.seen.put(o, destO); 
    for (int i = 0; i < l.size(); i++)
      destO.add(quickExport_impl(l.get(i), d));
    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(), d),
        quickExport_impl(((Map.Entry) e).getValue(), d));
    return destO;
  }
  
  Class c = o.getClass();
  S className = c.getName();
  if (className.startsWith("main$")) {
    Class destClass = classMap.get(c);
    if (destClass == null) {
      if (!classMap.containsKey(c)) {
        if (!isAnonymousClassName(className))
          destClass = getClass_vmName(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
      Field[] fields = c.getDeclaredFields();
      for (Field field : fields) {
        int mod = field.getModifiers();
        if ((mod & Modifier.STATIC) != 0) continue;
        if (!d.copyTransientFields && (mod & Modifier.TRANSIENT) != 0) continue;
        field.setAccessible(true);
        Object value = field.get(o);
        setOpt(destO, field.getName(), quickExport_impl(value, d));
      }
      c = c.getSuperclass();
    }
    
    return destO;
  }
  
  // assume it's a shared library object
  ret o;
}

Author comment

Began life as a copy of #1002777

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1021590
Snippet name: quickExport2 - export a structure to another realm - supports more options
Eternal ID of this version: #1021590/1
Text MD5: 5fdd5d123f25e0a19e723048fad31d2c
Author: stefan
Category: eleu / nl
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-02-19 16:45:50
Source code size: 2506 bytes / 88 lines
Pitched / IR pitched: No / No
Views / Downloads: 210 / 250
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)