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

60
LINES

< > BotCompany Repo | #1034186 // _MethodCache - caches methods of a class [backup before Java 9 fix]

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 #1018496

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034186
Snippet name: _MethodCache - caches methods of a class [backup before Java 9 fix]
Eternal ID of this version: #1034186/1
Text MD5: 10d11f8a082ab673e0ebd44558876a80
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-26 01:02:11
Source code size: 1888 bytes / 60 lines
Pitched / IR pitched: No / No
Views / Downloads: 78 / 98
Referenced in: [show references]