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

120
LINES

< > BotCompany Repo | #1003483 - callF + safeCallF - call a Runnable, func, voidfunc or function name

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

Transpiled version (4546L) is out of date.

1  
static Map<Class, ArrayList<Method>> callF_cache = newDangerousWeakHashMap();
2  
3  
ifclass F0
4  
  static <A> A callF(F0<A> f) {
5  
    ret f == null ? null : f.get();
6  
  }
7  
endif
8  
9  
ifclass F1
10  
  static <A, B> B callF(F1<A, B> f, A a) {
11  
    ret f == null ? null : f.get(a);
12  
  }
13  
endif
14  
15  
ifclass IF0
16  
  static <A> A callF(IF0<A> f) {
17  
    ret f == null ? null : f.get();
18  
  }
19  
endif
20  
21  
ifclass IF1
22  
  static <A, B> B callF(IF1<A, B> f, A a) {
23  
    ret f == null ? null : f.get(a);
24  
  }
25  
endif
26  
27  
static <A, B> B callF(A a, IF1<A, B> f) {
28  
  ret f == null ?: f.get(a);
29  
}
30  
31  
ifclass F2
32  
  static <A, B, C> C callF(F2<A, B, C> f, A a, B b) {
33  
    ret f == null ? null : f.get(a, b);
34  
  }
35  
endif
36  
37  
ifclass IF2
38  
  static <A, B, C> C callF(IF2<A, B, C> f, A a, B b) {
39  
    ret f == null ? null : f.get(a, b);
40  
  }
41  
endif
42  
43  
ifclass VF1
44  
  static <A> void callF(VF1<A> f, A a) {
45  
    if (f != null) f.get(a);
46  
  }
47  
endif
48  
49  
static <A> void callF(A a, IVF1<A> f) {
50  
  if (f != null) f.get(a);
51  
}
52  
53  
static <A> void callF(IVF1<A> f, A a) {
54  
  if (f != null) f.get(a);
55  
}
56  
57  
58  
sO callF(Runnable r) { r?.run(); null; }
59  
60  
sO callF(O f, O... args) {
61  
  ifdef callF_legacy
62  
  if (f instanceof S) // no way josé
63  
    ret callMCWithVarArgs((S) f, args);
64  
  endifdef
65  
    
66  
  ret safeCallF(f, args);
67  
}
68  
69  
sO safeCallF(O f, O... args) {
70  
  if (f instanceof Runnable) {
71  
    ((Runnable) f).run();
72  
    null;
73  
  }
74  
  if (f == null) null;
75  
  
76  
  Class c = f.getClass();
77  
  ArrayList<Method> methods;
78  
  synchronized(callF_cache) {
79  
    methods = callF_cache.get(c);
80  
    if (methods == null)
81  
      methods = callF_makeCache(c);
82  
  }
83  
  
84  
  int n = l(methods);
85  
  if (n == 0) {
86  
    ifclass DynamicCallable
87  
      if (f instanceof DynamicCallable)
88  
        ret f/DynamicCallable._dyn(args);
89  
    endif
90  
    ifndef callF_legacy
91  
    if (f instanceof S)
92  
      fail("Legacy call: " + f);
93  
    endifndef
94  
    fail("No get method in " + getClassName(c));
95  
  }
96  
  if (n == 1) ret invokeMethod(methods.get(0), f, args);
97  
  for i to n: {
98  
    Method m = methods.get(i);
99  
    if (call_checkArgs(m, args, false))
100  
      ret invokeMethod(m, f, args);
101  
  }
102  
  fail("No matching get method in " + getClassName(c));
103  
}
104  
105  
// used internally
106  
static ArrayList<Method> callF_makeCache(Class c) {
107  
  new ArrayList<Method> l;
108  
  Class _c = c;
109  
  do {
110  
    for (Method m : _c.getDeclaredMethods())
111  
      if (m.getName().equals("get")) {
112  
        makeAccessible(m);
113  
        l.add(m);
114  
      }
115  
    if (!l.isEmpty()) break;
116  
    _c = _c.getSuperclass();
117  
  } while (_c != null);
118  
  callF_cache.put(c, l);
119  
  ret l;
120  
}

Author comment

Began life as a copy of #1003313

download  show line numbers  debug dex  old transpilations   

Travelled to 21 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, ekrmjmnbrukm, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1003483
Snippet name: callF + safeCallF - call a Runnable, func, voidfunc or function name
Eternal ID of this version: #1003483/40
Text MD5: 6ca9fb45c9164b55eb335254b5c60fad
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-04 20:32:31
Source code size: 2580 bytes / 120 lines
Pitched / IR pitched: No / No
Views / Downloads: 766 / 1959
Version history: 39 change(s)
Referenced in: [show references]