Skip to content

Commit e6ac784

Browse files
committed
reducing code duplication, reorganize try/catch block in getProcessId
1 parent b3c53bf commit e6ac784

File tree

1 file changed

+11
-24
lines changed

1 file changed

+11
-24
lines changed

java/client/src/org/openqa/selenium/os/ProcessUtils.java

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,7 @@ private static int killWinProcess(Process process) {
125125
int exitValue;
126126

127127
try {
128-
Field f = process.getClass().getDeclaredField("handle");
129-
f.setAccessible(true);
130-
long hndl = f.getLong(process);
131-
132-
Kernel32 kernel = Kernel32.INSTANCE;
133-
WinNT.HANDLE handle = new WinNT.HANDLE();
134-
handle.setPointer(Pointer.createConstant(hndl));
135-
int pid = kernel.GetProcessId(handle);
136-
137-
killPID("" + pid);
128+
killPID("" + getProcessId(process));
138129
exitValue = waitForProcessDeath(process, 10000);
139130
} catch (Exception ex) {
140131
LOG.log(Level.WARNING, "Process refused to die after 10 seconds, and couldn't taskkill it", ex);
@@ -184,28 +175,24 @@ private static void closeAllStreamsAndDestroyProcess(Process process) {
184175
}
185176

186177
static int getProcessId(Process p) {
187-
if (thisIsWindows()) {
188-
try {
178+
try {
179+
if (Platform.getCurrent().is(WINDOWS)) {
189180
Field f = p.getClass().getDeclaredField("handle");
190181
f.setAccessible(true);
191182
long hndl = f.getLong(p);
192183

193184
Kernel32 kernel = Kernel32.INSTANCE;
194185
WinNT.HANDLE handle = new WinNT.HANDLE();
195186
handle.setPointer(Pointer.createConstant(hndl));
196-
int pid = kernel.GetProcessId(handle);
197-
return pid;
198-
} catch (Exception e) {
199-
throw new RuntimeException("Couldn't detect pid", e);
200-
}
201-
} else {
202-
try {
203-
Field f = p.getClass().getDeclaredField("pid");
204-
f.setAccessible(true);
205-
return (Integer) f.get(p);
206-
} catch (Exception e) {
207-
throw new RuntimeException("Couldn't detect pid", e);
187+
return kernel.GetProcessId(handle);
208188
}
189+
190+
Field f = p.getClass().getDeclaredField("pid");
191+
f.setAccessible(true);
192+
return (Integer) f.get(p);
193+
194+
} catch (Exception e) {
195+
throw new RuntimeException("Couldn't detect pid", e);
209196
}
210197
}
211198

0 commit comments

Comments
 (0)