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

475
LINES

< > BotCompany Repo | #1030383 // structure function (v18a, with serializers cache per class, dev.)

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (3385L) is out of date.

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

Author comment

Began life as a copy of #1024876

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030383
Snippet name: structure function (v18a, with serializers cache per class, dev.)
Eternal ID of this version: #1030383/8
Text MD5: 3a4878973516a58c8ad4f7d98710c39b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-03-16 15:16:00
Source code size: 14251 bytes / 475 lines
Pitched / IR pitched: No / No
Views / Downloads: 97 / 147
Version history: 7 change(s)
Referenced in: [show references]