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

94
LINES

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

JavaX fragment (include)

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

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 O callF(O f, O... args) ctex {
  if (f instanceof S)
    ret callMCWithVarArgs((S) f, args); // possible SLOWDOWN over callMC
  if (f instanceof Runnable) {
    ((Runnable) f).run();
    null;
  }
  if (f == null) ret 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   

Travelled to 14 computer(s): aoiabmzegqzx, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, sawdedvomwva, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #1003483
Snippet name: callF - call a Runnable, func, voidfunc or function name
Eternal ID of this version: #1003483/29
Text MD5: 746c9cc0f99c72e57b4a2109f8b7011f
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-01-20 18:39:03
Source code size: 2137 bytes / 94 lines
Pitched / IR pitched: No / No
Views / Downloads: 511 / 1535
Version history: 28 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1003483 & http://1003483.tinybrain.de