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

297
LINES

< > BotCompany Repo | #1005593 // structure function (v8, outdated)

JavaX fragment (include)

1  
sbool structure_showTiming;
2  
3  
static String structure(Object o) {
4  
  new structure_Data d;
5  
  structure_1(o, d);
6  
  long time = now();
7  
  S s = str(d.out);
8  
  int nOrig = l(s);
9  
  d.out = null;
10  
  s = structure_2(s, d.refd);
11  
  if (structure_showTiming)
12  
    done_always("structure_2 (" + nOrig + " => " + l(s) + ")", time);
13  
  ret s;
14  
}
15  
16  
// leave to false, unless unstructure() breaks
17  
static boolean structure_allowShortening = false;
18  
19  
static int structure_shareStringsLongerThan = 20;
20  
21  
static class structure_Data {
22  
  new StringBuilder out;
23  
  int stringSizeLimit;
24  
  new IdentityHashMap<O, Integer> seen;
25  
  new BitSet refd;
26  
  new HashMap<S, Int> strings;
27  
  new HashSet<S> concepts;
28  
  Class conceptClass = findClass("Concept");
29  
}
30  
31  
static void structure_1(Object o, structure_Data d) {
32  
  StringBuilder out = d.out;
33  
  
34  
  if (o == null) { out.append("null"); ret; }
35  
  
36  
  // these are never back-referenced (for readability)
37  
  
38  
  if (o instanceof Number) {
39  
    if (o instanceof Integer) { out.append((int) o); ret; }
40  
    if (o instanceof Long) { out.append((long) o).append("L"); ret; }
41  
    if (o instanceof Float) { out.append("fl "); quote_impl(str(o), out); ret; }
42  
    if (o instanceof Double) { out.append("d("); quote_impl(str(o), out); out.append(")"); ret; }
43  
    if (o instanceof BigInteger) { out.append("bigint(").append(o).append(")"); ret; }
44  
  }
45  
46  
  if (o instanceof Boolean) {
47  
    out.append(((Boolean) o).booleanValue() ? "t" : "f"); ret;
48  
  }
49  
    
50  
  if (o instanceof Character) {
51  
    out.append(quoteCharacter((Character) o)); ret;
52  
  }
53  
    
54  
  if (o instanceof File) {
55  
    out.append("File ").append(quote(((File) o).getPath())); ret;
56  
  }
57  
    
58  
  // referencable objects follow
59  
  
60  
  Integer ref = null;
61  
  if (!(o instanceof S)) {
62  
    ref = d.seen.get(o);
63  
    if (ref != null) {
64  
      d.refd.set(ref);
65  
      out.append("r").append(ref); ret;
66  
    }
67  
  }
68  
  
69  
  if (o instanceof S && (ref = d.strings.get((S) o)) != null) {
70  
    d.refd.set(ref);
71  
    out.append("r").append(ref); ret;
72  
  }
73  
  
74  
  ref = d.seen.size()+1;
75  
  d.seen.put(o, ref);
76  
  out.append("m").append(ref).append(" "); // marker
77  
78  
  if (o instanceof S) {
79  
    S s = d.stringSizeLimit != 0 ? shorten((S) o, d.stringSizeLimit) : (S) o;
80  
    if (l(s) >= structure_shareStringsLongerThan)
81  
      d.strings.put(s, ref);
82  
    quote_impl(s, out); ret;
83  
  }
84  
    
85  
  if (o instanceof HashSet) {
86  
    out.append("hashset ");
87  
    structure_1(new ArrayList((Set) o), d);
88  
    ret;
89  
  }
90  
91  
  if (o instanceof TreeSet) {
92  
    out.append("treeset ");
93  
    structure_1(new ArrayList((Set) o), d);
94  
    ret;
95  
  }
96  
  
97  
  Class c = o.getClass();
98  
  String name = c.getName();
99  
  
100  
  if (o instanceof Collection && neq(name, "main$Concept$RefL")) {
101  
    out.append("[");
102  
    int l = out.length();
103  
    for (Object x : (Collection) o) {
104  
      if (out.length() != l) out.append(", ");
105  
      structure_1(x, d);
106  
    }
107  
    out.append("]");
108  
    ret;
109  
  }
110  
  
111  
  if (o instanceof Map) {
112  
    if (o instanceof HashMap) out.append("hm");
113  
    out.append("{");
114  
    int l = out.length();
115  
    for (Object e : ((Map) o).entrySet()) {
116  
      if (out.length() != l) out.append(", ");
117  
      structure_1(((Map.Entry) e).getKey(), d);
118  
      out.append("=");
119  
      structure_1(((Map.Entry) e).getValue(), d);
120  
    }
121  
    out.append("}");
122  
    ret;
123  
  }
124  
  
125  
  if (c.isArray()) {
126  
    if (o instanceof byte[]) {
127  
      out.append("ba ").append(quote(bytesToHex((byte[]) o))); ret;
128  
    }
129  
130  
    int n = Array.getLength(o);
131  
132  
    if (o instanceof bool[]) {
133  
      S hex = boolArrayToHex((bool[]) o);
134  
      int i = l(hex);
135  
      while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2;
136  
      out.append("boolarray ").append(n).append(" ").append(quote(substring(hex, 0, i))); ret;
137  
    }
138  
    
139  
    S atype = "array", sep = ", ";
140  
141  
    if (o instanceof int[]) {
142  
      //ret "intarray " + quote(intArrayToHex((int[]) o));
143  
      atype = "intarray";
144  
      sep = " ";
145  
    }
146  
    
147  
    out.append(atype).append("{");
148  
    for (int i = 0; i < n; i++) {
149  
      if (i != 0) out.append(sep);
150  
      structure_1(Array.get(o, i), d);
151  
    }
152  
    out.append("}"); ret;
153  
  }
154  
155  
  if (o instanceof Class) {
156  
    out.append("class(").append(quote(((Class) o).getName())).append(")"); ret;
157  
  }
158  
    
159  
  if (o instanceof Throwable) {
160  
    out.append("exception(").append(quote(((Throwable) o).getMessage())).append(")"); ret;
161  
  }
162  
    
163  
  if (o instanceof BitSet) {
164  
    BitSet bs = (BitSet) o;
165  
    out.append("bitset{");
166  
    int l = out.length();
167  
    for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
168  
      if (out.length() != l) out.append(", ");
169  
      out.append(i);
170  
    }
171  
    out.append("}"); ret;
172  
  }
