1 | !636
|
2 | !700 // ctex
|
3 | !1000346 // "case" as a variable name
|
4 | !quicknew
|
5 | !standard functions
|
6 |
|
7 | !include #2000470 // class _x16
|
8 |
|
9 | main {
|
10 | psvm {
|
11 | Class solver = compileAndLoadMainClass("#695");
|
12 | Object case = call(solver, "parse", "#1000386");
|
13 | call(solver, "calculate", case);
|
14 | String in = "5*5";
|
15 | String result = (String) call(case, "processIn", in);
|
16 | System.out.println(in);
|
17 | System.out.println(result);
|
18 | }
|
19 |
|
20 |
|
21 | static Object call(Class c, String method, Object... args) ctex {
|
22 | Method m = findStaticMethod(c, method, args);
|
23 | m.setAccessible(true);
|
24 | return m.invoke(null, args);
|
25 | }
|
26 |
|
27 | static Object call(Object o, String method, Object... args) ctex {
|
28 | Method m = findMethod(o, method, args);
|
29 | m.setAccessible(true);
|
30 | return m.invoke(o, args);
|
31 | }
|
32 |
|
33 | static Method findMethod(Object o, String method, Object[] args) {
|
34 | // TODO: go to superclasses too
|
35 | for (Method m : o.getClass().getDeclaredMethods())
|
36 | if (m.getName().equals(method) && (m.getModifiers() & Modifier.STATIC) == 0 && m.getParameterTypes().length == args.length) // TODO: multicast
|
37 | return m;
|
38 | throw new RuntimeException("Method '" + method + "' with " + args.length + " parameter(s) not found in " + o.getClass().getName());
|
39 | }
|
40 |
|
41 | static Method findStaticMethod(Class<?> c, String method, Object[] args) {
|
42 | for (Method m : c.getDeclaredMethods())
|
43 | if (m.getName().equals(method) && (m.getModifiers() & Modifier.STATIC) != 0 && m.getParameterTypes().length == args.length) // TODO: multicast
|
44 | return m;
|
45 | throw new RuntimeException("Static method '" + method + "' with " + args.length + " parameter(s) not found in " + c.getName());
|
46 | }
|
47 |
|
48 | // compile JavaX source and load main class
|
49 | static Class<?> compileAndLoadMainClass(String src) tex {
|
50 | if (isSnippetID(src)) src = loadSnippet(src);
|
51 | File srcDir = _x16.TempDirMaker_make();
|
52 | File classesDir = _x16.TempDirMaker_make();
|
53 | _x16.saveTextFile(new File(srcDir, "main.java").getPath(), src);
|
54 | new List<File> libraries;
|
55 | File transpiledDir = _x16.topLevelTranslate(srcDir, libraries);
|
56 | String javacOutput = _x16.compileJava(transpiledDir, libraries, classesDir);
|
57 | System.out.println(javacOutput);
|
58 | URL[] urls = {classesDir.toURI().toURL()};
|
59 |
|
60 | // make class loader
|
61 | URLClassLoader classLoader = new URLClassLoader(urls);
|
62 |
|
63 | // load main class
|
64 | Class<?> mainClass = classLoader.loadClass("main");
|
65 | return mainClass;
|
66 | }
|
67 | } |