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

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

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/43
Text MD5: 0553da4adea2061ac52bbb188d4c73d9
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-12 22:48:03
Source code size: 2664 bytes / 125 lines
Pitched / IR pitched: No / No
Views / Downloads: 983 / 2234
Version history: 42 change(s)
Referenced in: [show references]