1 | // compile JavaX source, load classes & return main class
|
2 | // src can be a snippet ID or actual source code
|
3 |
|
4 | // requires class _javax
|
5 |
|
6 | static Class<?> hotwire(String src) {
|
7 | try {
|
8 | try { _javax.androidContext = get(main.class, "androidContext"); } catch (Exception e) {}
|
9 |
|
10 | List<File> libraries = new ArrayList<File>();
|
11 | File srcDir = _javax.transpileMain(src, libraries);
|
12 |
|
13 | if (_javax.androidContext != null)
|
14 | return _javax.loadx2android(srcDir, src);
|
15 |
|
16 | File classesDir = _javax.TempDirMaker_make();
|
17 | String javacOutput = _javax.compileJava(srcDir, libraries, classesDir);
|
18 | System.out.println(javacOutput);
|
19 |
|
20 | URL[] urls = new URL[libraries.size()+1];
|
21 | urls[0] = classesDir.toURI().toURL();
|
22 | for (int i = 0; i < libraries.size(); i++)
|
23 | urls[i+1] = libraries.get(i).toURI().toURL();
|
24 |
|
25 | // make class loader
|
26 | URLClassLoader classLoader = new URLClassLoader(urls);
|
27 |
|
28 | // load & return main class
|
29 | Class<?> theClass = classLoader.loadClass("main");
|
30 |
|
31 | if (isSnippetID(src))
|
32 | try {
|
33 | set(theClass, "programID", src);
|
34 | } catch (Throwable e) {}
|
35 |
|
36 | return theClass;
|
37 | } catch (Exception e) {
|
38 | throw e instanceof RuntimeException ? (RuntimeException) e : new RuntimeException(e);
|
39 | }
|
40 | } |