changeset 17612:4c4391db8060

8155102: (Process) Process.toString could include pid, isAlive, exitStatus Reviewed-by: rriggs Contributed-by: andrey.dyachkov@gmail.com
author rriggs
date Fri, 02 Sep 2016 12:30:46 -0400
parents 3dc9d5deab5d
children 594b8b4fef69 08d703b88378
files src/java.base/unix/classes/java/lang/ProcessImpl.java src/java.base/windows/classes/java/lang/ProcessImpl.java test/java/lang/ProcessBuilder/Basic.java
diffstat 3 files changed, 54 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/unix/classes/java/lang/ProcessImpl.java	Thu Sep 01 10:35:38 2016 +0530
+++ b/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/src/java.base/windows/classes/java/lang/ProcessImpl.java	Thu Sep 01 10:35:38 2016 +0530
+++ b/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/test/java/lang/ProcessBuilder/Basic.java	Thu Sep 01 10:35:38 2016 +0530
+++ b/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); }
 
         //----------------------------------------------------------------