173  
    
174  
  // Need more cases? This should cover all library classes...
175  
  if (name.startsWith("java.") || name.startsWith("javax.")) {
176  
    out.append(str(o)); ret;
177  
  }
178  
    
179  
  if (name.equals("main$Lisp")) {
180  
    out.append("l(");
181  
    structure_1(getOpt(o, "head"), d);
182  
    L args = cast getOpt(o, "args");
183  
    if (nempty(args))
184  
      for (int i = 0; i < l(args); i++) {
185  
        out.append(", ");
186  
        O arg = args.get(i);
187  
        
188  
        // sweet shortening
189  
        if (arg != null && eq(arg.getClass().getName(), "main$Lisp") && isTrue(call(arg, "isEmpty")))
190  
          arg = get(arg, "head");
191  
          
192  
        structure_1(arg, d);
193  
      }
194  
    out.append(")"); ret;
195  
  }
196  
  
197  
  bool concept = d.conceptClass != null && d.conceptClass.isInstance(o);
198  
  S dynName = shortDynamicClassName(o);
199  
  if (concept && !d.concepts.contains(dynName)) {
200  
    d.concepts.add(dynName);
201  
    out.append("c ");
202  
  }
203  
  
204  
  // serialize an object with fields.
205  
  // first, collect all fields and values in fv.
206  
  
207  
  String shortName = dropPrefix("main$", name);
208  
  
209  
  new TreeMap<S, O> fv;
210  
  while (c != Object.class) {
211  
    for (Field field : getDeclaredFields_cached(c)) {
212  
      if ((field.getModifiers() & (Modifier.STATIC | Modifier.TRANSIENT)) != 0)
213  
        continue;
214  
      S fieldName = field.getName();
215  
      
216  
      Object value;
217  
      try {
218  
        value = field.get(o);
219  
      } catch (Exception e) {
220  
        value = "?";
221  
      }
222  
      
223  
      // omit field "className" if equal to class's name
224  
      if (concept && eq(fieldName, "className")
225  
        && eq(value, shortName)) value = null;
226  
      
227  
      // put special cases here...
228  
      
229  
      if (value != null)
230  
        fv.put(fieldName, value);
231  
    }
232  
    c = c.getSuperclass();
233  
  }
234  
  
235  
  // Now we have fields & values. Process fieldValues if it's a DynamicObject.
236  
  
237  
  if (o instanceof DynamicObject) {
238  
    fv.putAll((Map) fv.get("fieldValues"));
239  
    fv.remove("fieldValues");
240  
    shortName = dynName;
241  
    fv.remove("className");
242  
  }
243  
  
244  
  S singleField = fv.size() == 1 ? first(fv.keySet()) : null;
245  
  
246  
  // Render this$1 first because unstructure needs it for constructor call.
247  
  
248  
  out.append(shortName).append("(");
249  
  int l = out.length();
250  
  
251  
  if (fv.containsKey("this$1")) {
252  
    out.append("this$1=");
253  
    structure_1(fv.get("this$1"), d);
254  
    fv.remove("this$1");
255  
  }
256  
  
257  
  // Render the other fields.
258  
259  
  for (S fieldName : fv.keySet()) {
260  
    if (out.length() != l) out.append(", ");
261  
    out.append(fieldName).append("=");
262  
    structure_1(fv.get(fieldName), d);
263  
  }
264  
265  
  /*if (structure_allowShortening && singleField != null)
266  
    b = b.replaceAll("^" + singleField + "=", ""); // drop field name if only one*/
267  
    
268  
  if (out.length() == l) out.setLength(l-1); // drop "("
269  
  else out.append(")");
270  
}
271  
272  
// drop unused markers
273  
static S structure_2(final S s, final BitSet refd) {
274  
  final new StringBuilder out;
275  
  
276  
  javaTok_streaming(s, new JavaTok_Stream {
277  
    int n = -1;
278  
    bool drop;
279  
    
280  
    void add(int i, int j) {
281  
      ++n;
282  
      if ((n & 1) != 0 && structure_isMarker(s, i, j)) {
283  
        S t = s.substring(i+1, j);
284  
        if (!refd.get(parseInt(t))) {
285  
          drop = true;
286  
          ret;
287  
        }
288  
      }
289  
      if (drop)
290  
        drop = false;
291  
      else
292  
        out.append(s, i, j);
293  
    }
294  
  });
295  
  
296  
  ret str(out);
297  
}

Author comment

Began life as a copy of #1005374

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: #1005593
Snippet name: structure function (v8, outdated)
Eternal ID of this version: #1005593/1
Text MD5: 3385310d301151fe65fcd78656bbac32
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-28 23:38:36
Source code size: 8120 bytes / 297 lines
Pitched / IR pitched: No / No
Views / Downloads: 579 / 540
Referenced in: [show references]