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

35
LINES

< > BotCompany Repo | #1002179 - objectToMap

JavaX fragment (include)

// o is either a map already (string->object) or an arbitrary object,
// in which case its fields are converted into a map.
static Map<S, O> objectToMap(O o) ctex {
  if (o instanceof Map) ret (Map) o;
  
  new TreeMap<S, O> map;
  Class c = o.getClass();
  while (c != Object.class) {
    Field[] fields = c.getDeclaredFields();
    for (final Field field : fields) {
      if ((field.getModifiers() & Modifier.STATIC) != 0)
        continue;
      field.setAccessible(true);
      final Object value = field.get(o);
      if (value != null)
        map.put(field.getName(), value);
    }
    c = c.getSuperclass();
  }
  
  // XXX NEW - hopefully this doesn't break anything
  if (o instanceof DynamicObject)
    map.putAll(o/DynamicObject.fieldValues);

  ret map;
}

// same for a collection (convert each element)
static L<Map<S, O>> objectToMap(Iterable l) {
  if (l == null) null;
  new L x;
  for (O o : l)
    x.add(objectToMap(o));
  ret x;
}

download  show line numbers  debug dex   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, whxojlpjdney, xrpafgyirdlv

No comments. add comment

Snippet ID: #1002179
Snippet name: objectToMap
Eternal ID of this version: #1002179/6
Text MD5: c874009bb7b2e27a3e047f2253e76b14
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-09-25 20:52:41
Source code size: 986 bytes / 35 lines
Pitched / IR pitched: No / No
Views / Downloads: 561 / 735
Version history: 5 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1002179 & http://1002179.tinybrain.de