8155102: (Process) Process.toString could include pid, isAlive, exitStatus

Reviewed-by: rriggs
This commit is contained in:
Andrey Dyachkov 2016-09-02 12:30:46 -04:00 committed by Roger Riggs
parent 6906b1ad96
commit 6d3b1a78ad
3 changed files with 54 additions and 0 deletions

View File

@ -630,6 +630,19 @@ final class ProcessImpl extends Process {
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 {

View File

@ -563,6 +563,20 @@ final class ProcessImpl extends Process {
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.

View File

@ -2226,6 +2226,33 @@ public class Basic {
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); }
//----------------------------------------------------------------