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

59
LINES

< > BotCompany Repo | #1011182 // callMC - call function in main class, optimizing [dev.]

JavaX fragment (include)

static final HashMap<S, Method[]> callMC_cache = new HashMap;
static S callMC_key;
static Method callMC_value;

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

static O callMC(S method, O... args) ctex {
  Method me;
  synchronized(callMC_cache) {
    me = method == callMC_key ? callMC_value : null;
  }
  if (me != null) ret callMC_value.invoke(null, args);

  Method[] m;
  synchronized(callMC_cache) {
    m = callMC_cache.get(method);
  }
  if (m == null) {
    if (callMC_cache.isEmpty()) {
      callMC_makeCache();
      m = callMC_cache.get(method);
    }
    if (m == null) fail("Method named " + method + " not found in main");
  }
  int n = m.length;
  if (n == 1) {
    me = m[0];
    synchronized(callMC_cache) {
      callMC_key = method;
      callMC_value = me;
    }
    ret me.invoke(null, args);
  }
  for i to n: {
    me = m[i];
    if (call_checkArgs(me, args, false))
      ret me.invoke(null, args);
  }
  fail("No method called " + method + " with matching arguments found in main");
}

static void callMC_makeCache() {
  synchronized(callMC_cache) {
    callMC_cache.clear();
    Class _c = (Class) mc(), c = _c;
    while (c != null) {
      for (Method m : c.getDeclaredMethods())
        if ((m.getModifiers() & Modifier.STATIC) != 0
          && !reflection_isForbiddenMethod(m)) {
          m.setAccessible(true);
          multiMapPut_array(callMC_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: #1011182
Snippet name: callMC - call function in main class, optimizing [dev.]
Eternal ID of this version: #1011182/2
Text MD5: 54307baabad9c43616171845a06decb0
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-04-01 04:59:25
Source code size: 1610 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 354 / 375
Version history: 1 change(s)
Referenced in: [show references]