1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/samples/javashell.js Wed Apr 27 01:36:41 2016 +0800 1.3 @@ -0,0 +1,146 @@ 1.4 +#// Usage: jjs -scripting javashell.js 1.5 + 1.6 +/* 1.7 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions 1.11 + * are met: 1.12 + * 1.13 + * - Redistributions of source code must retain the above copyright 1.14 + * notice, this list of conditions and the following disclaimer. 1.15 + * 1.16 + * - Redistributions in binary form must reproduce the above copyright 1.17 + * notice, this list of conditions and the following disclaimer in the 1.18 + * documentation and/or other materials provided with the distribution. 1.19 + * 1.20 + * - Neither the name of Oracle nor the names of its 1.21 + * contributors may be used to endorse or promote products derived 1.22 + * from this software without specific prior written permission. 1.23 + * 1.24 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.25 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1.26 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.27 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.28 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.29 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.30 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.31 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.32 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.33 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.34 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.35 + */ 1.36 + 1.37 +// Simple Java "shell" with which you can try out 1.38 +// your few liner Java code leaving imports, main etc. 1.39 +// And you can leave even compilation as this script 1.40 +// takes care boilerplate+compile step for you. 1.41 + 1.42 +// Java types used 1.43 +var Arrays = Java.type("java.util.Arrays"); 1.44 +var BufferedReader = Java.type("java.io.BufferedReader"); 1.45 +var FileWriter = Java.type("java.io.FileWriter"); 1.46 +var LocalDateTime = Java.type("java.time.LocalDateTime"); 1.47 +var InputStreamReader = Java.type("java.io.InputStreamReader"); 1.48 +var PrintWriter = Java.type("java.io.PrintWriter"); 1.49 +var ProcessBuilder = Java.type("java.lang.ProcessBuilder"); 1.50 +var System = Java.type("java.lang.System"); 1.51 + 1.52 +// read multiple lines of input from stdin till user 1.53 +// enters an empty line 1.54 +function input(endMarker, prompt) { 1.55 + if (!endMarker) { 1.56 + endMarker = ""; 1.57 + } 1.58 + 1.59 + if (!prompt) { 1.60 + prompt = " >> "; 1.61 + } 1.62 + 1.63 + var str = ""; 1.64 + var reader = new BufferedReader(new InputStreamReader(System.in)); 1.65 + var line; 1.66 + while (true) { 1.67 + System.out.print(prompt); 1.68 + line = reader.readLine(); 1.69 + if (line == null || line == endMarker) { 1.70 + break; 1.71 + } 1.72 + str += line + "\n"; 1.73 + } 1.74 + return str; 1.75 +} 1.76 + 1.77 +// write the string to the given file 1.78 +function writeTo(file, str) { 1.79 + var w = new PrintWriter(new FileWriter(file)); 1.80 + try { 1.81 + w.print(str); 1.82 + } finally { 1.83 + w.close(); 1.84 + } 1.85 +} 1.86 + 1.87 +// generate Java code with user's input 1.88 +// put inside generated main method 1.89 +function generate(className) { 1.90 + var usercode = input(); 1.91 + if (usercode == "") { 1.92 + return false; 1.93 + } 1.94 + 1.95 + var fullcode = <<EOF 1.96 +// userful imports, add more here if you want 1.97 +// more imports. 1.98 +import static java.lang.System.*; 1.99 +import java.io.*; 1.100 +import java.net.*; 1.101 +import java.math.*; 1.102 +import java.nio.file.*; 1.103 +import java.time.*; 1.104 +import java.time.chrono.*; 1.105 +import java.time.format.*; 1.106 +import java.time.temporal.*; 1.107 +import java.time.zone.*; 1.108 +import java.util.*; 1.109 +import java.util.concurrent.*; 1.110 +import java.util.function.*; 1.111 +import java.util.stream.*; 1.112 + 1.113 +public class ${className} { 1.114 + public static void main(String[] args) throws Exception { 1.115 + ${usercode} 1.116 + } 1.117 +} 1.118 +EOF 1.119 + 1.120 + writeTo("${className}.java", fullcode); 1.121 + return true; 1.122 +} 1.123 + 1.124 +// execute code command 1.125 +function exec(args) { 1.126 + // build child process and start it! 1.127 + new ProcessBuilder(Arrays.asList(args.split(' '))) 1.128 + .inheritIO() 1.129 + .start() 1.130 + .waitFor(); 1.131 +} 1.132 + 1.133 +// generate unique name 1.134 +function uniqueName() { 1.135 + var now = LocalDateTime.now().toString(); 1.136 + // replace unsafe chars with '_' 1.137 + return "JavaShell" + now.replace(/-|:|\./g, '_'); 1.138 +} 1.139 + 1.140 +// read-compile-run loop 1.141 +while(true) { 1.142 + var className = uniqueName(); 1.143 + if (generate(className)) { 1.144 + exec("javac ${className}.java"); 1.145 + exec("java ${className}"); 1.146 + } else { 1.147 + break; 1.148 + } 1.149 +}