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)

1  
// immutable, has strong refs
2  
// Do not run in a synchronized block - it goes wrong in the presence
3  
// of elaborate classloaders (like in Gazelle BEA)
4  
// see #1102990 and #1102991
5  
final sclass _MethodCache {
6  
  final Class c;
7  
  final HashMap<S, L<Method>> cache = new HashMap;
8  
  
9  
  *(Class *c) { _init(); }
10  
  
11  
  void _init() {
12  
    Class _c = c;
13  
    while (_c != null) {
14  
      for (Method m : _c.getDeclaredMethods())
15  
        if (!isAbstract(m) && !reflection_isForbiddenMethod(m))
16  
          multiMapPut(cache, m.getName(), makeAccessible(m));
17  
      _c = _c.getSuperclass();
18  
    }
19  
    
20  
    // add default methods - this might lead to a duplication
21  
    // because the overridden method is also added, but it's not
22  
    // a problem except for minimal performance loss.
23  
    for (Class intf : allInterfacesImplementedBy(c))
24  
      for (Method m : intf.getDeclaredMethods())
25  
        if (m.isDefault() && !reflection_isForbiddenMethod(m))
26  
          multiMapPut(cache, m.getName(), makeAccessible(m));
27  
28  
    ifdef _MethodCache_debug
29  
      print("MethodCache " + this + ": " + className(c) + " => " + keys(cache));
30  
    endifdef
31  
  }
32  
  
33  
  // Returns only matching methods
34  
  Method findMethod(S method, O[] args) ctex {
35  
    L<Method> m = cache.get(method);
36  
    ifdef _MethodCache_debug
37  
      print("findMethod " + this + ": " + className(c) + "/" + method + " => " + m);
38  
    endifdef
39  
    if (m == null) null;
40  
    int n = m.size();
41  
    for i to n: {
42  
      Method me = m.get(i);
43  
      if (call_checkArgs(me, args, false))
44  
        ret me;
45  
    }
46  
    null;
47  
  }
48  
  
49  
  Method findStaticMethod(S method, O[] args) ctex {
50  
    L<Method> m = cache.get(method);
51  
    if (m == null) null;
52  
    int n = m.size();
53  
    for i to n: {
54  
      Method me = m.get(i);
55  
      if (isStaticMethod(me) && call_checkArgs(me, args, false))
56  
        ret me;
57  
    }
58  
    null;
59  
  }
60  
}

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: 133 / 166
Referenced in: [show references]