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

36
LINES

< > BotCompany Repo | #1002179 // objectToMap

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (3857L) is out of date.

// 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;
  if (o == null) null;
  
  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)
    putAll(map, 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  old transpilations   

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

No comments. add comment

Snippet ID: #1002179
Snippet name: objectToMap
Eternal ID of this version: #1002179/8
Text MD5: 8e1d4ebfe3cedc0f171b7965f8f799f9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2023-02-12 14:04:07
Source code size: 1011 bytes / 36 lines
Pitched / IR pitched: No / No
Views / Downloads: 884 / 1241
Version history: 7 change(s)
Referenced in: [show references]