[infer][java] simplify the process of java.lang.Process

Summary: This is to prevent test failures to happen whenchanging the code a little.

Reviewed By: sblackshear

Differential Revision: D5815349

fbshipit-source-id: 8516102
master
Jeremy Dubreil 7 years ago committed by Facebook Github Bot
parent 5f176f40e3
commit bb85b24f0a

@ -9,84 +9,24 @@
package java.lang; package java.lang;
import com.facebook.infer.builtins.InferBuiltins;
import com.facebook.infer.builtins.InferUndefined; import com.facebook.infer.builtins.InferUndefined;
import java.io.*; import java.io.FileDescriptor;
public abstract class Process { public abstract class Process {
protected final int pid;
protected final InputStream inputStream;
protected final OutputStream outputStream;
protected final InputStream errorStream;
public Process(int pid, FileDescriptor in, FileDescriptor out, FileDescriptor err) { public Process(int pid, FileDescriptor in, FileDescriptor out, FileDescriptor err) {
this.pid = pid; InferBuiltins.__set_file_attribute(this);
this.inputStream = new ProcessInputStream(in); }
this.outputStream = new ProcessOutputStream(out);
this.errorStream = new ProcessInputStream(err);
}
public int exitValue() {
return InferUndefined.int_undefined();
}
public InputStream getInputStream() {
return this.inputStream;
}
public OutputStream getOutputStream() {
return this.outputStream;
}
public int waitFor() throws InterruptedException {
return InferUndefined.int_undefined();
}
public void destroy() {
try {
inputStream.close();
} catch (IOException e) {}
try {
outputStream.close();
} catch (IOException e) {}
try {
errorStream.close();
} catch (IOException e) {}
}
public Process destroyForcibly() {
destroy();
return this;
}
private static class ProcessInputStream extends FileInputStream {
private FileDescriptor fd;
private ProcessInputStream(FileDescriptor fd) {
super(fd);
this.fd = fd;
}
@Override
public void close() throws IOException {
super.close();
}
}
private static class ProcessOutputStream extends FileOutputStream {
private FileDescriptor fd;
private ProcessOutputStream(FileDescriptor fd) { public void destroy() {
super(fd); InferBuiltins.__set_mem_attribute(this);
this.fd = fd; }
}
@Override public Process destroyForcibly() {
public void close() throws IOException { destroy();
super.close(); return this;
} }
}
} }

@ -19,47 +19,26 @@ import java.lang.ref.WeakReference;
final class ProcessManager { abstract class ProcessManager {
public Process exec(String[] taintedCommand, String[] taintedEnvironment, File workingDirectory,
boolean redirectErrorStream) throws IOException {
public Process exec(String[] taintedCommand, String[] taintedEnvironment, File workingDirectory, FileDescriptor in = new FileDescriptor();
boolean redirectErrorStream) throws IOException { FileDescriptor out = new FileDescriptor();
FileDescriptor err = new FileDescriptor();
FileDescriptor in = new FileDescriptor(); ProcessImpl process = new ProcessImpl(InferUndefined.int_undefined(), in, out, err);
FileDescriptor out = new FileDescriptor(); return process;
FileDescriptor err = new FileDescriptor(); }
ProcessImpl process = new ProcessImpl(InferUndefined.int_undefined(), in, out, err); public static native ProcessManager getInstance();
return process;
}
static class ProcessReference extends WeakReference<ProcessImpl> {
final int processId;
public ProcessReference(ProcessImpl referent, ProcessReferenceQueue referenceQueue) {
super(referent, referenceQueue);
this.processId = referent.pid;
}
}
static class ProcessReferenceQueue extends ReferenceQueue<ProcessImpl> {
}
static class ProcessImpl extends Process {
ProcessImpl(int pid, FileDescriptor in, FileDescriptor out, FileDescriptor err) {
super(pid, in, out, err);
}
void setExitValue(int exitValue) {
}
}
private static final ProcessManager instance = new ProcessManager(); static class ProcessImpl extends Process {
public static ProcessManager getInstance() { ProcessImpl(int pid, FileDescriptor in, FileDescriptor out, FileDescriptor err) {
return instance; super(pid, in, out, err);
} }
}
} }

@ -15,33 +15,33 @@ import java.io.IOException;
public class Runtime { public class Runtime {
private Runtime() { private Runtime() {
} }
public Process exec(String command) throws IOException { public Process exec(String command) throws IOException {
return exec(command, null, null); return exec(command, null, null);
} }
public Process exec(String command, String[] envp) throws IOException { public Process exec(String command, String[] envp) throws IOException {
return exec(command, envp, null); return exec(command, envp, null);
} }
public Process exec(String command, String[] envp, File dir) public Process exec(String command, String[] envp, File dir)
throws IOException { throws IOException {
return ProcessManager.getInstance().exec(null, envp, null, false); return ProcessManager.getInstance().exec(null, envp, null, false);
} }
public Process exec(String cmdarray[]) throws IOException { public Process exec(String cmdarray[]) throws IOException {
return exec(cmdarray, null, null); return exec(cmdarray, null, null);
} }
public Process exec(String[] cmdarray, String[] envp) throws IOException { public Process exec(String[] cmdarray, String[] envp) throws IOException {
return exec(cmdarray, envp, null); return exec(cmdarray, envp, null);
} }
public Process exec(String[] cmdarray, String[] envp, File dir) public Process exec(String[] cmdarray, String[] envp, File dir)
throws IOException { throws IOException {
return ProcessManager.getInstance().exec(cmdarray, envp, dir, false); return ProcessManager.getInstance().exec(cmdarray, envp, dir, false);
} }
} }

Loading…
Cancel
Save