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

53
LINES

< > BotCompany Repo | #1007834 // callMC_2 - call function in main class, optimized

JavaX fragment (include)

1  
static final HashMap<S, L<Method>> callMC_2_cache = new HashMap;
2  
static S callMC_2_key;
3  
static Method callMC_2_value;
4  
5  
// varargs assignment fixer for a single string array argument
6  
static Object callMC_2(S method, S[] arg) {
7  
  ret callMC_2(method, new O[] {arg});
8  
}
9  
10  
static O callMC_2(S method, O... args) ctex {
11  
  Method me;
12  
  synchronized(callMC_2_cache) {
13  
    me = method == callMC_2_key ? callMC_2_value : null;
14  
  }
15  
  if (me != null) ret callMC_2_value.invoke(null, args);
16  
17  
  L<Method> m = callMC_2_cache.get(method);
18  
  if (m == null) {
19  
    if (callMC_2_cache.isEmpty()) {
20  
      callMC_2_makeCache();
21  
      m = callMC_2_cache.get(method);
22  
    }
23  
    if (m == null) fail("Method named " + method + " not found in main");
24  
  }
25  
  int n = m.size();
26  
  if (n == 1) {
27  
    me = m.get(0);
28  
    synchronized(callMC_2_cache) {
29  
      callMC_2_key = method;
30  
      callMC_2_value = me;
31  
    }
32  
    ret me.invoke(null, args);
33  
  }
34  
  for i to n: {
35  
    me = m.get(i);
36  
    if (call_checkArgs(me, args, false))
37  
      ret me.invoke(null, args);
38  
  }
39  
  fail("No method called " + method + " with matching arguments found in main");
40  
}
41  
42  
static synchronized void callMC_2_makeCache() {
43  
  callMC_2_cache.clear();
44  
  Class _c = (Class) mc(), c = _c;
45  
  while (c != null) {
46  
    for (Method m : c.getDeclaredMethods())
47  
      if ((m.getModifiers() & Modifier.STATIC) != 0) {
48  
        m.setAccessible(true);
49  
        multiMapPut(callMC_2_cache, m.getName(), m);
50  
      }
51  
    c = c.getSuperclass();
52  
  }
53  
}

Author comment

Began life as a copy of #1005370

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1007834
Snippet name: callMC_2 - call function in main class, optimized
Eternal ID of this version: #1007834/7
Text MD5: c35d6961016d146a41fdc7c262cccbee
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-04-11 14:40:29
Source code size: 1518 bytes / 53 lines
Pitched / IR pitched: No / No
Views / Downloads: 537 / 558
Version history: 6 change(s)
Referenced in: [show references]