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

60
LINES

< > BotCompany Repo | #1018496 - _MethodCache - caches methods of a class

JavaX fragment (include)

// immutable, has strong refs
// Do not run in a synchronized block - it goes wrong in the presence
// of elaborate classloaders (like in Gazelle BEA)
// see #1102990 and #1102991
final sclass _MethodCache {
  final Class c;
  final HashMap<S, L<Method>> cache = new HashMap;
  
  *(Class *c) { _init(); }
  
  void _init() {
    Class _c = c;
    while (_c != null) {
      for (Method m : _c.getDeclaredMethods())
        if (!isAbstract(m) && !reflection_isForbiddenMethod(m))
          multiMapPut(cache, m.getName(), makeAccessible(m));
      _c = _c.getSuperclass();
    }
    
    // add default methods - this might lead to a duplication
    // because the overridden method is also added, but it's not
    // a problem except for minimal performance loss.
    for (Class intf : allInterfacesImplementedBy(c))
      for (Method m : intf.getDeclaredMethods())
        if (m.isDefault() && !reflection_isForbiddenMethod(m))
          multiMapPut(cache, m.getName(), makeAccessible(m));

    ifdef _MethodCache_debug
      print("MethodCache " + this + ": " + className(c) + " => " + keys(cache));
    endifdef
  }
  
  // Returns only matching methods
  Method findMethod(S method, O[] args) ctex {
    L<Method> m = cache.get(method);
    ifdef _MethodCache_debug
      print("findMethod " + this + ": " + className(c) + "/" + method + " => " + m);
    endifdef
    if (m == null) null;
    int n = m.size();
    for i to n: {
      Method me = m.get(i);
      if (call_checkArgs(me, args, false))
        ret me;
    }
    null;
  }
  
  Method findStaticMethod(S method, O[] args) ctex {
    L<Method> m = cache.get(method);
    if (m == null) null;
    int n = m.size();
    for i to n: {
      Method me = m.get(i);
      if (isStaticMethod(me) && call_checkArgs(me, args, false))
        ret me;
    }
    null;
  }
}

Author comment

Began life as a copy of #1005370

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1018496
Snippet name: _MethodCache - caches methods of a class
Eternal ID of this version: #1018496/22
Text MD5: 10d11f8a082ab673e0ebd44558876a80
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:04:02
Source code size: 1888 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 245 / 885
Version history: 21 change(s)
Referenced in: [show references]