1 | static O getOpt(O o, S field) {
|
2 | ret getOpt_cached(o, field); |
3 | } |
4 | |
5 | static O mapMethodLike getOpt(S field, O o) {
|
6 | ret getOpt_cached(o, field); |
7 | } |
8 | |
9 | static Object getOpt_raw(Object o, String field) ctex {
|
10 | Field f = getOpt_findField(o.getClass(), field); |
11 | if (f == null) null; |
12 | makeAccessible(f); |
13 | ret f.get(o); |
14 | } |
15 | |
16 | // access of static fields is not yet optimized |
17 | static O getOpt(Class c, String field) ctex {
|
18 | if (c == null) null; |
19 | Field f = getOpt_findStaticField(c, field); |
20 | if (f == null) null; |
21 | makeAccessible(f); |
22 | ret f.get(null); |
23 | } |
24 | |
25 | static Field getOpt_findStaticField(Class<?> c, String field) {
|
26 | Class _c = c; |
27 | do {
|
28 | for (Field f : _c.getDeclaredFields()) |
29 | if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) |
30 | return f; |
31 | _c = _c.getSuperclass(); |
32 | } while (_c != null); |
33 | ret null; |
34 | } |
Began life as a copy of #1000597
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
| ID | Author/Program | Comment | Date |
|---|---|---|---|
| 1208 | stefan | getOpt can be passed a null 1st argument - then it just returns null. Useful for cascaded optionality, e.g.:
static S printableUserName() { // Custom or Slack name, depending on what's available ret or((S) callOpt(getBot("#1002355"), "getUserName"), getUserName()); } | 2016-01-27 01:28:45 |
| Snippet ID: | #1001101 |
| Snippet name: | "getOpt" function (get field by reflection, return null if not there) |
| Eternal ID of this version: | #1001101/9 |
| Text MD5: | 91e56345ffd108ce4905ab8367478f85 |
| Author: | stefan |
| Category: | javax |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2019-08-31 14:55:44 |
| Source code size: | 894 bytes / 34 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 2583 / 8522 |
| Version history: | 8 change(s) |
| Referenced in: | [show references] |