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

83
LINES

< > BotCompany Repo | #1000643 // structure function (generic)

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  
  new StringBuilder buf;
13  
  
14  
  if (o instanceof Collection) {
15  
    for (Object x : (Collection) o) {
16  
      if (buf.length() != 0) buf.append(", ");
17  
      buf.append(structure(x, stringSizeLimit));
18  
    }
19  
    return "[" + buf + "]";
20  
  }
21  
  
22  
  if (o instanceof Map) {
23  
    for (Object e : ((Map) o).entrySet()) {
24  
      if (buf.length() != 0) buf.append(", ");
25  
      buf.append(structure(((Map.Entry) e).getKey(), stringSizeLimit));
26  
      buf.append("=");
27  
      buf.append(structure(((Map.Entry) e).getValue(), stringSizeLimit));
28  
    }
29  
    return "{" + buf + "}";
30  
  }
31  
  
32  
  if (o.getClass().isArray()) {
33  
    int n = Array.getLength(o);
34  
    for (int i = 0; i < n; i++) {
35  
      if (buf.length() != 0) buf.append(", ");
36  
      buf.append(structure(Array.get(o, i), stringSizeLimit));
37  
    }
38  
    return "{" + buf + "}";
39  
  }
40  
41  
  if (o instanceof String)
42  
    return quote(stringSizeLimit != 0 ? shorten((String) o, stringSizeLimit) : (String) o);
43  
  
44  
  // Need more cases? This should cover all library classes...
45  
  if (name.startsWith("java.") || name.startsWith("javax."))
46  
    return String.valueOf(o);
47  
    
48  
  String shortName = o.getClass().getName().replaceAll("^main\\$", "");
49  
50  
  // TODO: go to superclasses too
51  
  Field[] fields = o.getClass().getDeclaredFields();
52  
  int numFields = 0;
53  
  String fieldName = "";
54  
  for (Field field : fields) {
55  
    if ((field.getModifiers() & Modifier.STATIC) != 0)
56  
      continue;
57  
    Object value;
58  
    try {
59  
      field.setAccessible(true);
60  
      value = field.get(o);
61  
    } catch (Exception e) {
62  
      value = "?";
63  
    }
64  
    
65  
    fieldName = field.getName();
66  
    
67  
    // put special cases here...
68  
69  
    if (value != null) {
70  
      if (buf.length() != 0) buf.append(", ");
71  
      buf.append(fieldName + "=" + structure(value, stringSizeLimit));
72  
    }
73  
    ++numFields;
74  
  }
75  
  String b = buf.toString();
76  
  
77  
  if (numFields == 1 && structure_allowShortening)
78  
    b = b.replaceAll("^" + fieldName + "=", ""); // drop field name if only one
79  
  String s = shortName;
80  
  if (buf.length() != 0)
81  
    s += "(" + b + ")";
82  
  return s;
83  
}

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: #1000643
Snippet name: structure function (generic)
Eternal ID of this version: #1000643/1
Text MD5: 50e5f3fede59d1fe3bc267f85099f6c9
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-07 01:50:00
Source code size: 2416 bytes / 83 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 615 / 1448
Referenced in: [show references]