package com.example.janinodemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * http://janino-compiler.github.io/janino/ * 需要再深入janino * 将web界面的输入与后台处理做得更好 */ @SpringBootApplication public class JaninoDemoApplication { public static void main(String[] args) { SpringApplication.run(JaninoDemoApplication.class, args); } }
package com.example.janinodemo; import org.codehaus.commons.compiler.CompileException; import org.codehaus.janino.ScriptEvaluator; import java.lang.reflect.InvocationTargetException; public class JaninoEEMethodTest { public static void main(String[] args) { ScriptEvaluator se = new ScriptEvaluator(); try { se.cook( "" +"static void method1(){\n" +" System.out.println(1);\n" +"}\n" +"method1();\n" +"method2();\n" +"\n" +"static void method2() {\n" +" System.out.println(2);\n" +"}\n" ); //求值 se.evaluate(new Object[0]); } catch (CompileException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } finally { } } }
package com.example.janinodemo; import org.codehaus.janino.ExpressionEvaluator; /** * @author ukyo * 测试使用janino * 老师推荐 */ public class JaninoEETest { public static void main(String[] args) { //Now here's where the story begins... try { ExpressionEvaluator ee = new ExpressionEvaluator(); // ee.cook("3 + 4"); // System.out.println(ee.evaluate(null)); //The expression will have two "int" parameters:"a" and "b" ee.setParameters(new String[]{"a","b"},new Class[]{int.class,int.class}); //And the expression(i.e. "result")type is also "int". ee.setExpressionType(int.class); //And now we "cook"(scan,parse,compile and load)the fabulous expression. ee.cook("a+b"); //Eventually we evaluate the expression - and that goes super-fast. int result = (Integer) ee.evaluate(new Object[]{19,23}); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } finally { } } }
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>edit codes</title> <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <style> textarea::-webkit-input-placeholder { /* WebKit browsers */ color:orangered; } textarea:-moz-placeholder { /* Mozilla Firefox 4 to 18 */ color: orangered; } textarea::-moz-placeholder { /* Mozilla Firefox 19+ */ color: orangered; } textarea::-ms-input-placeholder { /* Internet Explorer 10+ */ color: orangered; } </style> </head> <body> <textarea rows="30" cols="130" id="textarea1" placeholder="please code here~~" style="background-color: blue;color: snow"> public static void main(){ } main(); </textarea> <textarea rows="30" cols="50" id="textarea2" placeholder="" style="background-color: black;color: snow"> public static boolean testEquals(int[] arr1,int[] arr2){ if(arr1.length!=arr2.length){ return false; } int arrLenth = arr1.length; while(arrLenth!=0){ if(arr1[arrLenth -1]!=arr2[arrLenth -1]){ return false; } arrLenth --; } return true; } </textarea> <br> <button id="submitBtn">提交运行</button> <br> <h3>运行结果:</h3> <div style="background-color: lightsalmon"> <span id="cookResult"></span> </div> </body> <script> $("#submitBtn").click(cook); function cook(){ $.ajax({ url:'/edit/cookCodes', type:"POST", data:{"codes":$("#textarea1").val(),"codes2":$("#textarea2").val()}, // data:{"codes":$("#textarea1").val()}, dataType:"json", success:function(data){ if(data.success){ $("#cookResult").html(data.print) }else{ $("#cookResult").html(""); } } }) } </script> </html>
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>index</title> </head> <body> <h1>code online</h1> index <a href="/edit">code edit</a> </body> </html>
<!-- https://mvnrepository.com/artifact/org.codehaus.janino/janino --> <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>janino</artifactId> <version>3.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.codehaus.janino/commons-compiler --> <dependency> <groupId>org.codehaus.janino</groupId> <artifactId>commons-compiler</artifactId> <version>3.1.2</version> </dependency>