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

43
LINES

< > BotCompany Repo | #2000479 // Wiring runner

New Tinybrain snippet

1  
!636
2  
!700 // ctex
3  
4  
!include #2000470 // class _x16
5  
6  
main {
7  
  psvm {
8  
    Class<?> solver = compileAndLoadMainClass(loadSnippet("#695"));
9  
    Object case = call(solver, "parse", "#1000386");
10  
    call(solver, "calculate", case);
11  
  }
12  
  
13  
  static Object call(Object o, String method, Object... args) ctex {
14  
    findMethod(o, method, args).invoke(o, args);
15  
  }
16  
  
17  
  static Method findMethod(Object o, String method, Object[] args) {
18  
    // TODO: go to superclasses too
19  
    for (Method method : theClass.getMethods())
20  
      if (method.getName().equals("main") && method.getParameterTypes().length == 1)
21  
        return method;
22  
    throw new RuntimeException("Method 'main' with 1 parameter not found in " + theClass.getName());
23  
  }
24  
  
25  
  // compile JavaX source and load main class
26  
  static Class<?> compileAndLoadMainClass(String src) tex {
27  
    File srcDir = _x16.TempDirMaker_make();
28  
    File classesDir = _x16.TempDirMaker_make();
29  
    _x16.saveTextFile(new File(srcDir, "main.java").getPath(), src);
30  
    new List<File> libraries;
31  
    File transpiledDir = _x16.topLevelTranslate(srcDir, libraries);
32  
    String javacOutput = _x16.compileJava(transpiledDir, libraries, classesDir);
33  
    System.out.println(javacOutput);
34  
    URL[] urls = {classesDir.toURI().toURL()};
35  
    
36  
    // make class loader
37  
    URLClassLoader classLoader = new URLClassLoader(urls);
38  
39  
    // load main class
40  
    Class<?> mainClass = classLoader.loadClass("main");
41  
    return mainClass;
42  
  }
43  
}

download  show line numbers   

Snippet is not live.

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

Comments [hide]

ID Author/Program Comment Date
553 #1000610 (pitcher) 2015-08-18 00:07:07
552 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

Snippet ID: #2000479
Snippet name: Wiring runner
Eternal ID of this version: #2000479/1
Text MD5: 8f3e5e9ff883f2bf3842874bd090152b
Author: stefan
Category:
Type: New Tinybrain snippet
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-02 01:37:17
Source code size: 1495 bytes / 43 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 782 / 153
Referenced in: [show references]