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

!636

!700 // ctex
!1000346 // "case" as a variable name
!quicknew
!standard functions

!include #2000470 // class _x16

main {
  psvm {
    Class solver = compileAndLoadMainClass("#695");
    Object case = call(solver, "parse", "#1000386");
    call(solver, "calculate", case);
    String in = "5*5";
    String result = (String) call(case, "processIn", in);
    System.out.println(in);
    System.out.println(result);
  }
  
  
  static Object call(Class c, String method, Object... args) ctex {
    Method m = findStaticMethod(c, method, args);
    m.setAccessible(true);
    return m.invoke(null, args);
  }
  
  static Object call(Object o, String method, Object... args) ctex {
    Method m = findMethod(o, method, args);
    m.setAccessible(true);
    return m.invoke(o, args);
  }
  
  static Method findMethod(Object o, String method, Object[] args) {
    // TODO: go to superclasses too
    for (Method m : o.getClass().getDeclaredMethods())
      if (m.getName().equals(method) && (m.getModifiers() & Modifier.STATIC) == 0 && m.getParameterTypes().length == args.length) // TODO: multicast
        return m;
    throw new RuntimeException("Method '" + method + "' with " + args.length + " parameter(s) not found in " + o.getClass().getName());
  }
  
  static Method findStaticMethod(Class<?> c, String method, Object[] args) {
    for (Method m : c.getDeclaredMethods())
      if (m.getName().equals(method) && (m.getModifiers() & Modifier.STATIC) != 0 && m.getParameterTypes().length == args.length) // TODO: multicast
        return m;
    throw new RuntimeException("Static method '" + method + "' with " + args.length + " parameter(s) not found in " + c.getName());
  }
  
  // compile JavaX source and load main class
  static Class<?> compileAndLoadMainClass(String src) tex {
    if (isSnippetID(src)) src = loadSnippet(src);
    File srcDir = _x16.TempDirMaker_make();
    File classesDir = _x16.TempDirMaker_make();
    _x16.saveTextFile(new File(srcDir, "main.java").getPath(), src);
    new List<File> libraries;
    File transpiledDir = _x16.topLevelTranslate(srcDir, libraries);
    String javacOutput = _x16.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;
  }
}

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: 890 / 788
Referenced in: [show references]