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

36
LINES

< > BotCompany Repo | #1000524 // structureSize function - don't use

JavaX fragment (include)

1  
  public static int structureSize(Object o, Class baseClass) {
2  
    return structureSize(o, baseClass, new IdentityHashMap());
3  
  }
4  
  
5  
  static int structureSize(Object o, Class baseClass,
6  
    IdentityHashMap seen) {
7  
    if (o == null || seen.containsKey(o)) return 0;
8  
    seen.put(o, Boolean.TRUE);
9  
    int size = 1;
10  
    Class c = o.getClass();
11  
    if (c.isArray()) {
12  
      int n = Array.getLength(o);
13  
      for (int i = 0; i < n; i++)
14  
        size += structureSize(Array.get(o, i), baseClass, seen);
15  
    } else
16  
      while (c != Object.class && c != baseClass) {
17  
        Field[] fields = c.getDeclaredFields();
18  
        for (Field field : fields) {
19  
          if ((field.getModifiers() & Modifier.STATIC) != 0)
20  
            continue;
21  
          if (field.getType().isPrimitive())
22  
            ++size;
23  
          else {
24  
            Object value = null;
25  
            try {
26  
              value = field.get(o);
27  
            } catch (IllegalAccessException e) {
28  
              throw new RuntimeException(e);
29  
            }
30  
            size += structureSize(value, baseClass, seen);
31  
          }
32  
        }
33  
        c = c.getSuperclass();
34  
      }
35  
    return size;
36  
  }

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: #1000524
Snippet name: structureSize function - don't use
Eternal ID of this version: #1000524/1
Text MD5: ef0c90259cb83668d57ad75852630f06
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-20 22:12:41
Source code size: 1183 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 600 / 960
Referenced in: [show references]