langtools/src/jdk.jshell/share/classes/jdk/internal/jshell/tool/JShellTool.java
changeset 43757 7193f6ef25db
parent 43564 4287765963d5
child 43759 61535ac55add
equal deleted inserted replaced
43756:118221d3960d 43757:7193f6ef25db
   810         } else {
   810         } else {
   811             return s;
   811             return s;
   812         }
   812         }
   813     }
   813     }
   814 
   814 
       
   815     /**
       
   816      * The entry point into the JShell tool.
       
   817      *
       
   818      * @param args the command-line arguments
       
   819      * @throws Exception catastrophic fatal exception
       
   820      */
   815     public void start(String[] args) throws Exception {
   821     public void start(String[] args) throws Exception {
   816         OptionParserCommandLine commandLineArgs = new OptionParserCommandLine();
   822         OptionParserCommandLine commandLineArgs = new OptionParserCommandLine();
   817         options = commandLineArgs.parse(args);
   823         options = commandLineArgs.parse(args);
   818         if (options == null) {
   824         if (options == null) {
   819             // Abort
   825             // Abort
   840             // if we haven't died, and the feedback mode wants fluff, print welcome
   846             // if we haven't died, and the feedback mode wants fluff, print welcome
   841             if (feedback.shouldDisplayCommandFluff()) {
   847             if (feedback.shouldDisplayCommandFluff()) {
   842                 hardmsg("jshell.msg.welcome", version());
   848                 hardmsg("jshell.msg.welcome", version());
   843             }
   849             }
   844             // Be sure history is always saved so that user code isn't lost
   850             // Be sure history is always saved so that user code isn't lost
   845             Runtime.getRuntime().addShutdownHook(new Thread() {
   851             Thread shutdownHook = new Thread() {
   846                 @Override
   852                 @Override
   847                 public void run() {
   853                 public void run() {
   848                     replayableHistory.storeHistory(prefs);
   854                     replayableHistory.storeHistory(prefs);
   849                 }
   855                 }
   850             });
   856             };
       
   857             Runtime.getRuntime().addShutdownHook(shutdownHook);
   851             // execute from user input
   858             // execute from user input
   852             try (IOContext in = new ConsoleIOContext(this, cmdin, console)) {
   859             try (IOContext in = new ConsoleIOContext(this, cmdin, console)) {
   853                 start(in);
   860                 while (regenerateOnDeath) {
   854             }
   861                     if (!live) {
   855         }
   862                         resetState();
   856     }
   863                     }
   857 
   864                     run(in);
   858     private void start(IOContext in) {
   865                 }
   859         try {
   866             } finally {
   860             while (regenerateOnDeath) {
   867                 replayableHistory.storeHistory(prefs);
   861                 if (!live) {
   868                 closeState();
   862                     resetState();
   869                 try {
   863                 }
   870                     Runtime.getRuntime().removeShutdownHook(shutdownHook);
   864                 run(in);
   871                 } catch (Exception ex) {
   865             }
   872                     // ignore, this probably caused by VM aready being shutdown
   866         } finally {
   873                     // and this is the last act anyhow
   867             closeState();
   874                 }
   868         }
   875             }
       
   876         }
       
   877         closeState();
   869     }
   878     }
   870 
   879 
   871     private EditorSetting configEditor() {
   880     private EditorSetting configEditor() {
   872         // Read retained editor setting (if any)
   881         // Read retained editor setting (if any)
   873         editor = EditorSetting.fromPrefs(prefs);
   882         editor = EditorSetting.fromPrefs(prefs);
  1017 
  1026 
  1018     private void closeState() {
  1027     private void closeState() {
  1019         live = false;
  1028         live = false;
  1020         JShell oldState = state;
  1029         JShell oldState = state;
  1021         if (oldState != null) {
  1030         if (oldState != null) {
       
  1031             state = null;
       
  1032             analysis = null;
  1022             oldState.unsubscribe(shutdownSubscription); // No notification
  1033             oldState.unsubscribe(shutdownSubscription); // No notification
  1023             oldState.close();
  1034             oldState.close();
  1024         }
  1035         }
  1025     }
  1036     }
  1026 
  1037 
  2004     }
  2015     }
  2005 
  2016 
  2006     private boolean cmdExit() {
  2017     private boolean cmdExit() {
  2007         regenerateOnDeath = false;
  2018         regenerateOnDeath = false;
  2008         live = false;
  2019         live = false;
  2009         replayableHistory.storeHistory(prefs);
       
  2010         fluffmsg("jshell.msg.goodbye");
  2020         fluffmsg("jshell.msg.goodbye");
  2011         return true;
  2021         return true;
  2012     }
  2022     }
  2013 
  2023 
  2014     boolean cmdHelp(String arg) {
  2024     boolean cmdHelp(String arg) {