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

22
LINES

< > BotCompany Repo | #1001137 // findClass

JavaX fragment (include)

static new HashMap<S, Class> findClass_cache;

// currently finds only inner classes of class "main"
// returns null on not found
// this is the simple version that is not case-tolerant
static Class findClass(String name) {
  synchronized(findClass_cache) {
    if (findClass_cache.containsKey(name))
      ret findClass_cache.get(name);
      
    if (!isJavaIdentifier(name)) ret null;
    
    Class c;
    try {
      c = Class.forName("main$" + name);
    } catch (ClassNotFoundException e) {
      c = null;
    }
    findClass_cache.put(name, c);
    ret c;
  }
}

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: 743 / 4592
Referenced in: #1002427 - Accellerating 629 (SPIKE)
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1020753 - findClass_fullName
#3000382 - Answer for ferdie (>> t = 1, f = 0)