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)

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

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: 209 / 249
Referenced in: [show references]