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


< > 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).

please include function getOpt_cached.

// Some people use this.
static Field setOpt_findField(Class c, S field) {
  HashMap<S, Field> map;
  synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  ret map.get(field);

static void setOpt(Object o, String field, Object value) ctex {
  if (o == null) ret;
  ifclass _SetField
    if (o instanceof _SetField) ret with o/_SetField._setField(field, value);
  Class c = o.getClass();
  HashMap<S, Field> map;
  if (getOpt_cache == null)
    map = getOpt_makeCache(c); // in class init
  else synchronized(getOpt_cache) {
    map = getOpt_cache.get(c);
    if (map == null)
      map = getOpt_makeCache(c);
  if (map == getOpt_special) {
    if (o instanceof Class) {
      setOpt((Class) o, field, value);
    // It's probably a subclass of Map. Use raw method. TODO: huh?
    setOpt_raw(o, field, value);
  Field f = map.get(field);
  ifdef setOpt_debug
  printVars_str(+c, +field, +f);
  if (f != null)
    ret with smartSet(f, o, value); // possible improvement: skip setAccessible
  ifclass DynamicObject
    if (o cast DynamicObject)
      ret with setDyn(o, field, value);
  if (o cast IMeta)
    setDyn(o, field, value);

static void setOpt(Class c, String field, Object value) {
  if (c == null) ret;
  try {
    Field f = setOpt_findStaticField(c, field); // TODO: optimize
    if (f != null)
      smartSet(f, null, value);
  } catch (Exception e) {
    throw new RuntimeException(e);
static Field setOpt_findStaticField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0) {
        return f;
    _c = _c.getSuperclass();
  } while (_c != null);
  ret null;

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