scope structure. static bool structure_showTiming, structure_checkTokenCount; static S structure(O o) { ret structure(o, new structure_Data); } static S structure(O o, structure_Data d) { new StringWriter sw; d.out = new PrintWriter(sw); structure_go(o, d); S s = str(sw); if (structure_checkTokenCount) { print("token count=" + d.n); assertEquals("token count", l(javaTokC(s)), d.n); } ret s; } static void structure_go(O o, structure_Data d) { structure_1(o, d); while (nempty(d.stack)) popLast(d.stack).run(); } static void structureToPrintWriter(O o, PrintWriter out) { new structure_Data d; d.out = out; structure_go(o, d); } // leave to false, unless unstructure() breaks static boolean structure_allowShortening = false; sinterface #Serializer { void serialize(O o); } // info on how to serialize objects of a certain class sclass structure_ClassInfo { Serializer serializer; L fields; Method customSerializer; bool special, nullInstances; } sclass #Data { PrintWriter out; int stringSizeLimit; int shareStringsLongerThan = 20; bool noStringSharing; new IdentityHashMap seen; //new BitSet refd; new HashMap strings; new HashSet concepts; HashMap infoByClass = new HashMap; new HashMap persistenceInfo; int n; // token count new L stack; bool unrestructurable; // true if we serialized raw Java objects // append single token structure_Data append(S token) { out.print(token); ++n; ret this; } structure_Data append(int i) { out.print(i); ++n; ret this; } // append multiple tokens structure_Data append(S token, int tokCount) { out.print(token); n += tokCount; ret this; } // extend last token structure_Data app(S token) { out.print(token); ret this; } structure_Data app(int i) { out.print(i); ret this; } } static void structure_1(final Object o, final structure_Data d) ctex { if (o == null) { d.append("null"); ret; } Class c = o.getClass(); bool concept = false; ifclass Concept concept = o instanceof Concept; endif structure_ClassInfo info = d.infoByClass.get(c); if (info == null) { d.infoByClass.put(c, info = new structure_ClassInfo); if ((info.customSerializer = findMethodNamed(c, '_serialize)) != null) info.special = true; info.serializer = makeSerializer(c, info, d); } info.serializer.serialize(o); Field persistenceInfoField = cast d.persistenceInfo.get(c); Map persistenceInfo = persistenceInfoField == null ? null : (Map) persistenceInfoField.get(o); ifdef structure_debug print("persistenceInfo for " + c + ": " + persistenceInfo); endifdef new LinkedHashMap fv; for (Field f : lFields) { Object value; try { value = f.get(o); } catch (Exception e) { value = "?"; } if (value != null && (persistenceInfo == null || !Boolean.FALSE.equals(persistenceInfo.get(f.getName())))) fv.put(f.getName(), value); ifdef structure_debug else print("Skipping null field " + f.getName() + " for " + identityHashCode(o)); endifdef } S name = c.getName(); String shortName = dropPrefix("main$", name); // TODO: drop loadableUtils also if (startsWithDigit(shortName)) shortName = name; // for anonymous classes // Now we have fields & values. Process fieldValues if it's a DynamicObject. // omit field "className" if equal to class's name if (concept && eq(fv.get("className"), shortName)) fv.remove("className"); if (o instanceof DynamicObject) { putAll(fv, (Map) fv.get("fieldValues")); fv.remove("fieldValues"); shortName = shortDynamicClassName(o); fv.remove("className"); } S singleField = fv.size() == 1 ? first(fv.keySet()) : null; if (concept && !d.concepts.contains(shortName)) { d.concepts.add(shortName); d.append("c "); } d.append(shortName); d.n += countDots(shortName)*2; // correct token count ifdef structure_debug print("Fields for " + shortName + ": " + fv.keySet()); endifdef final int l = d.n; final Iterator it = fv.entrySet().iterator(); d.stack.add(r { if (!it.hasNext()) { if (d.n != l) d.append(")"); } else { Map.Entry e = (Map.Entry) it.next(); d.append(d.n == l ? "(" : ", "); d.append((S) e.getKey()).append("="); d.stack.add(this); structure_1(e.getValue(), d); } }); } Serializer #makeSerializer(Class c, ClassInfo info, Data d) { // these are never back-referenced (for readability) if (isSubclassOf(c, Number.class)) { PrintWriter out = d.out; if (c == Integer) ret o -> { int i = ((Int) o).intValue(); out.print(i); d.n += i < 0 ? 2 : 1; ret; } if (c == Long) ret o -> { long l = ((Long) o).longValue(); out.print(l); out.print("L"); d.n += l < 0 ? 2 : 1; ret; } if (c == Short) ret o -> { short s = o.shortValue(); d.append("sh "); out.print(s); d.n += s < 0 ? 2 : 1; ret; } if (c == Float) ret o -> { d.append("fl ", 2); quoteToPrintWriter(str(o), out); ret; } if (c == Double) ret o -> { d.append("d(", 3); quoteToPrintWriter(str(o), out); d.append(")"); ret; } if (c == BigInteger) ret o -> { out.print("bigint("); out.print(o); out.print(")"); d.n += ((BigInteger) o).signum() < 0 ? 5 : 4; ret; } } if (c == Boolean.class) ret o -> { d.append(((Boolean) o).booleanValue() ? "t" : "f"); ret; }; if (c == Character.class) ret o -> { d.append(quoteCharacter((Character) o)); ret; }; if (c == File.class) ret o -> { d.append("File ").append(quote(((File) o).getPath())); ret; }; // referencable objects follow Serializer serializer2 = makeSerializer2(c, info, d); ret o -> { Int ref = d.seen.get(o); if (o instanceof S && ref == null) ref = d.strings.get((S) o); if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); ret; } if (!o instanceof S) d.seen.put(o, d.n); // record token number else { S s = d.stringSizeLimit != 0 ? shorten((S) o, d.stringSizeLimit) : (S) o; if (!d.noStringSharing) { if (d.shareStringsLongerThan == Int.MAX_VALUE) d.seen.put(o, d.n); if (l(s) >= d.shareStringsLongerThan) d.strings.put(s, d.n); } quoteToPrintWriter(s, d.out); d.n++; ret; } serializer2.serialize(o); }; } // make serializer for back-referenceable objects Serializer #makeSerializer2(Class c, ClassInfo info, Data d) { if (isSubclassOf(c, Set.class)) { /*O set2 = unwrapSynchronizedSet(o); if (set2 != o) { d.append("sync"); o = set2; } TODO */ if (isSubclassOf(c, TreeSet.class)) ret o -> { d.append(isCISet_gen(o) ? "ciset" : "treeset"); structure_1(new ArrayList(o), d); ret; }; ret o -> { // assume it's a HashSet or LinkedHashSet d.append(o instanceof LinkedHashSet ? "lhs" : "hashset"); structure_1(new ArrayList(o), d); ret; }; } S name = c.getName(); if (isSubclassOf(c, Collection.class) && !isJavaXClassName(name) /* && neq(name, "main$Concept$RefL") */) { // it's a list if (name.equals("java.util.Collections$SynchronizedList") || name.equals("java.util.Collections$SynchronizedRandomAccessList")) ret o -> { d.append("sync "); ret with structure_1(unwrapSynchronizedList(o/List), d); }; bool isLL = name.equals("java.util.LinkedList"); ret o -> { if (isLL) d.append("ll"); d.append("["); final int l = d.n; final Iterator it = ((Collection) o).iterator(); d.stack.add(r { if (!it.hasNext()) d.append("]"); else { d.stack.add(this); if (d.n != l) d.append(", "); structure_1(it.next(), d); } }); ret; }; } if (isSuperclass Map(c) && !startsWith(name, "main$")) { IF0 prefix; if (isSuperclass LinkedHashMap(c)) prefix = () -> "lhm"; else if (isSuperclass HashMap(c)) prefix = () -> "hm"; else if (isSuperclass TreeMap(c)) prefix = () -> isCIMap_gen(o) ? "cimap" : "tm"; else if (name.equals("java.util.Collections$SynchronizedMap") || name.equals("java.util.Collections$SynchronizedSortedMap") || name.equals("java.util.Collections$SynchronizedNavigableMap")) { d.append("sync "); ret with structure_1(unwrapSynchronizedMap(o/Map), d); } else prefix = null; ret o -> { if (prefix != null) d.append(prefix!); d.append("{"); final int l = d.n; final Iterator it = ((Map) o).entrySet().iterator(); d.stack.add(new Runnable { bool v; Map.Entry e; run { if (v) { d.append("="); v = false; d.stack.add(this); structure_1(e.getValue(), d); } else { if (!it.hasNext()) d.append("}"); else { e = (Map.Entry) it.next(); v = true; d.stack.add(this); if (d.n != l) d.append(", "); structure_1(e.getKey(), d); } } } }); ret; }; } if (c.isArray()) { if (o instanceof byte[]) ret o -> { d.append("ba ").append(quote(bytesToHex((byte[]) o))); ret; }; if (o instanceof bool[]) ret o -> { int n = Array.getLength(o); S hex = boolArrayToHex((bool[]) o); int i = l(hex); while (i > 0 && hex.charAt(i-1) == '0' && hex.charAt(i-2) == '0') i -= 2; d.append("boolarray ").append(n).app(" ").append(quote(substring(hex, 0, i))); ret; } S atype/*, sep = ", "*/; // sep is not used yet if (o instanceof int[]) { //ret "intarray " + quote(intArrayToHex((int[]) o)); atype = "intarray"; //sep = " "; } else if (o instanceof double[]) { atype = "dblarray"; //sep = " "; } else atype = "array"; ret o -> { int n = Array.getLength(o); d.append(atype).append("{"); d.stack.add(new Runnable { int i; run { if (i >= n) d.append("}"); else { d.stack.add(this); if (i > 0) d.append(", "); structure_1(Array.get(o, i++), d); } } }); ret; }; } if (c == Class.class) ret o -> { d.append("class(", 2).append(quote(((Class) o).getName())).append(")"); ret; }; if (isSubclassOf(c, Throwable.class)) ret o -> { d.append("exception(", 2).append(quote(((Throwable) o).getMessage())).append(")"); ret; }; if (c == BitSet.class) ret o -> { BitSet bs = (BitSet) o; d.append("bitset{", 2); int l = d.n; for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) { if (d.n != l) d.append(", "); d.append(i); } d.append("}"); ret; } // Need more cases? This should cover all library classes... if (name.startsWith("java.") || name.startsWith("javax.")) ret o -> { d.unrestructurable = true; // Note: as not unstructure-able d.append("j ").append(quote(str(o))); ret; }; ifclass Lisp if (isSuperclass Lisp(o)) ret o -> { d.append("l(", 2); Lisp lisp = cast o; structure_1(lisp.head, d); Iterator it = lisp.args == null ? emptyIterator() : lisp.args.iterator(); d.stack.add(r { if (!it.hasNext()) d.append(")"); else { d.stack.add(this); d.append(", "); structure_1(it.next(), d); } }); ret; }; endif /*if (name.equals("main$Lisp")) { fail("lisp not supported right now"); }*/ if (info.special) { if (info.customSerializer != null) ret o -> { // custom serialization (_serialize method) O o2 = invokeMethod(info.customSerializer, o); d.append("cu "); S shortName = dropPrefix("main$", name); d.append(shortName); d.out.append(' '); structure_1(o2, d); ret; }; else if (info.nullInstances) ret o -> { d.append("null"); }; else fail("unknown special type"); } // serialize an object with fields. // first, collect all fields and values in fv. TreeSet fields = new TreeSet(new Comparator() { public int compare(Field a, Field b) { ret stdcompare(a.getName(), b.getName()); } }); Class cc = c; while (cc != Object.class) { for (Field field : getDeclaredFields_cached(cc)) { S fieldName = field.getName(); if (fieldName.equals("_persistenceInfo")) d.persistenceInfo.put(c, field); if ((field.getModifiers() & (java.lang.reflect.Modifier.STATIC | java.lang.reflect.Modifier.TRANSIENT)) != 0) continue; fields.add(field); // put special cases here...? } cc = cc.getSuperclass(); } // TODO: S fieldOrder = getOpt(c, "_fieldOrder"); lFields = asList(fields); // Render this$1 first because unstructure needs it for constructor call. int n = l(lFields); for (int i = 0; i < n; i++) { Field f = lFields.get(i); if (f.getName().equals("this$1")) { lFields.remove(i); lFields.add(0, f); break; } } ifdef structure_debug print("Saving fields for " + c + ": " + lFields); endifdef info.fields = lFields; } // << if (lFields == null) else { // ref handling for lFields != null Int ref = d.seen.get(o); if (ref != null) { /*d.refd.set(ref);*/ d.append("t").app(ref); ret; } d.seen.put(o, d.n); // record token number } } end scope