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

44
LINES

< > BotCompany Repo | #1000891 // "setOpt" function (set field by reflection, don't complain if not there)

JavaX fragment (include)

1  
static void setOpt(Object o, String field, Object value) {
2  
  if (o == null) ret;
3  
  if (o instanceof Class) setOpt((Class) o, field, value);
4  
  else try {
5  
    Field f = setOpt_findField(o.getClass(), field);
6  
    if (f != null)
7  
      smartSet(f, o, value);
8  
  } catch (Exception e) {
9  
    throw new RuntimeException(e);
10  
  }
11  
}
12  
13  
static void setOpt(Class c, String field, Object value) {
14  
  if (c == null) ret;
15  
  try {
16  
    Field f = setOpt_findStaticField(c, field);
17  
    if (f != null)
18  
      smartSet(f, null, value);
19  
  } catch (Exception e) {
20  
    throw new RuntimeException(e);
21  
  }
22  
}
23  
  
24  
static Field setOpt_findStaticField(Class<?> c, String field) {
25  
  Class _c = c;
26  
  do {
27  
    for (Field f : _c.getDeclaredFields())
28  
      if (f.getName().equals(field) && (f.getModifiers() & Modifier.STATIC) != 0)
29  
        return f;
30  
    _c = _c.getSuperclass();
31  
  } while (_c != null);
32  
  ret null;
33  
}
34  
35  
static Field setOpt_findField(Class<?> c, String field) {
36  
  Class _c = c;
37  
  do {
38  
    for (Field f : _c.getDeclaredFields())
39  
      if (f.getName().equals(field))
40  
        return f;
41  
    _c = _c.getSuperclass();
42  
  } while (_c != null);
43  
  ret null;
44  
}

Author comment

Began life as a copy of #1000415

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000891
Snippet name: "setOpt" function (set field by reflection, don't complain if not there)
Eternal ID of this version: #1000891/1
Text MD5: c1ee0bd846571ad20dd9063d668f71c5
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-19 16:32:40
Source code size: 1165 bytes / 44 lines
Pitched / IR pitched: No / No
Views / Downloads: 680 / 6007
Referenced in: [show references]