Wed, 07 Oct 2015 10:01:40 -0700
Merge
aoqi@0 | 1 | #// Usage: jjs -scripting javashell.js |
aoqi@0 | 2 | |
aoqi@0 | 3 | /* |
aoqi@0 | 4 | * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. |
attila@962 | 5 | * |
aoqi@0 | 6 | * Redistribution and use in source and binary forms, with or without |
aoqi@0 | 7 | * modification, are permitted provided that the following conditions |
aoqi@0 | 8 | * are met: |
attila@962 | 9 | * |
aoqi@0 | 10 | * - Redistributions of source code must retain the above copyright |
aoqi@0 | 11 | * notice, this list of conditions and the following disclaimer. |
attila@962 | 12 | * |
aoqi@0 | 13 | * - Redistributions in binary form must reproduce the above copyright |
aoqi@0 | 14 | * notice, this list of conditions and the following disclaimer in the |
aoqi@0 | 15 | * documentation and/or other materials provided with the distribution. |
attila@962 | 16 | * |
aoqi@0 | 17 | * - Neither the name of Oracle nor the names of its |
aoqi@0 | 18 | * contributors may be used to endorse or promote products derived |
aoqi@0 | 19 | * from this software without specific prior written permission. |
attila@962 | 20 | * |
aoqi@0 | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
aoqi@0 | 22 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
aoqi@0 | 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
aoqi@0 | 25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
aoqi@0 | 26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
aoqi@0 | 27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
aoqi@0 | 28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
aoqi@0 | 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
aoqi@0 | 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
aoqi@0 | 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
aoqi@0 | 32 | */ |
aoqi@0 | 33 | |
aoqi@0 | 34 | // Simple Java "shell" with which you can try out |
aoqi@0 | 35 | // your few liner Java code leaving imports, main etc. |
aoqi@0 | 36 | // And you can leave even compilation as this script |
aoqi@0 | 37 | // takes care boilerplate+compile step for you. |
aoqi@0 | 38 | |
aoqi@0 | 39 | // Java types used |
aoqi@0 | 40 | var Arrays = Java.type("java.util.Arrays"); |
aoqi@0 | 41 | var BufferedReader = Java.type("java.io.BufferedReader"); |
aoqi@0 | 42 | var FileWriter = Java.type("java.io.FileWriter"); |
aoqi@0 | 43 | var LocalDateTime = Java.type("java.time.LocalDateTime"); |
aoqi@0 | 44 | var InputStreamReader = Java.type("java.io.InputStreamReader"); |
aoqi@0 | 45 | var PrintWriter = Java.type("java.io.PrintWriter"); |
aoqi@0 | 46 | var ProcessBuilder = Java.type("java.lang.ProcessBuilder"); |
aoqi@0 | 47 | var System = Java.type("java.lang.System"); |
aoqi@0 | 48 | |
aoqi@0 | 49 | // read multiple lines of input from stdin till user |
aoqi@0 | 50 | // enters an empty line |
aoqi@0 | 51 | function input(endMarker, prompt) { |
aoqi@0 | 52 | if (!endMarker) { |
aoqi@0 | 53 | endMarker = ""; |
aoqi@0 | 54 | } |
aoqi@0 | 55 | |
aoqi@0 | 56 | if (!prompt) { |
aoqi@0 | 57 | prompt = " >> "; |
aoqi@0 | 58 | } |
aoqi@0 | 59 | |
aoqi@0 | 60 | var str = ""; |
aoqi@0 | 61 | var reader = new BufferedReader(new InputStreamReader(System.in)); |
aoqi@0 | 62 | var line; |
aoqi@0 | 63 | while (true) { |
aoqi@0 | 64 | System.out.print(prompt); |
aoqi@0 | 65 | line = reader.readLine(); |
aoqi@0 | 66 | if (line == null || line == endMarker) { |
aoqi@0 | 67 | break; |
aoqi@0 | 68 | } |
aoqi@0 | 69 | str += line + "\n"; |
aoqi@0 | 70 | } |
aoqi@0 | 71 | return str; |
aoqi@0 | 72 | } |
aoqi@0 | 73 | |
aoqi@0 | 74 | // write the string to the given file |
aoqi@0 | 75 | function writeTo(file, str) { |
aoqi@0 | 76 | var w = new PrintWriter(new FileWriter(file)); |
aoqi@0 | 77 | try { |
aoqi@0 | 78 | w.print(str); |
aoqi@0 | 79 | } finally { |
aoqi@0 | 80 | w.close(); |
aoqi@0 | 81 | } |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | // generate Java code with user's input |
aoqi@0 | 85 | // put inside generated main method |
aoqi@0 | 86 | function generate(className) { |
aoqi@0 | 87 | var usercode = input(); |
aoqi@0 | 88 | if (usercode == "") { |
aoqi@0 | 89 | return false; |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | var fullcode = <<EOF |
aoqi@0 | 93 | // userful imports, add more here if you want |
aoqi@0 | 94 | // more imports. |
aoqi@0 | 95 | import static java.lang.System.*; |
aoqi@0 | 96 | import java.io.*; |
aoqi@0 | 97 | import java.net.*; |
aoqi@0 | 98 | import java.math.*; |
aoqi@0 | 99 | import java.nio.file.*; |
aoqi@0 | 100 | import java.time.*; |
aoqi@0 | 101 | import java.time.chrono.*; |
aoqi@0 | 102 | import java.time.format.*; |
aoqi@0 | 103 | import java.time.temporal.*; |
aoqi@0 | 104 | import java.time.zone.*; |
aoqi@0 | 105 | import java.util.*; |
aoqi@0 | 106 | import java.util.concurrent.*; |
aoqi@0 | 107 | import java.util.function.*; |
aoqi@0 | 108 | import java.util.stream.*; |
aoqi@0 | 109 | |
aoqi@0 | 110 | public class ${className} { |
aoqi@0 | 111 | public static void main(String[] args) throws Exception { |
aoqi@0 | 112 | ${usercode} |
aoqi@0 | 113 | } |
aoqi@0 | 114 | } |
aoqi@0 | 115 | EOF |
aoqi@0 | 116 | |
aoqi@0 | 117 | writeTo("${className}.java", fullcode); |
aoqi@0 | 118 | return true; |
aoqi@0 | 119 | } |
aoqi@0 | 120 | |
aoqi@0 | 121 | // execute code command |
aoqi@0 | 122 | function exec(args) { |
aoqi@0 | 123 | // build child process and start it! |
attila@1239 | 124 | new ProcessBuilder(Arrays.asList(args.split(' '))) |
aoqi@0 | 125 | .inheritIO() |
aoqi@0 | 126 | .start() |
aoqi@0 | 127 | .waitFor(); |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | // generate unique name |
aoqi@0 | 131 | function uniqueName() { |
aoqi@0 | 132 | var now = LocalDateTime.now().toString(); |
attila@962 | 133 | // replace unsafe chars with '_' |
aoqi@0 | 134 | return "JavaShell" + now.replace(/-|:|\./g, '_'); |
aoqi@0 | 135 | } |
aoqi@0 | 136 | |
aoqi@0 | 137 | // read-compile-run loop |
aoqi@0 | 138 | while(true) { |
aoqi@0 | 139 | var className = uniqueName(); |
aoqi@0 | 140 | if (generate(className)) { |
aoqi@0 | 141 | exec("javac ${className}.java"); |
aoqi@0 | 142 | exec("java ${className}"); |
aoqi@0 | 143 | } else { |
aoqi@0 | 144 | break; |
aoqi@0 | 145 | } |
aoqi@0 | 146 | } |