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)

  public static int structureSize(Object o, Class baseClass) {
    return structureSize(o, baseClass, new IdentityHashMap());
  }
  
  static int structureSize(Object o, Class baseClass,
    IdentityHashMap seen) {
    if (o == null || seen.containsKey(o)) return 0;
    seen.put(o, Boolean.TRUE);
    int size = 1;
    Class c = o.getClass();
    if (c.isArray()) {
      int n = Array.getLength(o);
      for (int i = 0; i < n; i++)
        size += structureSize(Array.get(o, i), baseClass, seen);
    } else
      while (c != Object.class && c != baseClass) {
        Field[] fields = c.getDeclaredFields();
        for (Field field : fields) {
          if ((field.getModifiers() & Modifier.STATIC) != 0)
            continue;
          if (field.getType().isPrimitive())
            ++size;
          else {
            Object value = null;
            try {
              value = field.get(o);
            } catch (IllegalAccessException e) {
              throw new RuntimeException(e);
            }
            size += structureSize(value, baseClass, seen);
          }
        }
        c = c.getSuperclass();
      }
    return size;
  }

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: 597 / 956
Referenced in: [show references]