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

95
LINES

< > BotCompany Repo | #1004665 // JavaXClassLoader (LIVE)

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

Libraryless. Click here for Pure Java version (6007L/34K).

// progID is just for information
static class JavaXClassLoader extends URLClassLoader {
  S progID;
  Set<File> files = syncLinkedHashSet();
  Set<S> libraryIDs = syncLinkedHashSet();
  Set<S> triedToLoad = synchroSet();
  Set<Class> loadedClasses = synchroSet();
  bool retired;
  O retiredMarker;
  IF1<S, Class> findClass_extension;
  S mainClassName;
  bool verbose;
  
  *(S progID, L<File> files) {
    this(progID, files, getSystemClassLoader());
  }
  
  *(S progID, L<File> files, ClassLoader parent) {
    // sadly can't see this constructor
    //super(progID, new URL[0], parent, vm_globalACC());
    super(new URL[0], parent);
    this.progID = progID;

    fOr (File f : files)
      addFile(f);
      
    // TODO: how to get around this
    fixACCInClassLoader(this);
  }
  
  Class<?> super_findClass(S name) throws ClassNotFoundException {
    ret super.findClass(name);
  }
  
  protected Class<?> findClass(S name) throws ClassNotFoundException {
    if (verbose) System.out.println(this + " findClass: " + name);
    if (findClass_extension != null) {
      Class<?> c = findClass_extension.get(name);
      if (verbose) System.out.println("extension returned: " + c);
      if (c != null) ret c;
    }
    bool triedBefore = !triedToLoad.add(name);
    try {
      Class<?> c = super.findClass(name);
      if (verbose) System.out.println("super.findClass returned: " + c);
      loadedClasses.add(c);
      if (eq(name, mainClassName()))
        callOpt(javax(), 'registerAMainClass, c);
      ret c;
    } catch (ClassNotFoundException e) {
      if (verbose) System.out.println(getStackTrace(e));
      throw new ClassNotFoundException("Class " + name + " not found in " + joinWithComma(map f2s(files)) + " (progID=" + progID + ")"
        + (triedBefore ? ", tried to load before" : ""), e);
    }
  }
  
  toString {
    ret shortClassName(this) + "[" + systemHashCodeHex(this) + "] - " + progID;
  }
  
  S mainClassName() {
    if (mainClassName == null) {
      mainClassName = "main";
      pcall {
        mainClassName = or2(trim(loadTextFileResource(this, "main-class")), mainClassName);
      }
    }
    ret mainClassName;
  }
  
  bool addFile(File f, S libraryID) {
    if (nempty(libraryID))
      if (!libraryIDs.add(libraryID))
        false;
    ret addFile(f);
  }
  
  bool addFile(File f) ctex {
    if (!files.add(f)) false;
    addURL(f.toURI().toURL());
    mainClassName(); // calculate early. no good loading text files later
    true;
  }
  
  Set<S> libraryIDs() { ret libraryIDs; }
  
  bool hasLibraryID(S libraryID) { ret libraryIDs.contains(libraryID); }
  
  protected swappable S findLibrary(S libname) { ret super.findLibrary(libname); }
  
  // make public
  public void addURL(URL url) {
    super.addURL(url);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 17 computer(s): aoiabmzegqzx, ayivmpnvhhik, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1004665
Snippet name: JavaXClassLoader (LIVE)
Eternal ID of this version: #1004665/56
Text MD5: 63856002cffd2944a3ee48ad3cd0aaa3
Transpilation MD5: 8fe4c94aa0918124e1832a31df3941bd
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-02-05 01:46:24
Source code size: 2872 bytes / 95 lines
Pitched / IR pitched: No / No
Views / Downloads: 783 / 2180
Version history: 55 change(s)
Referenced in: [show references]