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   

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

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

Formerly at http://tinybrain.de/1001137 & http://1001137.tinybrain.de