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

456
LINES

< > BotCompany Repo | #1030701 // structure function (v18, custom serializing through structure_Data)

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

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

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: #1030701
Snippet name: structure function (v18, custom serializing through structure_Data)
Eternal ID of this version: #1030701/10
Text MD5: 9378068b598d9c41b0c85adaf5ac80d7
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-14 16:11:49
Source code size: 14104 bytes / 456 lines
Pitched / IR pitched: No / No
Views / Downloads: 141 / 221
Version history: 9 change(s)
Referenced in: [show references]