// requires class _javax and ctex static String execute(String src) ctex { Class<?> mainClass = execute_compileAndLoadMainClass(src); Method m = execute_findCalcMethod(mainClass); return String.valueOf(m.invoke(null)); } // compile JavaX source and load main class static Class<?> execute_compileAndLoadMainClass(String src) tex { File srcDir = _javax.TempDirMaker_make(); File classesDir = _javax.TempDirMaker_make(); _javax.saveTextFile(new File(srcDir, "main.java").getPath(), src); new List<File> libraries; _javax.safeTranslate = true; File transpiledDir = _javax.topLevelTranslate(srcDir, libraries); String javacOutput = _javax.compileJava(transpiledDir, libraries, classesDir); System.out.println(javacOutput); URL[] urls = {classesDir.toURI().toURL()}; // make class loader URLClassLoader classLoader = new URLClassLoader(urls); // load main class Class<?> mainClass = classLoader.loadClass("main"); return mainClass; } static Method execute_findCalcMethod(Class<?> theClass) { for (Method method : theClass.getMethods()) if (method.getName().equals("calc")) return method; throw new RuntimeException("Method 'calc' not found in " + theClass.getName()); }
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: | 640 / 836 |
Referenced in: | #1000589 - Run expander (stand-alone test) #1000598 - isSafeCompilable function - checks if code is JavaX-compilable to bytecode using only safe translators #3000382 - Answer for ferdie (>> t = 1, f = 0) |