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).

1  
// progID is just for information
2  
static class JavaXClassLoader extends URLClassLoader {
3  
  S progID;
4  
  Set<File> files = syncLinkedHashSet();
5  
  Set<S> libraryIDs = syncLinkedHashSet();
6  
  Set<S> triedToLoad = synchroSet();
7  
  Set<Class> loadedClasses = synchroSet();
8  
  bool retired;
9  
  O retiredMarker;
10  
  IF1<S, Class> findClass_extension;
11  
  S mainClassName;
12  
  bool verbose;
13  
  
14  
  *(S progID, L<File> files) {
15  
    this(progID, files, getSystemClassLoader());
16  
  }
17  
  
18  
  *(S progID, L<File> files, ClassLoader parent) {
19  
    // sadly can't see this constructor
20  
    //super(progID, new URL[0], parent, vm_globalACC());
21  
    super(new URL[0], parent);
22  
    this.progID = progID;
23  
24  
    fOr (File f : files)
25  
      addFile(f);
26  
      
27  
    // TODO: how to get around this
28  
    fixACCInClassLoader(this);
29  
  }
30  
  
31  
  Class<?> super_findClass(S name) throws ClassNotFoundException {
32  
    ret super.findClass(name);
33  
  }
34  
  
35  
  protected Class<?> findClass(S name) throws ClassNotFoundException {
36  
    if (verbose) System.out.println(this + " findClass: " + name);
37  
    if (findClass_extension != null) {
38  
      Class<?> c = findClass_extension.get(name);
39  
      if (verbose) System.out.println("extension returned: " + c);
40  
      if (c != null) ret c;
41  
    }
42  
    bool triedBefore = !triedToLoad.add(name);
43  
    try {
44  
      Class<?> c = super.findClass(name);
45  
      if (verbose) System.out.println("super.findClass returned: " + c);
46  
      loadedClasses.add(c);
47  
      if (eq(name, mainClassName()))
48  
        callOpt(javax(), 'registerAMainClass, c);
49  
      ret c;
50  
    } catch (ClassNotFoundException e) {
51  
      if (verbose) System.out.println(getStackTrace(e));
52  
      throw new ClassNotFoundException("Class " + name + " not found in " + joinWithComma(map f2s(files)) + " (progID=" + progID + ")"
53  
        + (triedBefore ? ", tried to load before" : ""), e);
54  
    }
55  
  }
56  
  
57  
  toString {
58  
    ret shortClassName(this) + "[" + systemHashCodeHex(this) + "] - " + progID;
59  
  }
60  
  
61  
  S mainClassName() {
62  
    if (mainClassName == null) {
63  
      mainClassName = "main";
64  
      pcall {
65  
        mainClassName = or2(trim(loadTextFileResource(this, "main-class")), mainClassName);
66  
      }
67  
    }
68  
    ret mainClassName;
69  
  }
70  
  
71  
  bool addFile(File f, S libraryID) {
72  
    if (nempty(libraryID))
73  
      if (!libraryIDs.add(libraryID))
74  
        false;
75  
    ret addFile(f);
76  
  }
77  
  
78  
  bool addFile(File f) ctex {
79  
    if (!files.add(f)) false;
80  
    addURL(f.toURI().toURL());
81  
    mainClassName(); // calculate early. no good loading text files later
82  
    true;
83  
  }
84  
  
85  
  Set<S> libraryIDs() { ret libraryIDs; }
86  
  
87  
  bool hasLibraryID(S libraryID) { ret libraryIDs.contains(libraryID); }
88  
  
89  
  protected swappable S findLibrary(S libname) { ret super.findLibrary(libname); }
90  
  
91  
  // make public
92  
  public void addURL(URL url) {
93  
    super.addURL(url);
94  
  }
95  
}

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: 786 / 2186
Version history: 55 change(s)
Referenced in: [show references]