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

79
LINES

< > BotCompany Repo | #1005598 // setOpt, setOpt_findField, optimized for objects (but not for classes yet, LIVE)

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

Libraryless. Click here for Pure Java version (10153L/56K).

1  
please include function getOpt_cached.
2  
3  
// Some people use this.
4  
static Field setOpt_findField(Class c, S field) {
5  
  HashMap<S, Field> map;
6  
  synchronized(getOpt_cache) {
7  
    map = getOpt_cache.get(c);
8  
    if (map == null)
9  
      map = getOpt_makeCache(c);
10  
  }
11  
  ret map.get(field);
12  
}
13  
14  
static void setOpt(Object o, String field, Object value) ctex {
15  
  if (o == null) ret;
16  
  
17  
  ifclass _SetField
18  
    if (o instanceof _SetField) ret with o/_SetField._setField(field, value);
19  
  endif
20  
  
21  
  Class c = o.getClass();
22  
  HashMap<S, Field> map;
23  
  
24  
  if (getOpt_cache == null)
25  
    map = getOpt_makeCache(c); // in class init
26  
  else synchronized(getOpt_cache) {
27  
    map = getOpt_cache.get(c);
28  
    if (map == null)
29  
      map = getOpt_makeCache(c);
30  
  }
31  
  
32  
  if (map == getOpt_special) {
33  
    if (o instanceof Class) {
34  
      setOpt((Class) o, field, value);
35  
      ret;
36  
    }
37  
    
38  
    // It's probably a subclass of Map. Use raw method. TODO: huh?
39  
    setOpt_raw(o, field, value);
40  
    ret;
41  
  }
42  
  
43  
  Field f = map.get(field);
44  
  ifdef setOpt_debug
45  
  printVars_str(+c, +field, +f);
46  
  endifdef
47  
  if (f != null)
48  
    ret with smartSet(f, o, value); // possible improvement: skip setAccessible
49  
  ifclass DynamicObject
50  
    if (o cast DynamicObject)
51  
      ret with setDyn(o, field, value);
52  
  endif
53  
  if (o cast IMeta)
54  
    setDyn(o, field, value);
55  
}
56  
57  
static void setOpt(Class c, String field, Object value) {
58  
  if (c == null) ret;
59  
  try {
60  
    Field f = setOpt_findStaticField(c, field); // TODO: optimize
61  
    if (f != null)
62  
      smartSet(f, null, value);
63  
  } catch (Exception e) {
64  
    throw new RuntimeException(e);
65  
  }
66  
}
67  
  
68  
static Field setOpt_findStaticField(Class<?> c, String field) {
69  
  Class _c = c;
70  
  do {
71  
    for (Field f : _c.getDeclaredFields())
72  
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
73  
        makeAccessible(f);
74  
        return f;
75  
      }
76  
    _c = _c.getSuperclass();
77  
  } while (_c != null);
78  
  ret null;
79  
}

Author comment

Began life as a copy of #1000891

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1005598
Snippet name: setOpt, setOpt_findField, optimized for objects (but not for classes yet, LIVE)
Eternal ID of this version: #1005598/16
Text MD5: d5230814e7f51aebde0b808897a62ba7
Transpilation MD5: 551df47a5fbf609aefcb7f9b34743a26
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-03 17:52:04
Source code size: 2027 bytes / 79 lines
Pitched / IR pitched: No / No
Views / Downloads: 874 / 1355
Version history: 15 change(s)
Referenced in: [show references]