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.

1  
// get purpose 1: access a list/array/map (safer version of x.get(y))
2  
3  
static <A> A get(L<A> l, int idx) {
4  
  ret l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
5  
}
6  
7  
// seems to conflict with other signatures
8  
/*static <A, B> B get(Map<A, B> map, A key) {
9  
  ret map != null ? map.get(key) : null;
10  
}*/
11  
12  
static <A> A get(A[] l, int idx) {
13  
  ret idx >= 0 && idx < l(l) ? l[idx] : null;
14  
}
15  
16  
// default to false
17  
static bool get(bool[] l, int idx) {
18  
  ret idx >= 0 && idx < l(l) ? l[idx] : false;
19  
}
20  
21  
// get purpose 2: access a field by reflection or a map
22  
23  
static Object get(Object o, String field) {
24  
  try {
25  
    if (o == null) null;
26  
    if (o instanceof Class) return get((Class) o, field);
27  
    
28  
    if (o instanceof Map)
29  
      ret ((Map) o).get(field);
30  
      
31  
    Field f = getOpt_findField(o.getClass(), field);
32  
    if (f != null) {
33  
      makeAccessible(f);
34  
      ret f.get(o);
35  
    }
36  
      
37  
    ifclass DynamicObject
38  
      if (o cast DynamicObject)
39  
        ret getOptDynOnly(o, field);
40  
    endif
41  
  } catch (Exception e) {
42  
    throw asRuntimeException(e);
43  
  }
44  
  throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
45  
}
46  
47  
sO mapMethodLike get_raw(S field, O o) {
48  
  ret get_raw(o, field);
49  
}
50  
51  
static Object get_raw(Object o, String field) ctex {
52  
  if (o == null) null;
53  
  Field f = get_findField(o.getClass(), field);
54  
  makeAccessible(f);
55  
  ret f.get(o);
56  
}
57  
58  
static Object get(Class c, String field) {
59  
  try {
60  
    Field f = get_findStaticField(c, field);
61  
    makeAccessible(f);
62  
    return f.get(null);
63  
  } catch (Exception e) {
64  
    throw new RuntimeException(e);
65  
  }
66  
}
67  
68  
static Field get_findStaticField(Class<?> c, String field) {
69  
  Class _c = c;
70  
  do {
71  
    for (Field f : _c.getDeclaredFields())
72  
      if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
73  
        return f;
74  
    _c = _c.getSuperclass();
75  
  } while (_c != null);
76  
  throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
77  
}
78  
79  
static Field get_findField(Class<?> c, String field) {
80  
  Class _c = c;
81  
  do {
82  
    for (Field f : _c.getDeclaredFields())
83  
      if (f.getName().equals(field))
84  
        return f;
85  
    _c = _c.getSuperclass();
86  
  } while (_c != null);
87  
  throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
88  
}
89  
90  
static O mapMethodLike get(S field, O o) {
91  
  ret get(o, field);
92  
}
93  
94  
sbool get(BitSet bs, int idx) {
95  
  ret bs != null && bs.get(idx);
96  
}

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: 1559 / 13638
Version history: 16 change(s)
Referenced in: [show references]