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

92
LINES

< > BotCompany Repo | #1000619 - get function

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

Transpiled version (1719L) is out of date.

// get purpose 1: access a list/array/map (safer version of x.get(y))

static <A> A get(L<A> l, int idx) {
  ret l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}

// seems to conflict with other signatures
/*static <A, B> B get(Map<A, B> map, A key) {
  ret map != null ? map.get(key) : null;
}*/

static <A> A get(A[] l, int idx) {
  ret idx >= 0 && idx < l(l) ? l[idx] : null;
}

// default to false
static bool get(bool[] l, int idx) {
  ret idx >= 0 && idx < l(l) ? l[idx] : false;
}

// get purpose 2: access a field by reflection or a map

static Object get(Object o, String field) {
  try {
    if (o == null) null;
    if (o instanceof Class) return get((Class) o, field);
    
    if (o instanceof Map)
      ret ((Map) o).get(field);
      
    Field f = getOpt_findField(o.getClass(), field);
    if (f != null) {
      f.setAccessible(true);
      ret f.get(o);
    }
      
    ifclass DynamicObject
      if (o instanceof DynamicObject)
        ret o/DynamicObject.fieldValues.get(field);
    endif
  } catch (Exception e) {
    throw asRuntimeException(e);
  }
  throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}

sO mapMethodLike get_raw(S field, O o) {
  ret get_raw(o, field);
}

static Object get_raw(Object o, String field) ctex {
  if (o == null) null;
  Field f = get_findField(o.getClass(), field);
  f.setAccessible(true);
  ret f.get(o);
}

static Object get(Class c, String field) {
  try {
    Field f = get_findStaticField(c, field);
    f.setAccessible(true);
    return f.get(null);
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}

static Field get_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);
  throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}

static Field get_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);
  throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}

static O mapMethodLike get(S field, O o) {
  ret get(o, field);
}

download  show line numbers  debug dex   

Travelled to 22 computer(s): aoiabmzegqzx, ayfdnjdhpjha, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, imzmzdywqqli, irmadwmeruwu, ishqpsrjomds, jozkyjcghlvl, jtubtzbbkimh, kltaiputbqfu, lnbujpyubztb, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, ppjhyzlbdabe, sawdedvomwva, tslmcundralx, tvejysmllsmz, whxojlpjdney

Comments [hide]

ID Author/Program Comment Date
1106 stefan This doesn't scan superclasses... does it? 2015-09-22 20:12:23
443 #1000604 (pitcher) 2015-08-18 14:51:38

add comment

Snippet ID: #1000619
Snippet name: get function
Eternal ID of this version: #1000619/14
Text MD5: f7cfe2aa34b851011380746971e8f2f6
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-08-13 13:19:13
Source code size: 2497 bytes / 92 lines
Pitched / IR pitched: No / No
Views / Downloads: 696 / 11935
Version history: 13 change(s)
Referenced in: [show references]