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.

ifndef EnforceFunctionTypes
static Map<Class, ArrayList<Method>> callF_cache = newDangerousWeakHashMap();
endifndef

ifclass F0
  static <A> A callF(F0<A> f) {
    ret f == null ? null : f.get();
  }
endif

ifclass F1
  static <A, B> B callF(F1<A, B> f, A a) {
    ret f == null ? null : f.get(a);
  }
endif

ifclass IF0
  static <A> A callF(IF0<A> f) {
    ret f == null ? null : f.get();
  }
endif

ifclass IF1
  static <A, B> B callF(IF1<A, B> f, A a) {
    ret f == null ? null : f.get(a);
  }
endif

static <A, B> B callF(A a, IF1<A, B> f) {
  ret f == null ?: f.get(a);
}

ifclass F2
  static <A, B, C> C callF(F2<A, B, C> f, A a, B b) {
    ret f == null ? null : f.get(a, b);
  }
endif

ifclass IF2
  static <A, B, C> C callF(IF2<A, B, C> f, A a, B b) {
    ret f == null ? null : f.get(a, b);
  }
endif

ifclass VF1
  static <A> void callF(VF1<A> f, A a) {
    if (f != null) f.get(a);
  }
endif

static <A> void callF(A a, IVF1<A> f) {
  if (f != null) f.get(a);
}

static <A> void callF(IVF1<A> f, A a) {
  if (f != null) f.get(a);
}


sO callF(Runnable r) { r?.run(); null; }

ifndef EnforceFunctionTypes
sO callF(O f, O... args) {
  ifdef callF_legacy
  if (f instanceof S) // no way josé
    ret callMCWithVarArgs((S) f, args);
  endifdef
    
  ret safeCallF(f, args);
}

sO safeCallF(O f, O... args) {
  if (f instanceof Runnable) {
    ((Runnable) f).run();
    null;
  }
  if (f == null) null;
  
  Class c = f.getClass();
  ArrayList<Method> methods;
  synchronized(callF_cache) {
    methods = callF_cache.get(c);
    if (methods == null)
      methods = callF_makeCache(c);
  }
  
  int n = l(methods);
  if (n == 0) {
    ifclass DynamicCallable
      if (f instanceof DynamicCallable)
        ret f/DynamicCallable._dyn(args);
    endif
    ifndef callF_legacy
    if (f instanceof S)
      fail("Legacy call: " + f);
    endifndef
    fail("No get method in " + getClassName(c));
  }
  if (n == 1) ret invokeMethod(methods.get(0), f, args);
  for i to n: {
    Method m = methods.get(i);
    if (call_checkArgs(m, args, false))
      ret invokeMethod(m, f, args);
  }
  fail("No matching get method in " + getClassName(c));
}

// used internally
static ArrayList<Method> callF_makeCache(Class c) {
  new ArrayList<Method> l;
  Class _c = c;
  do {
    for (Method m : _c.getDeclaredMethods())
      if (m.getName().equals("get")) {
        makeAccessible(m);
        l.add(m);
      }
    if (!l.isEmpty()) break;
    _c = _c.getSuperclass();
  } while (_c != null);
  callF_cache.put(c, l);
  ret l;
}

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: 980 / 2231
Version history: 42 change(s)
Referenced in: [show references]