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

32
LINES

< > BotCompany Repo | #1018497 // callOpt_cached (optimized version of callOpt)

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

Libraryless. Click here for Pure Java version (2817L/17K).

1  
static final Map<Class, _MethodCache> callOpt_cache = newDangerousWeakHashMap();
2  
3  
static O callOpt_cached(O o, S methodName, O... args) ctex {
4  
  if (o == null) null;
5  
  
6  
  if (o instanceof Class) {
7  
    Class c = (Class) o;
8  
    _MethodCache cache = callOpt_getCache(c);
9  
    
10  
    // TODO: (super-rare) case where method exists static and non-static
11  
    // with different args
12  
    
13  
    Method me = cache.findMethod(methodName, args);
14  
    if (me == null || (me.getModifiers() & Modifier.STATIC) == 0) null;
15  
    ret invokeMethod(me, null, args);
16  
  } else {
17  
    Class c = o.getClass();
18  
    _MethodCache cache = callOpt_getCache(c);
19  
20  
    Method me = cache.findMethod(methodName, args);
21  
    if (me == null) null;
22  
    ret invokeMethod(me, o, args);
23  
  }
24  
}
25  
26  
// no longer synchronizes! (see #1102990)
27  
static _MethodCache callOpt_getCache(Class c) {
28  
  _MethodCache cache = callOpt_cache.get(c);
29  
  if (cache == null)
30  
    callOpt_cache.put(c, cache = _MethodCache(c));
31  
  ret cache;
32  
}

Author comment

Began life as a copy of #1005583

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1018497
Snippet name: callOpt_cached (optimized version of callOpt)
Eternal ID of this version: #1018497/10
Text MD5: 73a3166f4054df018e85d66a42a17443
Transpilation MD5: ca8018bc2f769363f5f7459f33c01e1a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-22 12:09:53
Source code size: 998 bytes / 32 lines
Pitched / IR pitched: No / No
Views / Downloads: 454 / 573
Version history: 9 change(s)
Referenced in: [show references]