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

116
LINES

< > BotCompany Repo | #1003483 - callF + safeCallF - call a Runnable, func, voidfunc or function name

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

Transpiled version (4481L) is out of date.

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

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; }

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
    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;
}

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/37
Text MD5: 1ac5dd52c306535ae781e1f320a2b623
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-10-24 16:52:19
Source code size: 2481 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 728 / 1895
Version history: 36 change(s)
Referenced in: [show references]