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)

static final HashMap<S, L<Method>> callMC_2_cache = new HashMap;
static S callMC_2_key;
static Method callMC_2_value;

// varargs assignment fixer for a single string array argument
static Object callMC_2(S method, S[] arg) {
  ret callMC_2(method, new O[] {arg});
}

static O callMC_2(S method, O... args) ctex {
  Method me;
  synchronized(callMC_2_cache) {
    me = method == callMC_2_key ? callMC_2_value : null;
  }
  if (me != null) ret callMC_2_value.invoke(null, args);

  L<Method> m = callMC_2_cache.get(method);
  if (m == null) {
    if (callMC_2_cache.isEmpty()) {
      callMC_2_makeCache();
      m = callMC_2_cache.get(method);
    }
    if (m == null) fail("Method named " + method + " not found in main");
  }
  int n = m.size();
  if (n == 1) {
    me = m.get(0);
    synchronized(callMC_2_cache) {
      callMC_2_key = method;
      callMC_2_value = me;
    }
    ret me.invoke(null, args);
  }
  for i to n: {
    me = m.get(i);
    if (call_checkArgs(me, args, false))
      ret me.invoke(null, args);
  }
  fail("No method called " + method + " with matching arguments found in main");
}

static synchronized void callMC_2_makeCache() {
  callMC_2_cache.clear();
  Class _c = (Class) mc(), c = _c;
  while (c != null) {
    for (Method m : c.getDeclaredMethods())
      if ((m.getModifiers() & Modifier.STATIC) != 0) {
        m.setAccessible(true);
        multiMapPut(callMC_2_cache, m.getName(), m);
      }
    c = c.getSuperclass();
  }
}

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: 535 / 555
Version history: 6 change(s)
Referenced in: [show references]