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

96
LINES

< > BotCompany Repo | #1000619 // get function

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

Transpiled version (2740L) 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) {
      makeAccessible(f);
      ret f.get(o);
    }
      
    ifclass DynamicObject
      if (o cast DynamicObject)
        ret getOptDynOnly(o, 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);
  makeAccessible(f);
  ret f.get(o);
}

static Object get(Class c, String field) {
  try {
    Field f = get_findStaticField(c, field);
    makeAccessible(f);
    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);
}

sbool get(BitSet bs, int idx) {
  ret bs != null && bs.get(idx);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 29 computer(s): aoiabmzegqzx, ayfdnjdhpjha, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, imzmzdywqqli, irmadwmeruwu, ishqpsrjomds, jozkyjcghlvl, jtubtzbbkimh, kltaiputbqfu, lnbujpyubztb, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, podlckwnjdmb, ppjhyzlbdabe, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, whxojlpjdney, xrpafgyirdlv

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/17
Text MD5: b698bb3cf617d701e5299944cab25fab
Author: stefan
Category:
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-26 22:39:18
Source code size: 2536 bytes / 96 lines
Pitched / IR pitched: No / No
Views / Downloads: 1550 / 13625
Version history: 16 change(s)
Referenced in: [show references]