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

22
LINES

< > BotCompany Repo | #1001137 // findClass

JavaX fragment (include)

1  
static new HashMap<S, Class> findClass_cache;
2  
3  
// currently finds only inner classes of class "main"
4  
// returns null on not found
5  
// this is the simple version that is not case-tolerant
6  
static Class findClass(String name) {
7  
  synchronized(findClass_cache) {
8  
    if (findClass_cache.containsKey(name))
9  
      ret findClass_cache.get(name);
10  
      
11  
    if (!isJavaIdentifier(name)) ret null;
12  
    
13  
    Class c;
14  
    try {
15  
      c = Class.forName("main$" + name);
16  
    } catch (ClassNotFoundException e) {
17  
      c = null;
18  
    }
19  
    findClass_cache.put(name, c);
20  
    ret c;
21  
  }
22  
}

Author comment

The cache can contain null values - it's a very important optimization if you look for non-existing classes a lot (e.g. when loading foreign concepts without having the custom classes).

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, qsqiayxyrbia, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1001137
Snippet name: findClass
Eternal ID of this version: #1001137/1
Text MD5: d75ff28f0ae40817234f1333213d4585
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-11-29 15:18:23
Source code size: 591 bytes / 22 lines
Pitched / IR pitched: No / No
Views / Downloads: 865 / 4715
Referenced in: [show references]