< prev index next >

src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java

Print this page
rev 3613 : imported patch 8131023


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.jshell.tool;
  27 
  28 import java.io.BufferedWriter;
  29 import java.io.ByteArrayInputStream;
  30 import java.io.File;
  31 import java.io.FileNotFoundException;
  32 import java.io.FileReader;
  33 import java.io.IOException;
  34 import java.io.InputStream;
  35 import java.io.PrintStream;
  36 import java.io.Reader;
  37 import java.io.StringReader;
  38 import java.nio.charset.Charset;
  39 import java.nio.file.AccessDeniedException;
  40 import java.nio.file.FileSystems;
  41 import java.nio.file.Files;
  42 import java.nio.file.NoSuchFileException;
  43 import java.nio.file.Path;
  44 import java.nio.file.Paths;
  45 import java.text.MessageFormat;
  46 import java.util.ArrayList;
  47 import java.util.Arrays;
  48 import java.util.Collections;
  49 import java.util.Iterator;


 136     final Preferences prefs;
 137     final Locale locale;
 138 
 139     final Feedback feedback = new Feedback();
 140 
 141     /**
 142      * The constructor for the tool (used by tool launch via main and by test
 143      * harnesses to capture ins and outs.
 144      * @param cmdin command line input -- snippets and commands
 145      * @param cmdout command line output, feedback including errors
 146      * @param cmderr start-up errors and debugging info
 147      * @param console console control interaction
 148      * @param userin code execution input (not yet functional)
 149      * @param userout code execution output  -- System.out.printf("hi")
 150      * @param usererr code execution error stream  -- System.err.printf("Oops")
 151      * @param prefs preferences to use
 152      * @param locale locale to use
 153      */
 154     public JShellTool(InputStream cmdin, PrintStream cmdout, PrintStream cmderr,
 155             PrintStream console,
 156             InputStream userin, PrintStream userout, PrintStream usererr,
 157             Preferences prefs, Locale locale) {
 158         this.cmdin = cmdin;
 159         this.cmdout = cmdout;
 160         this.cmderr = cmderr;
 161         this.console = console;
 162         this.userin = userin;





 163         this.userout = userout;
 164         this.usererr = usererr;
 165         this.prefs = prefs;
 166         this.locale = locale;
 167     }
 168 
 169     private ResourceBundle versionRB = null;
 170     private ResourceBundle outputRB  = null;
 171 
 172     private IOContext input = null;
 173     private boolean regenerateOnDeath = true;
 174     private boolean live = false;
 175     private boolean feedbackInitialized = false;
 176     private String commandLineFeedbackMode = null;
 177     private List<String> remoteVMOptions = new ArrayList<>();
 178 
 179     SourceCodeAnalysis analysis;
 180     JShell state = null;
 181     Subscription shutdownSubscription = null;
 182 


 433     static String trimEnd(String s) {
 434         int last = s.length() - 1;
 435         int i = last;
 436         while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
 437             --i;
 438         }
 439         if (i != last) {
 440             return s.substring(0, i + 1);
 441         } else {
 442             return s;
 443         }
 444     }
 445 
 446     /**
 447      * Normal start entry point
 448      * @param args
 449      * @throws Exception
 450      */
 451     public static void main(String[] args) throws Exception {
 452         new JShellTool(System.in, System.out, System.err, System.out,
 453                  new ByteArrayInputStream(new byte[0]), System.out, System.err,
 454                  Preferences.userRoot().node("tool/JShell"),
 455                  Locale.getDefault())
 456                 .start(args);
 457     }
 458 
 459     public void start(String[] args) throws Exception {
 460         List<String> loadList = processCommandArgs(args);
 461         if (loadList == null) {
 462             // Abort
 463             return;
 464         }
 465         try (IOContext in = new ConsoleIOContext(this, cmdin, console)) {
 466             start(in, loadList);
 467         }
 468     }
 469 
 470     private void start(IOContext in, List<String> loadList) {
 471         // If startup hasn't been set by command line, set from retained/default
 472         if (startup == null) {
 473             startup = prefs.get(STARTUP_KEY, null);


2549 class ScannerIOContext extends NonInteractiveIOContext {
2550     private final Scanner scannerIn;
2551 
2552     ScannerIOContext(Scanner scannerIn) {
2553         this.scannerIn = scannerIn;
2554     }
2555 
2556     @Override
2557     public String readLine(String prompt, String prefix) {
2558         if (scannerIn.hasNextLine()) {
2559             return scannerIn.nextLine();
2560         } else {
2561             return null;
2562         }
2563     }
2564 
2565     @Override
2566     public void close() {
2567         scannerIn.close();
2568     }





2569 }
2570 
2571 class FileScannerIOContext extends ScannerIOContext {
2572 
2573     FileScannerIOContext(String fn) throws FileNotFoundException {
2574         this(new FileReader(fn));
2575     }
2576 
2577     FileScannerIOContext(Reader rdr) throws FileNotFoundException {
2578         super(new Scanner(rdr));
2579     }
2580 }
2581 
2582 class ReloadIOContext extends NonInteractiveIOContext {
2583     private final Iterator<String> it;
2584     private final PrintStream echoStream;
2585 
2586     ReloadIOContext(Iterable<String> history, PrintStream echoStream) {
2587         this.it = history.iterator();
2588         this.echoStream = echoStream;
2589     }
2590 
2591     @Override
2592     public String readLine(String prompt, String prefix) {
2593         String s = it.hasNext()
2594                 ? it.next()
2595                 : null;
2596         if (echoStream != null && s != null) {
2597             String p = "-: ";
2598             String p2 = "\n   ";
2599             echoStream.printf("%s%s\n", p, s.replace("\n", p2));
2600         }
2601         return s;
2602     }
2603 
2604     @Override
2605     public void close() {





2606     }
2607 }


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.internal.jshell.tool;
  27 
  28 import java.io.BufferedWriter;

  29 import java.io.File;
  30 import java.io.FileNotFoundException;
  31 import java.io.FileReader;
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 import java.io.PrintStream;
  35 import java.io.Reader;
  36 import java.io.StringReader;
  37 import java.nio.charset.Charset;
  38 import java.nio.file.AccessDeniedException;
  39 import java.nio.file.FileSystems;
  40 import java.nio.file.Files;
  41 import java.nio.file.NoSuchFileException;
  42 import java.nio.file.Path;
  43 import java.nio.file.Paths;
  44 import java.text.MessageFormat;
  45 import java.util.ArrayList;
  46 import java.util.Arrays;
  47 import java.util.Collections;
  48 import java.util.Iterator;


 135     final Preferences prefs;
 136     final Locale locale;
 137 
 138     final Feedback feedback = new Feedback();
 139 
 140     /**
 141      * The constructor for the tool (used by tool launch via main and by test
 142      * harnesses to capture ins and outs.
 143      * @param cmdin command line input -- snippets and commands
 144      * @param cmdout command line output, feedback including errors
 145      * @param cmderr start-up errors and debugging info
 146      * @param console console control interaction
 147      * @param userin code execution input (not yet functional)
 148      * @param userout code execution output  -- System.out.printf("hi")
 149      * @param usererr code execution error stream  -- System.err.printf("Oops")
 150      * @param prefs preferences to use
 151      * @param locale locale to use
 152      */
 153     public JShellTool(InputStream cmdin, PrintStream cmdout, PrintStream cmderr,
 154             PrintStream console,
 155             PrintStream userout, PrintStream usererr,
 156             Preferences prefs, Locale locale) {
 157         this.cmdin = cmdin;
 158         this.cmdout = cmdout;
 159         this.cmderr = cmderr;
 160         this.console = console;
 161         this.userin = new InputStream() {
 162             @Override
 163             public int read() throws IOException {
 164                 return input.readUserInput();
 165             }
 166         };
 167         this.userout = userout;
 168         this.usererr = usererr;
 169         this.prefs = prefs;
 170         this.locale = locale;
 171     }
 172 
 173     private ResourceBundle versionRB = null;
 174     private ResourceBundle outputRB  = null;
 175 
 176     private IOContext input = null;
 177     private boolean regenerateOnDeath = true;
 178     private boolean live = false;
 179     private boolean feedbackInitialized = false;
 180     private String commandLineFeedbackMode = null;
 181     private List<String> remoteVMOptions = new ArrayList<>();
 182 
 183     SourceCodeAnalysis analysis;
 184     JShell state = null;
 185     Subscription shutdownSubscription = null;
 186 


 437     static String trimEnd(String s) {
 438         int last = s.length() - 1;
 439         int i = last;
 440         while (i >= 0 && Character.isWhitespace(s.charAt(i))) {
 441             --i;
 442         }
 443         if (i != last) {
 444             return s.substring(0, i + 1);
 445         } else {
 446             return s;
 447         }
 448     }
 449 
 450     /**
 451      * Normal start entry point
 452      * @param args
 453      * @throws Exception
 454      */
 455     public static void main(String[] args) throws Exception {
 456         new JShellTool(System.in, System.out, System.err, System.out,
 457                  System.out, System.err,
 458                  Preferences.userRoot().node("tool/JShell"),
 459                  Locale.getDefault())
 460                 .start(args);
 461     }
 462 
 463     public void start(String[] args) throws Exception {
 464         List<String> loadList = processCommandArgs(args);
 465         if (loadList == null) {
 466             // Abort
 467             return;
 468         }
 469         try (IOContext in = new ConsoleIOContext(this, cmdin, console)) {
 470             start(in, loadList);
 471         }
 472     }
 473 
 474     private void start(IOContext in, List<String> loadList) {
 475         // If startup hasn't been set by command line, set from retained/default
 476         if (startup == null) {
 477             startup = prefs.get(STARTUP_KEY, null);


2553 class ScannerIOContext extends NonInteractiveIOContext {
2554     private final Scanner scannerIn;
2555 
2556     ScannerIOContext(Scanner scannerIn) {
2557         this.scannerIn = scannerIn;
2558     }
2559 
2560     @Override
2561     public String readLine(String prompt, String prefix) {
2562         if (scannerIn.hasNextLine()) {
2563             return scannerIn.nextLine();
2564         } else {
2565             return null;
2566         }
2567     }
2568 
2569     @Override
2570     public void close() {
2571         scannerIn.close();
2572     }
2573 
2574     @Override
2575     public int readUserInput() {
2576         return -1;
2577     }
2578 }
2579 
2580 class FileScannerIOContext extends ScannerIOContext {
2581 
2582     FileScannerIOContext(String fn) throws FileNotFoundException {
2583         this(new FileReader(fn));
2584     }
2585 
2586     FileScannerIOContext(Reader rdr) throws FileNotFoundException {
2587         super(new Scanner(rdr));
2588     }
2589 }
2590 
2591 class ReloadIOContext extends NonInteractiveIOContext {
2592     private final Iterator<String> it;
2593     private final PrintStream echoStream;
2594 
2595     ReloadIOContext(Iterable<String> history, PrintStream echoStream) {
2596         this.it = history.iterator();
2597         this.echoStream = echoStream;
2598     }
2599 
2600     @Override
2601     public String readLine(String prompt, String prefix) {
2602         String s = it.hasNext()
2603                 ? it.next()
2604                 : null;
2605         if (echoStream != null && s != null) {
2606             String p = "-: ";
2607             String p2 = "\n   ";
2608             echoStream.printf("%s%s\n", p, s.replace("\n", p2));
2609         }
2610         return s;
2611     }
2612 
2613     @Override
2614     public void close() {
2615     }
2616 
2617     @Override
2618     public int readUserInput() {
2619         return -1;
2620     }
2621 }
< prev index next >