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

42
LINES

< > BotCompany Repo | #1009441 - setOpt_raw function (set field by reflection, ignore if it's a Map)

JavaX fragment (include)

static void setOpt_raw(Object o, String field, Object value) ctex {
  if (o == null) ret;
  if (o instanceof Class) setOpt_raw((Class) o, field, value);
  else {
    Field f = setOpt_raw_findField(o.getClass(), field);
    if (f != null) {
      makeAccessible(f);
      smartSet(f, o, value);
    }
  }
}

static void setOpt_raw(Class c, String field, Object value) ctex {
  if (c == null) ret;
  Field f = setOpt_raw_findStaticField(c, field);
  if (f != null) {
    makeAccessible(f);
    smartSet(f, null, value);
  }
}
  
static Field setOpt_raw_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;
}

static Field setOpt_raw_findField(Class<?> c, String field) {
  Class _c = c;
  do {
    for (Field f : _c.getDeclaredFields())
      if (f.getName().equals(field))
        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   

Travelled to 10 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz, whxojlpjdney

No comments. add comment

Snippet ID: #1009441
Snippet name: setOpt_raw function (set field by reflection, ignore if it's a Map)
Eternal ID of this version: #1009441/6
Text MD5: 17d6f52547373b9985b860700fc0bd66
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:03:12
Source code size: 1137 bytes / 42 lines
Pitched / IR pitched: No / No
Views / Downloads: 240 / 337
Version history: 5 change(s)
Referenced in: [show references]