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 | Constructor ctor; |
11 | Field[] fields; // source and dest field, interleaving |
12 | } |
13 | |
14 | static O quickExport_impl(O o, O dest, IdentityHashMap seen, HashMap<Class, quickExport_info> 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 = new O[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 List) { |
30 | List l = cast o; |
31 | List destO = new ArrayList(l.size()); |
32 | seen.put(o, destO); |
33 | for (int i = 0; i < l.size(); i++) |
34 | destO.add(quickExport_impl(l.get(i), dest, seen, classMap)); |
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(), dest, seen, classMap), |
45 | quickExport_impl(((Map.Entry) e).getValue(), dest, seen, classMap)); |
46 | return destO; |
47 | } |
48 | |
49 | Class c = o.getClass(); |
50 | S className = c.getName(); |
51 | if (className.startsWith("main$")) { |
52 | quickExport_info info = classMap.get(c); |
53 | if (info == null) |
54 | classMap.put(c, info = quickExport_makeInfo(c, className, dest)); |
55 | if (info.ctor == null) ret o; // Class not found in target realm, keep object as is |
56 | |
57 | // actually make a new object, copy fields |
58 | |
59 | O destO = info.ctor.newInstance(); |
60 | seen.put(o, destO); |
61 | |
62 | Field[] fields = info.fields; |
63 | int n = l(fields); |
64 | for (int i = 0; i < n; i += 2) |
65 | fields[i+1].set(destO, |
66 | quickExport_impl(fields[i].get(o), dest, seen, classMap)); |
67 | |
68 | ret destO; |
69 | } |
70 | |
71 | // assume it's a shared library object |
72 | ret o; |
73 | } |
74 | |
75 | static quickExport_info quickExport_makeInfo(Class c, S className, O dest) { |
76 | new quickExport_info info; |
77 | Class destClass = null; |
78 | if (!isAnonymousClassName(className)) |
79 | destClass = getClass_vmName(dest, className); |
80 | |
81 | if (c == destClass) destClass = null; // no export necessary |
82 | |
83 | if (destClass != null) { |
84 | info.ctor = nuObjectWithoutArguments_findConstructor(destClass); |
85 | Map<S, Field> destFields = nonStaticFieldsMap(destClass); |
86 | new L<Field> l; |
87 | while (c != O.class) { |
88 | Field[] fields = c.getDeclaredFields(); |
89 | for (Field field : fields) { |
90 | if ((field.getModifiers() & Modifier.STATIC) != 0) continue; |
91 | Field destField = destFields.get(field.getName()); |
92 | if (destField != null) { |
93 | field.setAccessible(true); |
94 | destField.setAccessible(true); |
95 | l.add(field); |
96 | l.add(destField); |
97 | } |
98 | } |
99 | c = c.getSuperclass(); |
100 | } |
101 | info.fields = l.toArray(new Field[l(l)]); |
102 | } |
103 | |
104 | ret info; |
105 | } |
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: | 344 / 575 |
Version history: | 11 change(s) |
Referenced in: | [show references] |