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

73
LINES

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

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

Libraryless. Click here for Pure Java version (7633L/43K).

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  
6  
final sclass _MethodCache {
7  
  final Class c;
8  
  final HashMap<S, L<Method>> cache = new HashMap;
9  
  
10  
  *(Class *c) { _init(); }
11  
  
12  
  void _init() {
13  
    Class _c = c;
14  
    java.lang.Module myModule = getClass().getModule();
15  
    bool anyHiddenClasses;
16  
    
17  
    while (_c != null) {
18  
      bool exported = classIsExportedTo(_c, myModule);
19  
      printVars ifdef _MethodCache_debug("_MethodCache._init", +_c, +exported);
20  
      
21  
      if (!exported)
22  
        set anyHiddenClasses;
23  
      else
24  
        for (Method m : _c.getDeclaredMethods())
25  
          if ((anyHiddenClasses || !isAbstract(m))
26  
            && !reflection_isForbiddenMethod(m))
27  
            multiMapPut(cache, m.getName(), makeAccessible(m));
28  
29  
      _c = _c.getSuperclass();
30  
    }
31  
    
32  
    // add default methods - this might lead to a duplication
33  
    // because the overridden method is also added, but it's not
34  
    // a problem except for minimal performance loss.
35  
    // If any classes in the hierarchy were inaccessible, we add
36  
    // all interface methods (see test_callForbiddenMethodByReflection for a test)
37  
    
38  
    for (Class intf : allInterfacesImplementedBy(c))
39  
      for (Method m : intf.getDeclaredMethods())
40  
        if ((anyHiddenClasses || m.isDefault()) && !reflection_isForbiddenMethod(m))
41  
          multiMapPut(cache, m.getName(), makeAccessible(m));
42  
43  
    print ifdef _MethodCache_debug("MethodCache " + this + ": " + className(c) + " => " + keys(cache));
44  
  }
45  
  
46  
  // Returns only matching methods
47  
  Method findMethod(S method, O[] args) ctex {
48  
    L<Method> m = cache.get(method);
49  
    print ifdef _MethodCache_debug("findMethod " + this + ": " + className(c) + "/" + method + " => " + m);
50  
    if (m == null) null;
51  
    int n = m.size();
52  
    for i to n: {
53  
      Method me = m.get(i);
54  
      if (call_checkArgs(me, args, false))
55  
        ret me;
56  
    }
57  
    null;
58  
  }
59  
  
60  
  Method findStaticMethod(S method, O[] args) ctex {
61  
    L<Method> m = cache.get(method);
62  
    if (m == null) null;
63  
    int n = m.size();
64  
    for i to n: {
65  
      Method me = m.get(i);
66  
      if (isStaticMethod(me) && call_checkArgs(me, args, false))
67  
        ret me;
68  
    }
69  
    null;
70  
  }
71  
  
72  
  //Cl<Method> allMethods() { ret allValues(cache); }
73  
}

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/29
Text MD5: ca089a15a737d1f1a5ee6074d102fd52
Transpilation MD5: 679aa3a130c44e61f8c248153f03b879
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-08 06:19:22
Source code size: 2429 bytes / 73 lines
Pitched / IR pitched: No / No
Views / Downloads: 491 / 1245
Version history: 28 change(s)
Referenced in: [show references]