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

67
LINES

< > BotCompany Repo | #1005370 // callMC - call function in main class

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

Libraryless. Click here for Pure Java version (2117L/13K).

static HashMap<S, L<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;
  if (callMC_cache == null) callMC_cache = new HashMap; // initializer time workaround
  synchronized(callMC_cache) {
    me = method == callMC_key ? callMC_value : null;
  }
  if (me != null) try {
    ret invokeMethod(me, null, args);
  } catch (IllegalArgumentException e) {
    throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e);
  }

  L<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.size();
  if (n == 1) {
    me = m.get(0);
    synchronized(callMC_cache) {
      callMC_key = method;
      callMC_value = me;
    }
    try {
      ret invokeMethod(me, null, args);
    } catch (IllegalArgumentException e) {
      throw new RuntimeException("Can't call " + me + " with arguments " + classNames(args), e);
    }
  }
  for i to n: {
    me = m.get(i);
    if (call_checkArgs(me, args, false))
      ret invokeMethod(me, null, args);
  }
  fail("No method called " + method + " with arguments (" + joinWithComma(getClasses(args)) + ") 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() & java.lang.reflect.Modifier.STATIC) != 0) {
          makeAccessible(m);
          multiMapPut(callMC_cache, m.getName(), m);
        }
      c = c.getSuperclass();
    }
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1005370
Snippet name: callMC - call function in main class
Eternal ID of this version: #1005370/16
Text MD5: d6d257d7568bc1f8e4cc6329c0146862
Transpilation MD5: de25577a3d86202fafd0c455241400ea
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-17 02:17:06
Source code size: 2023 bytes / 67 lines
Pitched / IR pitched: No / No
Views / Downloads: 682 / 885
Version history: 15 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1007834 - callMC_2 - call function in main class, optimized
#1007836 - callMC - call function in main class [OLD]
#1009339 - callMC_precise - call function in main class, precisely looking it up [might be slow]
#1011182 - callMC - call function in main class, optimizing [dev.]
#1018496 - _MethodCache - caches methods of a class