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).

1  
static O quickExport(O o, O dest) {
2  
  ret quickExport_impl(o, dest, new IdentityHashMap, new HashMap);
3  
}
4  
5  
static O quickExport(O o, O dest, int expectedNumberOfObjects) {
6  
  ret quickExport_impl(o, dest, new IdentityHashMap(expectedNumberOfObjects), new HashMap);
7  
}
8  
9  
/*sclass quickExport_info {
10  
  Class destClass;
11  
  Field[] fields;
12  
}*/
13  
14  
static O quickExport_impl(O o, O dest, IdentityHashMap seen, HashMap<Class> classMap) ctex {
15  
  if (o == null || o instanceof String || o instanceof Number) return o;
16  
  
17  
  O oo = seen.get(o);
18  
  if (oo != null) ret oo;
19  
  
20  
  if (o instanceof O[]) {
21  
    O[] l = (O[]) o;
22  
    O[] destO = newObjectArrayOfSameType(l, l.length);
23  
    seen.put(o, destO); 
24  
    for (int i = 0; i < l.length; i++)
25  
      destO[i] = quickExport_impl(l[i], dest, seen, classMap);
26  
    ret destO;
27  
  }
28  
  
29  
  if (o instanceof Cl) {
30  
    Cl l = cast o;
31  
    int n = l.size();
32  
    List destO = new ArrayList(n);
33  
    seen.put(o, destO);
34  
    Iterator it = l.iterator();
35  
    for i to n:
36  
      destO.add(quickExport_impl(it.next(), dest, seen, classMap));
37  
    ret destO;
38  
  }
39  
  
40  
  if (o instanceof Map) {
41  
    Map m = cast o;
42  
    Map destO = new HashMap();
43  
    seen.put(o, destO);
44  
    for (Object e : ((Map) o).entrySet())
45  
      destO.put(
46  
        quickExport_impl(((Map.Entry) e).getKey(), dest, seen, classMap),
47  
        quickExport_impl(((Map.Entry) e).getValue(), dest, seen, classMap));
48  
    return destO;
49  
  }
50  
  
51  
  Class c = o.getClass();
52  
  S className = c.getName();
53  
  className = loadableUtilsClassNameToMain(className);
54  
  if (className.startsWith("main$")) {
55  
    Class destClass = classMap.get(c);
56  
    if (destClass == null) {
57  
      if (!classMap.containsKey(c)) {
58  
        if (!isAnonymousClassName(className))
59  
          destClass = getClass_vmName_withLoadableUtils(dest, className);
60  
        classMap.put(c, destClass);
61  
      }
62  
      if (destClass == null) ret o; // Class not found in target realm, keep object as is
63  
    }
64  
    
65  
    if (c == destClass) ret o; // no export necessary
66  
      
67  
    // actually make a new object, copy fields
68  
    
69  
    O destO = nuObjectWithoutArguments(destClass);
70  
    seen.put(o, destO);
71  
    
72  
    while (c != O.class) {
73  
      // TODO: cache fields and destination fields
74  
      Field[] fields = c.getDeclaredFields();
75  
      for (Field field : fields) {
76  
        if ((field.getModifiers() & Modifier.STATIC) != 0)
77  
          continue;
78  
        field.setAccessible(true);
79  
        Object value = field.get(o);
80  
        setOpt(destO, field.getName(), quickExport_impl(value, dest, seen, classMap));
81  
      }
82  
      c = c.getSuperclass();
83  
    }
84  
    
85  
    return destO;
86  
  }
87  
  
88  
  // assume it's a shared library object
89  
  ret o;
90  
}

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: 825 / 1131
Version history: 17 change(s)
Referenced in: [show references]