8149665: $EXEC changes clean up
authorjlaskey
Thu, 11 Feb 2016 12:58:57 -0400
changeset 35795 5b9049223c34
parent 35794 049749629dbe
child 35796 8ca6ec05000c
8149665: $EXEC changes clean up Reviewed-by: attila
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CommandExecutor.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CommandExecutor.java	Wed Feb 10 11:18:02 2016 -0400
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/CommandExecutor.java	Thu Feb 11 12:58:57 2016 -0400
@@ -339,7 +339,7 @@
      * @param deflt value to return if not found
      * @return value of the environment variable
      */
-    private String envVarValue(String key, String deflt) {
+    private String envVarValue(final String key, final String deflt) {
         return environment.getOrDefault(key, deflt);
     }
 
@@ -349,7 +349,7 @@
      * @param key name of environment variable
      * @return long value of the environment variable
      */
-    private long envVarLongValue(String key) {
+    private long envVarLongValue(final String key) {
         try {
             return Long.parseLong(envVarValue(key, "0"));
         } catch (NumberFormatException ex) {
@@ -363,7 +363,7 @@
      * @param key name of environment variable
      * @return boolean value of the environment variable
      */
-    private boolean envVarBooleanValue(String key) {
+    private boolean envVarBooleanValue(final String key) {
         return envVarLongValue(key) != 0;
     }
 
@@ -373,7 +373,7 @@
      * @param token token to strip
      * @return stripped token
      */
-    private String stripQuotes(String token) {
+    private static String stripQuotes(String token) {
         if ((token.startsWith("\"") && token.endsWith("\"")) ||
              token.startsWith("\'") && token.endsWith("\'")) {
             token = token.substring(1, token.length() - 1);
@@ -387,7 +387,7 @@
      * @param fileName name of file or directory
      * @return resolved Path to file
      */
-    private static Path resolvePath(String cwd, String fileName) {
+    private static Path resolvePath(final String cwd, final String fileName) {
         return Paths.get(cwd).resolve(fileName).normalize();
     }
 
@@ -403,12 +403,12 @@
             // Set current working directory.
             case "cd":
                 // If zero args then use home dirrectory as cwd else use first arg.
-                String newCWD = cmd.size() < 2 ? HOME_DIRECTORY : cmd.get(1);
+                final String newCWD = cmd.size() < 2 ? HOME_DIRECTORY : cmd.get(1);
                 // Normalize the cwd
-                Path cwdPath = resolvePath(cwd, newCWD);
+                final Path cwdPath = resolvePath(cwd, newCWD);
 
                 // Check if is a directory.
-                File file = cwdPath.toFile();
+                final File file = cwdPath.toFile();
                 if (!file.exists()) {
                     reportError("file.not.exist", file.toString());
                     return true;
@@ -424,8 +424,8 @@
             // Set an environment variable.
             case "setenv":
                 if (3 <= cmd.size()) {
-                    String key = cmd.get(1);
-                    String value = cmd.get(2);
+                    final String key = cmd.get(1);
+                    final String value = cmd.get(2);
                     environment.put(key, value);
                 }
 
@@ -434,7 +434,7 @@
             // Unset an environment variable.
             case "unsetenv":
                 if (2 <= cmd.size()) {
-                    String key = cmd.get(1);
+                    final String key = cmd.get(1);
                     environment.remove(key);
                 }
 
@@ -454,10 +454,10 @@
     private List<String>  preprocessCommand(final List<String> tokens,
             final String cwd, final RedirectInfo redirectInfo) {
         // Tokens remaining for actual command.
-        List<String> command = new ArrayList<>();
+        final List<String> command = new ArrayList<>();
 
         // iterate through all tokens.
-        Iterator<String> iterator = tokens.iterator();
+        final Iterator<String> iterator = tokens.iterator();
         while (iterator.hasNext()) {
             String token = iterator.next();
 
@@ -483,12 +483,12 @@
     private void createProcessBuilder(final List<String> command,
             final String cwd, final RedirectInfo redirectInfo) {
         // Create new ProcessBuilder.
-        ProcessBuilder pb = new ProcessBuilder(command);
+        final ProcessBuilder pb = new ProcessBuilder(command);
         // Set current working directory.
         pb.directory(new File(cwd));
 
         // Map environment variables.
-        Map<String, String> processEnvironment = pb.environment();
+        final Map<String, String> processEnvironment = pb.environment();
         processEnvironment.clear();
         processEnvironment.putAll(environment);
 
@@ -510,10 +510,10 @@
         }
 
         // Get the current working directory.
-        String cwd = envVarValue("PWD", HOME_DIRECTORY);
+        final String cwd = envVarValue("PWD", HOME_DIRECTORY);
         // Preprocess the command for redirects.
-        RedirectInfo redirectInfo = new RedirectInfo();
-        List<String> command = preprocessCommand(tokens, cwd, redirectInfo);
+        final RedirectInfo redirectInfo = new RedirectInfo();
+        final List<String> command = preprocessCommand(tokens, cwd, redirectInfo);
 
         // Skip if empty or a built in.
         if (command.isEmpty() || builtIn(command, cwd)) {
@@ -529,8 +529,8 @@
         }
 
         // Fetch first and last ProcessBuilder.
-        ProcessBuilder firstProcessBuilder = processBuilders.get(0);
-        ProcessBuilder lastProcessBuilder = processBuilders.get(processBuilders.size() - 1);
+        final ProcessBuilder firstProcessBuilder = processBuilders.get(0);
+        final ProcessBuilder lastProcessBuilder = processBuilders.get(processBuilders.size() - 1);
 
         // Determine which streams have not be redirected from pipes.
         boolean inputIsPipe = firstProcessBuilder.redirectInput() == Redirect.PIPE;
@@ -560,7 +560,7 @@
         }
 
         // Start the processes.
-        List<Process> processes = new ArrayList<>();
+        final List<Process> processes = new ArrayList<>();
         for (ProcessBuilder pb : processBuilders) {
             try {
                 processes.add(pb.start());
@@ -574,8 +574,8 @@
         processBuilders.clear();
 
         // Get first and last process.
-        Process firstProcess = processes.get(0);
-        Process lastProcess = processes.get(processes.size() - 1);
+        final Process firstProcess = processes.get(0);
+        final Process lastProcess = processes.get(processes.size() - 1);
 
         // Prepare for string based i/o if no redirection or provided streams.
         ByteArrayOutputStream byteOutputStream = null;
@@ -589,7 +589,6 @@
                 new Piper(inputStream, firstProcess.getOutputStream()).start();
             } else {
                 // Otherwise assume an input string has been provided.
-                ByteArrayInputStream byteInputStream = new ByteArrayInputStream(inputString.getBytes());
                 new Piper(new ByteArrayInputStream(inputString.getBytes()), firstProcess.getOutputStream()).start();
             }
         }
@@ -621,8 +620,8 @@
 
         // Pipe commands in between.
         for (int i = 0, n = processes.size() - 1; i < n; i++) {
-            Process prev = processes.get(i);
-            Process next = processes.get(i + 1);
+            final Process prev = processes.get(i);
+            final Process next = processes.get(i + 1);
             new Piper(prev.getInputStream(), next.getOutputStream()).start();
         }
 
@@ -671,7 +670,7 @@
      * @param script command script to parsed
      * @return StreamTokenizer for command script
      */
-    private StreamTokenizer createTokenizer(final String script) {
+    private static StreamTokenizer createTokenizer(final String script) {
         final StreamTokenizer tokenizer = new StreamTokenizer(new StringReader(script));
         tokenizer.resetSyntax();
         // Default all characters to word.
@@ -702,9 +701,9 @@
         final StreamTokenizer tokenizer = createTokenizer(script);
 
         // Prepare to accumulate command tokens.
-        List<String> command = new ArrayList<>();
+        final List<String> command = new ArrayList<>();
         // Prepare to acumulate partial tokens joined with "\ ".
-        StringBuilder sb = new StringBuilder();
+        final StringBuilder sb = new StringBuilder();
 
         try {
             // Fetch next token until end of script.
@@ -768,12 +767,12 @@
      * process - process a command array of strings
      * @param script command script to be processed
      */
-    void process(List<String> tokens) {
+    void process(final List<String> tokens) {
         // Prepare to accumulate command tokens.
-        List<String> command = new ArrayList<>();
+        final List<String> command = new ArrayList<>();
 
         // Iterate through tokens.
-        Iterator<String> iterator = tokens.iterator();
+        final Iterator<String> iterator = tokens.iterator();
         while (iterator.hasNext() && exitCode == EXIT_SUCCESS) {
             // Next word token.
             String token = iterator.next();
@@ -806,7 +805,7 @@
         command(command, false);
     }
 
-    void reportError(String msg, String object) {
+    void reportError(final String msg, final String object) {
         errorString += ECMAErrors.getMessage("range.error.exec." + msg, object);
         exitCode = EXIT_FAILURE;
     }
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Wed Feb 10 11:18:02 2016 -0400
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/runtime/ScriptingFunctions.java	Thu Feb 11 12:58:57 2016 -0400
@@ -136,11 +136,8 @@
      *               4. Array scriptTokens, InputStream input, OutputStream output, OutputStream error
      *
      * @return output string from the request if in form of 1. or 3., empty string otherwise
-     *
-     * @throws IOException           if any stream access fails
-     * @throws InterruptedException  if execution is interrupted
      */
-    public static Object exec(final Object self, final Object... args) throws IOException, InterruptedException {
+    public static Object exec(final Object self, final Object... args) {
         final Object arg0 = args.length > 0 ? args[0] : UNDEFINED;
         final Object arg1 = args.length > 1 ? args[1] : UNDEFINED;
         final Object arg2 = args.length > 2 ? args[2] : UNDEFINED;
@@ -179,7 +176,7 @@
         final ScriptObject global = Context.getGlobal();
 
         // Capture ENV property state.
-        Map<String, String> environment = new HashMap<>();
+        final Map<String, String> environment = new HashMap<>();
         final Object env = global.get(ENV_NAME);
 
         if (env instanceof ScriptObject) {