| 1 | // requires class _javax and ctex | 
| 2 | |
| 3 | static String execute(String src) ctex {
 | 
| 4 | Class<?> mainClass = execute_compileAndLoadMainClass(src); | 
| 5 | Method m = execute_findCalcMethod(mainClass); | 
| 6 | return String.valueOf(m.invoke(null)); | 
| 7 | } | 
| 8 | |
| 9 | // compile JavaX source and load main class | 
| 10 | static Class<?> execute_compileAndLoadMainClass(String src) tex {
 | 
| 11 | File srcDir = _javax.TempDirMaker_make(); | 
| 12 | File classesDir = _javax.TempDirMaker_make(); | 
| 13 | _javax.saveTextFile(new File(srcDir, "main.java").getPath(), src); | 
| 14 | new List<File> libraries; | 
| 15 | _javax.safeTranslate = true; | 
| 16 | File transpiledDir = _javax.topLevelTranslate(srcDir, libraries); | 
| 17 | String javacOutput = _javax.compileJava(transpiledDir, libraries, classesDir); | 
| 18 | System.out.println(javacOutput); | 
| 19 |   URL[] urls = {classesDir.toURI().toURL()};
 | 
| 20 | |
| 21 | // make class loader | 
| 22 | URLClassLoader classLoader = new URLClassLoader(urls); | 
| 23 | |
| 24 | // load main class | 
| 25 |   Class<?> mainClass = classLoader.loadClass("main");
 | 
| 26 | return mainClass; | 
| 27 | } | 
| 28 | |
| 29 | static Method execute_findCalcMethod(Class<?> theClass) {
 | 
| 30 | for (Method method : theClass.getMethods()) | 
| 31 |     if (method.getName().equals("calc"))
 | 
| 32 | return method; | 
| 33 |   throw new RuntimeException("Method 'calc' not found in " + theClass.getName());
 | 
| 34 | } | 
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
| Snippet ID: | #1000590 | 
| Snippet name: | execute function (for !simplejava stuff) | 
| Eternal ID of this version: | #1000590/1 | 
| Text MD5: | 32b9b7dd54711b1c6d7eb112ab9c8360 | 
| Author: | stefan | 
| Category: | javax | 
| Type: | JavaX fragment (include) | 
| Public (visible to everyone): | Yes | 
| Archived (hidden from active list): | No | 
| Created/modified: | 2015-08-15 02:42:49 | 
| Source code size: | 1258 bytes / 34 lines | 
| Pitched / IR pitched: | No / Yes | 
| Views / Downloads: | 864 / 1063 | 
| Referenced in: | [show references] |