8155102: (Process) Process.toString could include pid, isAlive, exitStatus
authorrriggs
Fri, 02 Sep 2016 12:30:46 -0400
changeset 40738 86952e676976
parent 40737 9b5eee5d7a26
child 40739 bdff9be6c98f
child 40818 9c2079cba606
8155102: (Process) Process.toString could include pid, isAlive, exitStatus Reviewed-by: rriggs Contributed-by: andrey.dyachkov@gmail.com
jdk/src/java.base/unix/classes/java/lang/ProcessImpl.java
jdk/src/java.base/windows/classes/java/lang/ProcessImpl.java
jdk/test/java/lang/ProcessBuilder/Basic.java
--- a/jdk/src/java.base/unix/classes/java/lang/ProcessImpl.java	Thu Sep 01 10:35:38 2016 +0530
+++ b/jdk/src/java.base/unix/classes/java/lang/ProcessImpl.java	Fri Sep 02 12:30:46 2016 -0400
@@ -630,6 +630,19 @@
         return !hasExited;
     }
 
+    /**
+     * The {@code toString} method returns a string consisting of
+     * the native process ID of the process and the exit value of the process.
+     *
+     * @return a string representation of the object.
+     */
+    @Override
+    public String toString() {
+        return new StringBuilder("Process[pid=").append(pid)
+                .append(", exitValue=").append(hasExited ? exitcode : "\"not exited\"")
+                .append("]").toString();
+    }
+
     private static native void init();
 
     static {
--- a/jdk/src/java.base/windows/classes/java/lang/ProcessImpl.java	Thu Sep 01 10:35:38 2016 +0530
+++ b/jdk/src/java.base/windows/classes/java/lang/ProcessImpl.java	Fri Sep 02 12:30:46 2016 -0400
@@ -564,6 +564,20 @@
     private static native boolean isProcessAlive(long handle);
 
     /**
+     * The {@code toString} method returns a string consisting of
+     * the native process ID of the process and the exit value of the process.
+     *
+     * @return a string representation of the object.
+     */
+    @Override
+    public String toString() {
+        int exitCode = getExitCodeProcess(handle);
+        return new StringBuilder("Process[pid=").append(getPid())
+                .append(", exitValue=").append(exitCode == STILL_ACTIVE ? "\"not exited\"" : exitCode)
+                .append("]").toString();
+    }
+
+    /**
      * Create a process using the win32 function CreateProcess.
      * The method is synchronized due to MS kb315939 problem.
      * All native handles should restore the inherit flag at the end of call.
--- a/jdk/test/java/lang/ProcessBuilder/Basic.java	Thu Sep 01 10:35:38 2016 +0530
+++ b/jdk/test/java/lang/ProcessBuilder/Basic.java	Fri Sep 02 12:30:46 2016 -0400
@@ -2226,6 +2226,33 @@
                     reader.join();
                 }
             }
+
+            //----------------------------------------------------------------
+            // Check the Process toString() method
+            //----------------------------------------------------------------
+            {
+                List<String> childArgs = new ArrayList<String>(javaChildArgs);
+                childArgs.add("testIO");
+                ProcessBuilder pb = new ProcessBuilder(childArgs);
+                pb.redirectInput(Redirect.PIPE);
+                pb.redirectOutput(DISCARD);
+                pb.redirectError(DISCARD);
+                final Process p = pb.start();
+                // Child process waits until it gets input
+                String s = p.toString();
+                check(s.contains("not exited"));
+                check(s.contains("pid=" + p.getPid() + ","));
+
+                new PrintStream(p.getOutputStream()).print("standard input");
+                p.getOutputStream().close();
+
+                // Check the toString after it exits
+                int exitValue = p.waitFor();
+                s = p.toString();
+                check(s.contains("pid=" + p.getPid() + ","));
+                check(s.contains("exitValue=" + exitValue) &&
+                        !s.contains("not exited"));
+            }
         } catch (Throwable t) { unexpected(t); }
 
         //----------------------------------------------------------------