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

66
LINES

< > BotCompany Repo | #1031279 // Change module's class loader at runtime Spike [OK]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 1113K of libraries. Click here for Pure Java version (7679L/43K).

!7

set flag DynModule. // use OS transpiler

// We are trying to add classes directly in to the module's class loader
// without any sub-class loaders

cprint {
  transient ClassLoader cl;
  
  start-thread {
    cl = getClassLoader(module());
    print("My class loader: " + cl);
    print("Class loader type: " + className(cl));
    Map<S, ClassLoader> parents = cast getOpt parentMap(cl);
    print("Parents: " + parents);
    
    hotwireTest();
  }
  
  void hotwireTest ctex {
    S name = aGlobalID();
    print("Class name: " + name);
    S mcName = "main_" + name;
    S src = "mainClassName " + mcName + "\n\n"
      + "sclass " + name + " {\n"
      + "  S name() {\n"
      + "    ret " + quote("I am " + name) + ";\n"
      + "  }\n"
      + "}";
    print();
    printWithIndent("SRC> ", src);
    print();
    
    // taken from veryQuickJava3
    new LS libs;
    src = transpileRaw(src); // transpiled, with lib references
    
    // taken from veryQuickJava_finish
    src = findTranslators2(src, libs);
    S dehlibs = join(" ", libs);
    File bytecode = javaCompile_overInternalBot(src, dehlibs);
    print(+bytecode);
    print("Files: " + listZip(bytecode));
    
    //print("Name: " + call(o, "name"));
    
    /*set(cl, findClass_extension := (IF1<S, Class>) name -> {
      if (equalsOrStartsWithPlusDollar(name, mcName))
      null;
    });*/
    
    call(cl, "addURL", bytecode.toURI().toURL());
    
    Class c = Class.forName(mcName);
    print("Resolved: " + c);
    
    O o = newInstance(Class.forName(mcName + "$" + name));
    print("Instance: " + o);
    print();
    O result = call(o, "name");
    print("Result of function call: " + result);
    
    assertEquals("I am " + name, result);
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1031279
Snippet name: Change module's class loader at runtime Spike [OK]
Eternal ID of this version: #1031279/25
Text MD5: 6377dfec0492122847e8bb8ef4faf6ab
Transpilation MD5: 4e90c0bc1435d2e9038cb811e69187ed
Author: stefan
Category: javax
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-06-02 18:13:10
Source code size: 1806 bytes / 66 lines
Pitched / IR pitched: No / No
Views / Downloads: 198 / 402
Version history: 24 change(s)
Referenced in: [show references]