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

67
LINES

< > BotCompany Repo | #702 // Wiring runner example

JavaX source code - run with: x30.jar

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  
}

download  show line numbers  debug dex  old transpilations   

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

Comments [hide]

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

add comment

Snippet ID: #702
Snippet name: Wiring runner example
Eternal ID of this version: #702/1
Text MD5: 2fd3cbded69b9cefe1114ab09bd4c5cc
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-02 02:14:05
Source code size: 2530 bytes / 67 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 809 / 704
Referenced in: [show references]