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.

1  
// o is either a map already (string->object) or an arbitrary object,
2  
// in which case its fields are converted into a map.
3  
static Map<S, O> objectToMap(O o) ctex {
4  
  if (o instanceof Map) ret (Map) o;
5  
  if (o == null) null;
6  
  
7  
  new TreeMap<S, O> map;
8  
  Class c = o.getClass();
9  
  while (c != Object.class) {
10  
    Field[] fields = c.getDeclaredFields();
11  
    for (final Field field : fields) {
12  
      if ((field.getModifiers() & Modifier.STATIC) != 0)
13  
        continue;
14  
      field.setAccessible(true);
15  
      final Object value = field.get(o);
16  
      if (value != null)
17  
        map.put(field.getName(), value);
18  
    }
19  
    c = c.getSuperclass();
20  
  }
21  
  
22  
  // XXX NEW - hopefully this doesn't break anything
23  
  if (o instanceof DynamicObject)
24  
    putAll(map, o/DynamicObject.fieldValues);
25  
26  
  ret map;
27  
}
28  
29  
// same for a collection (convert each element)
30  
static L<Map<S, O>> objectToMap(Iterable l) {
31  
  if (l == null) null;
32  
  new L x;
33  
  for (O o : l)
34  
    x.add(objectToMap(o));
35  
  ret x;
36  
}

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: 885 / 1244
Version history: 7 change(s)
Referenced in: [show references]