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

139
LINES

< > BotCompany Repo | #1001386 // structure function (v2, without seen)

JavaX fragment (include)

1  
static String structure(Object o) {
2  
  return structure(o, 0);
3  
}
4  
5  
// leave to false, unless unstructure() breaks
6  
static boolean structure_allowShortening = false;
7  
  
8  
static String structure(Object o, int stringSizeLimit) {
9  
  if (o == null) return "null";
10  
  String name = o.getClass().getName();
11  
  
12  
  StringBuilder buf = new StringBuilder();
13  
  
14  
  if (o instanceof HashSet)
15  
    return "hashset" + structure(new ArrayList((Set) o), stringSizeLimit);
16  
17  
  if (o instanceof TreeSet)
18  
    return "treeset" + structure(new ArrayList((Set) o), stringSizeLimit);
19  
  
20  
  if (o instanceof Collection) {
21  
    for (Object x : (Collection) o) {
22  
      if (buf.length() != 0) buf.append(", ");
23  
      buf.append(structure(x, stringSizeLimit));
24  
    }
25  
    return "[" + buf + "]";
26  
  }
27  
  
28  
  if (o instanceof Map) {
29  
    for (Object e : ((Map) o).entrySet()) {
30  
      if (buf.length() != 0) buf.append(", ");
31  
      buf.append(structure(((Map.Entry) e).getKey(), stringSizeLimit));
32  
      buf.append("=");
33  
      buf.append(structure(((Map.Entry) e).getValue(), stringSizeLimit));
34  
    }
35  
    return (o instanceof HashMap ? "hashmap" : "") + "{" + buf + "}";
36  
  }
37  
  
38  
  if (o.getClass().isArray()) {
39  
    int n = Array.getLength(o);
40  
    for (int i = 0; i < n; i++) {
41  
      if (buf.length() != 0) buf.append(", ");
42  
      buf.append(structure(Array.get(o, i), stringSizeLimit));
43  
    }
44  
    return "array{" + buf + "}";
45  
  }
46  
47  
  if (o instanceof String)
48  
    return quote(stringSizeLimit != 0 ? shorten((String) o, stringSizeLimit) : (String) o);
49  
    
50  
  if (o instanceof Class)
51  
    return "class(" + quote(((Class) o).getName()) + ")";
52  
    
53  
  if (o instanceof Throwable)
54  
    return "exception(" + quote(((Throwable) o).getMessage()) + ")";
55  
    
56  
  if (o instanceof BigInteger)
57  
    return "bigint(" + o + ")";
58  
  
59  
  if (o instanceof Double)
60  
    return "d(" + quote(str(o)) + ")";
61  
    
62  
  if (o instanceof Long)
63  
    return o + "L";
64  
  
65  
  // Need more cases? This should cover all library classes...
66  
  if (name.startsWith("java.") || name.startsWith("javax."))
67  
    return String.valueOf(o);
68  
    
69  
  String shortName = o.getClass().getName().replaceAll("^main\\$", "");
70  
  
71  
  if (shortName.equals("Lisp")) {
72  
    buf.append("l(" + structure(getOpt(o, "head")));
73  
    L args = cast getOpt(o, "args");
74  
    if (nempty(args))
75  
      for (int i = 0; i < l(args); i++) {
76  
        buf.append(", ");
77  
        O arg = args.get(i);
78  
        
79  
        // sweet shortening
80  
        if (arg != null && eq(arg.getClass().getName(), "main$Lisp") && isTrue(call(arg, "isEmpty")))
81  
          arg = get(arg, "head");
82  
          
83  
        buf.append(structure(arg));
84  
      }
85  
    buf.append(")");
86  
    ret str(buf);
87  
  }
88  
    
89  
  int numFields = 0;
90  
  String fieldName = "";
91  
  if (shortName.equals("DynamicObject")) {
92  
    shortName = (String) get(o, "className");
93  
    Map<String, Object> fieldValues = (Map) get(o, "fieldValues");
94  
    
95  
    for (String _fieldName : fieldValues.keySet()) {
96  
      fieldName = _fieldName;
97  
      Object value = fieldValues.get(fieldName);
98  
      if (value != null) {
99  
        if (buf.length() != 0) buf.append(", ");
100  
        buf.append(fieldName + "=" + structure(value, stringSizeLimit));
101  
      }
102  
      ++numFields;
103  
   }
104  
  } else {
105  
    // regular class
106  
    // TODO: go to superclasses too
107  
    Field[] fields = o.getClass().getDeclaredFields();
108  
    for (Field field : fields) {
109  
      if ((field.getModifiers() & Modifier.STATIC) != 0)
110  
        continue;
111  
      Object value;
112  
      try {
113  
        field.setAccessible(true);
114  
        value = field.get(o);
115  
      } catch (Exception e) {
116  
        value = "?";
117  
      }
118  
      
119  
      fieldName = field.getName();
120  
      
121  
      // put special cases here...
122  
  
123  
      if (value != null) {
124  
        if (buf.length() != 0) buf.append(", ");
125  
        buf.append(fieldName + "=" + structure(value, stringSizeLimit));
126  
      }
127  
      ++numFields;
128  
    }
129  
  }
130  
  
131  
  String b = buf.toString();
132  
  
133  
  if (numFields == 1 && structure_allowShortening)
134  
    b = b.replaceAll("^" + fieldName + "=", ""); // drop field name if only one
135  
  String s = shortName;
136  
  if (buf.length() != 0)
137  
    s += "(" + b + ")";
138  
  return s;
139  
}

Author comment

Began life as a copy of #1000643

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: #1001386
Snippet name: structure function (v2, without seen)
Eternal ID of this version: #1001386/1
Text MD5: 3bdb6de7d1ed8fd28a85bc61c0d17707
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-29 01:54:58
Source code size: 4207 bytes / 139 lines
Pitched / IR pitched: No / No
Views / Downloads: 646 / 6121
Referenced in: [show references]