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

528
LINES

< > BotCompany Repo | #1034594 // structure function (v20, supports IPersistenceInfo)

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

Transpiled version (6389L) is out of date.

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

Author comment

Began life as a copy of #1030944

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034594
Snippet name: structure function (v20, supports IPersistenceInfo)
Eternal ID of this version: #1034594/7
Text MD5: 1e338348fa0812a1e982e915c148acf6
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-28 18:45:52
Source code size: 16740 bytes / 528 lines
Pitched / IR pitched: No / No
Views / Downloads: 69 / 121
Version history: 6 change(s)
Referenced in: [show references]