1 | static bool structure_showTiming, structure_checkTokenCount; |
2 | |
3 | static S structure(O o) { |
4 | ret structure(o, new structure_Data); |
5 | } |
6 | |
7 | static S structure(O o, structure_Data d) { |
8 | new StringWriter sw; |
9 | d.out = new PrintWriter(sw); |
10 | structure_go(o, d); |
11 | S s = str(sw); |
12 | if (structure_checkTokenCount) { |
13 | print("token count=" + d.n); |
14 | assertEquals("token count", l(javaTokC(s)), d.n); |
15 | } |
16 | ret s; |
17 | } |
18 | |
19 | static void structure_go(O o, structure_Data d) { |
20 | structure_1(o, d); |
21 | while (nempty(d.stack)) |
22 | popLast(d.stack).run(); |
23 | } |
24 | |
25 | static void structureToPrintWriter(O o, PrintWriter out) { |
26 | new structure_Data d; |
27 | d.out = out; |
28 | structure_go(o, d); |
29 | } |
30 | |
31 | // leave to false, unless unstructure() breaks |
32 | static boolean structure_allowShortening = false; |
33 | |
34 | static class structure_Data { |
35 | PrintWriter out; |
36 | int stringSizeLimit; |
37 | int shareStringsLongerThan = 20; |
38 | bool noStringSharing; |
39 | |
40 | new IdentityHashMap<O, Integer> seen; |
41 | //new BitSet refd; |
42 | new HashMap<S, Int> strings; |
43 | new HashSet<S> concepts; |
44 | HashMap<Class, L<Field>> fieldsByClass = new HashMap; |
45 | new HashMap<Class, Field> persistenceInfo; |
46 | int n; // token count |
47 | new L<Runnable> stack; |
48 | |
49 | // append single token |
50 | structure_Data append(S token) { out.print(token); ++n; ret this; } |
51 | structure_Data append(int i) { out.print(i); ++n; ret this; } |
52 | |
53 | // append multiple tokens |
54 | structure_Data append(S token, int tokCount) { out.print(token); n += tokCount; ret this; } |
55 | |
56 | // extend last token |
57 | structure_Data app(S token) { out.print(token); ret this; } |
58 | structure_Data app(int i) { out.print(i); ret this; } |
59 | } |
60 | |
61 | static void structure_1(final Object o, final structure_Data d) ctex { |
62 | if (o == null) { d.append("null"); ret; } |
63 | |
64 | Class c = o.getClass(); |
65 | bool concept = false; |
66 | ifclass Concept |
67 | concept = o instanceof Concept; |
68 | endif |
69 | L<Field> lFields = d.fieldsByClass.get(c); |
70 | |
71 | if (lFields == null) { |
72 | // these are never back-referenced (for readability) |
73 | |
74 | if (o instanceof Number) { |
75 | PrintWriter out = d.out; |
76 | if (o instanceof Integer) { int i = ((Int) o).intValue(); out.print(i); d.n += i < 0 ? 2 : 1; ret; } |
77 | if (o instanceof Long) { long l = ((Long) o).longValue(); out.print(l); out.print("L"); d.n += l < 0 ? 2 : 1; ret; } |
78 | if (o cast Short) { short s = o.shortValue(); d.append("sh "); out.print(s); d.n += s < 0 ? 2 : 1; ret; } |
79 | if (o instanceof Float) { d.append("fl ", 2); quoteToPrintWriter(str(o), out); ret; } |
80 | if (o instanceof Double) { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); ret; } |
81 | if (o instanceof BigInteger) { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; ret; } |
82 | } |
83 | |
84 | if (o instanceof Boolean) { |
85 | d.append(((Boolean) o).booleanValue() ? "t" : "f"); ret; |
86 | } |
87 | |
88 | if (o instanceof Character) { |
89 | d.append(quoteCharacter((Character) o)); ret; |
90 | } |
91 | |
92 | if (o instanceof File) { |
93 | d.append("File ").append(quote(((File) o).getPath())); ret; |
94 | } |
95 | |
96 | // referencable objects follow |
97 | |
98 | Int ref = d.seen.get(o); |
99 | if (o instanceof S && ref == null) ref = d.strings.get((S) o); |
100 | if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); ret; } |
101 | |
102 | if (!o instanceof S) |
103 | d.seen.put(o, d.n); // record token number |
104 | else { |
105 | S s = d.stringSizeLimit != 0 ? shorten((S) o, d.stringSizeLimit) : (S) o; |
106 | if (!d.noStringSharing) { |
107 | if (d.shareStringsLongerThan == Int.MAX_VALUE) |
108 | d.seen.put(o, d.n); |
109 | if (l(s) >= d.shareStringsLongerThan) |
110 | d.strings.put(s, d.n); |
111 | } |
112 | quoteToPrintWriter(s, d.out); d.n++; ret; |
113 | } |
114 | |
115 | if (o cast Set) { |
116 | /*O set2 = unwrapSynchronizedSet(o); |
117 | if (set2 != o) { |
118 | d.append("sync"); |
119 | o = set2; |
120 | } TODO */ |
121 | |
122 | if (o instanceof TreeSet) { |
123 | d.append(isCISet_gen(o) ? "ciset" : "treeset"); |
124 | structure_1(new ArrayList(o), d); |
125 | ret; |
126 | } |
127 | |
128 | // assume it's a HashSet or LinkedHashSet |
129 | d.append(o instanceof LinkedHashSet ? "lhs" : "hashset"); |
130 | structure_1(new ArrayList(o), d); |
131 | ret; |
132 | } |
133 | |
134 | S name = c.getName(); |
135 | |
136 | if (o instanceof Collection |
137 | && !startsWith(name, "main$") |
138 | /* && neq(name, "main$Concept$RefL") */) { |
139 | |
140 | // it's a list |
141 | |
142 | if (name.equals("java.util.Collections$SynchronizedList") |
143 | || name.equals("java.util.Collections$SynchronizedRandomAccessList")) { |
144 | if (unwrapSynchronizedList(o/List) instanceof LinkedList) |
145 | d.append("syncLL"); |
146 | else |
147 | d.append("sync"); |
148 | } |
149 | else if (name.equals("java.util.LinkedList")) d.append("ll"); |
150 | d.append("["); |
151 | final int l = d.n; |
152 | final Iterator it = ((Collection) o).iterator(); |
153 | d.stack.add(r { |
154 | if (!it.hasNext()) |
155 | d.append("]"); |
156 | else { |
157 | d.stack.add(this); |
158 | if (d.n != l) d.append(", "); |
159 | structure_1(it.next(), d); |
160 | } |
161 | }); |
162 | ret; |
163 | } |
164 | |
165 | if (o instanceof Map) { |
166 | if (startsWith(name, "main$")) { |
167 | if (isTrue(getOpt(o, '_serializeAsMap))) { |
168 | ret; |
169 | } |
170 | // !_serializeAsMap => proceed to normal object serialisation |
171 | } else { // standard map |
172 | if (o instanceof LinkedHashMap) d.append("lhm"); |
173 | else if (o instanceof HashMap) d.append("hm"); |
174 | else if (o cast TreeMap) |
175 | d.append(isCIMap_gen(o) ? "cimap" : "tm"); |
176 | else if (name.equals("java.util.Collections$SynchronizedMap")) d.append("sync"); |
177 | else if (name.equals("java.util.Collections$SynchronizedSortedMap")) { d.append("sync tm", 2); } |
178 | |
179 | d.append("{"); |
180 | final int l = d.n; |
181 | final Iterator it = ((Map) o).entrySet().iterator(); |
182 | |
183 | d.stack.add(new Runnable() { |
184 | bool v; |
185 | Map.Entry e; |
186 | |
187 | public void run() { |
188 | if (v) { |
189 | d.append("="); |
190 | v = false; |
191 | d.stack.add(this); |
192 | structure_1(e.getValue(), d); |
193 | } else { |
194 | if (!it.hasNext()) |
195 | d.append("}"); |
196 | else { |
197 | e = (Map.Entry) it.next(); |
198 | v = true; |
199 | d.stack.add(this); |
200 | if (d.n != l) d.append(", "); |
201 | structure_1(e.getKey(), d); |
202 | } |
203 | } |
204 | } |
205 | }); |
206 | ret; |
207 | } |
208 | } |
209 | |
210 | if (c.isArray()) { |
211 | if (o instanceof byte[]) { |
212 | d.append("ba ").append(quote(bytesToHex((byte[]) o))); ret; |
213 | } |
214 | |
215 | final int n = Array.getLength(o); |
216 | |
217 | if (o instanceof bool[]) { |
218 | S hex = boolArrayToHex((bool[]) o); |
219 | int i = l(hex); |
220 | while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2; |
221 | d.append("boolarray ").append(n).app(" ").append(quote(substring(hex, 0, i))); ret; |
222 | } |
223 | |
224 | S atype = "array", sep = ", "; |
225 | |
226 | if (o instanceof int[]) { |
227 | //ret "intarray " + quote(intArrayToHex((int[]) o)); |
228 | atype = "intarray"; |
229 | sep = " "; |
230 | } |
231 | |
232 | d.append(atype).append("{"); |
233 | d.stack.add(new Runnable() { |
234 | int i; |
235 | public void run() { |
236 | if (i >= n) |
237 | d.append("}"); |
238 | else { |
239 | d.stack.add(this); |
240 | if (i > 0) d.append(", "); |
241 | structure_1(Array.get(o, i++), d); |
242 | } |
243 | } |
244 | }); |
245 | ret; |
246 | } |
247 | |
248 | if (o instanceof Class) { |
249 | d.append("class(", 2).append(quote(((Class) o).getName())).append(")"); ret; |
250 | } |
251 | |
252 | if (o instanceof Throwable) { |
253 | d.append("exception(", 2).append(quote(((Throwable) o).getMessage())).append(")"); ret; |
254 | } |
255 | |
256 | if (o instanceof BitSet) { |
257 | BitSet bs = (BitSet) o; |
258 | d.append("bitset{", 2); |
259 | int l = d.n; |
260 | for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { |
261 | if (d.n != l) d.append(", "); |
262 | d.append(i); |
263 | } |
264 | d.append("}"); ret; |
265 | } |
266 | |
267 | // Need more cases? This should cover all library classes... |
268 | if (name.startsWith("java.") || name.startsWith("javax.")) { |
269 | d.append("j ").append(quote(str(o))); ret; // Hm. this is not unstructure-able |
270 | } |
271 | |
272 | ifclass Lisp |
273 | if (o instanceof Lisp) { |
274 | d.append("l(", 2); |
275 | final Lisp lisp = cast o; |
276 | structure_1(lisp.head, d); |
277 | final Iterator it = lisp.args == null ? emptyIterator() : lisp.args.iterator(); |
278 | d.stack.add(r { |
279 | if (!it.hasNext()) |
280 | d.append(")"); |
281 | else { |
282 | d.stack.add(this); |
283 | d.append(", "); |
284 | structure_1(it.next(), d); |
285 | } |
286 | }); |
287 | ret; |
288 | } |
289 | endif |
290 | |
291 | /*if (name.equals("main$Lisp")) { |
292 | fail("lisp not supported right now"); |
293 | }*/ |
294 | |
295 | structure_writeCustomObject(o, d); |
296 | } |
297 | |
298 | |
299 | |
300 | S dynName = shortDynamicClassName(o); |
301 | if (concept && !d.concepts.contains(dynName)) { |
302 | d.concepts.add(dynName); |
303 | d.append("c "); |
304 | } |
305 | |
306 | // serialize an object with fields. |
307 | // first, collect all fields and values in fv. |
308 | |
309 | TreeSet<Field> fields = new TreeSet<Field>(new Comparator<Field>() { |
310 | public int compare(Field a, Field b) { |
311 | ret stdcompare(a.getName(), b.getName()); |
312 | } |
313 | }); |
314 | |
315 | Class cc = c; |
316 | while (cc != Object.class) { |
317 | for (Field field : getDeclaredFields_cached(cc)) { |
318 | S fieldName = field.getName(); |
319 | if (fieldName.equals("_persistenceInfo")) |
320 | d.persistenceInfo.put(c, field); |
321 | if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0) |
322 | continue; |
323 | |
324 | fields.add(field); |
325 | |
326 | // put special cases here...? |
327 | } |
328 | |
329 | cc = cc.getSuperclass(); |
330 | } |
331 | |
332 | // TODO: S fieldOrder = getOpt(c, "_fieldOrder"); |
333 | lFields = asList(fields); |
334 | |
335 | // Render this$1 first because unstructure needs it for constructor call. |
336 | |
337 | for (int i = 0; i < l(lFields); i++) { |
338 | Field f = lFields.get(i); |
339 | if (f.getName().equals("this$1")) { |
340 | lFields.remove(i); |
341 | lFields.add(0, f); |
342 | break; |
343 | } |
344 | } |
345 | |
346 | ifdef structure_debug |
347 | print("Saving fields for " + c + ": " + lFields); |
348 | endifdef |
349 | d.fieldsByClass.put(c, lFields); |
350 | } // << if (lFields == null) |
351 | else { // ref handling for lFields != null |
352 | Int ref = d.seen.get(o); |
353 | if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); ret; } |
354 | d.seen.put(o, d.n); // record token number |
355 | } |
356 | |
357 | Field persistenceInfoField = cast d.persistenceInfo.get(c); |
358 | Map<S, O> persistenceInfo = persistenceInfoField == null ? null : (Map) persistenceInfoField.get(o); |
359 | ifdef structure_debug |
360 | print("persistenceInfo for " + c + ": " + persistenceInfo); |
361 | endifdef |
362 | new LinkedHashMap<S, O> fv; |
363 | for (Field f : lFields) { |
364 | Object value; |
365 | try { |
366 | value = f.get(o); |
367 | } catch (Exception e) { |
368 | value = "?"; |
369 | } |
370 | |
371 | if (value != null && (persistenceInfo == null |
372 | || !Boolean.FALSE.equals(persistenceInfo.get(f.getName())))) |
373 | fv.put(f.getName(), value); |
374 | ifdef structure_debug |
375 | else print("Skipping null field " + f.getName() + " for " + identityHashCode(o)); |
376 | endifdef |
377 | } |
378 | |
379 | S name = c.getName(); |
380 | String shortName = dropPrefix("main$", name); |
381 | if (startsWithDigit(shortName)) shortName = name; // for anonymous classes |
382 | |
383 | // Now we have fields & values. Process fieldValues if it's a DynamicObject. |
384 | |
385 | // omit field "className" if equal to class's name |
386 | if (concept && eq(fv.get("className"), shortName)) |
387 | fv.remove("className"); |
388 | |
389 | if (o instanceof DynamicObject) { |
390 | putAll(fv, (Map) fv.get("fieldValues")); |
391 | fv.remove("fieldValues"); |
392 | shortName = shortDynamicClassName(o); |
393 | fv.remove("className"); |
394 | } |
395 | |
396 | S singleField = fv.size() == 1 ? first(fv.keySet()) : null; |
397 | |
398 | d.append(shortName); |
399 | d.n += countDots(shortName)*2; // correct token count |
400 | ifdef structure_debug |
401 | print("Fields for " + shortName + ": " + fv.keySet()); |
402 | endifdef |
403 | |
404 | final int l = d.n; |
405 | final Iterator it = fv.entrySet().iterator(); |
406 | |
407 | d.stack.add(r { |
408 | if (!it.hasNext()) { |
409 | if (d.n != l) |
410 | d.append(")"); |
411 | } else { |
412 | Map.Entry e = (Map.Entry) it.next(); |
413 | d.append(d.n == l ? "(" : ", "); |
414 | d.append((S) e.getKey()).append("="); |
415 | d.stack.add(this); |
416 | structure_1(e.getValue(), d); |
417 | } |
418 | }); |
419 | } |
Began life as a copy of #1009411
download show line numbers debug dex old transpilations
Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1024863 |
Snippet name: | structure function (v15, custom maps, dev.) |
Eternal ID of this version: | #1024863/3 |
Text MD5: | cb8363aa17b73417bc50fb92ece70bb8 |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-08-27 20:37:47 |
Source code size: | 12890 bytes / 419 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 176 / 207 |
Version history: | 2 change(s) |
Referenced in: | [show references] |