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

77
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 (2290L/14K).

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
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  
      setDyn(o, field, value);
52  
  endif
53  
}
54  
55  
static void setOpt(Class c, String field, Object value) {
56  
  if (c == null) ret;
57  
  try {
58  
    Field f = setOpt_findStaticField(c, field); // TODO: optimize
59  
    if (f != null)
60  
      smartSet(f, null, value);
61  
  } catch (Exception e) {
62  
    throw new RuntimeException(e);
63  
  }
64  
}
65  
  
66  
static Field setOpt_findStaticField(Class<?> c, String field) {
67  
  Class _c = c;
68  
  do {
69  
    for (Field f : _c.getDeclaredFields())
70  
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
71  
        makeAccessible(f);
72  
        return f;
73  
      }
74  
    _c = _c.getSuperclass();
75  
  } while (_c != null);
76  
  ret null;
77  
}

Author comment

Began life as a copy of #1000891

download  show line numbers  debug dex   

Travelled to 14 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jozkyjcghlvl, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, tslmcundralx, tvejysmllsmz, 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/14
Text MD5: 5d4526902201c35d770b0e4ab03a2cac
Transpilation MD5: 43da4b2634ee53d8c9db42cd1ff07d8d
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-24 01:40:40
Source code size: 1955 bytes / 77 lines
Pitched / IR pitched: No / No
Views / Downloads: 422 / 746
Version history: 13 change(s)
Referenced in: [show references]