Skip to content

Commit f2ee3bf

Browse files
committed
Allow to use Java9 method to get pid (avoid internal reflection)
Change-Id: I50b05b86d53d21b6b8006a78c8aca722ce2372f2 Signed-off-by: Florent BENOIT <fbenoit@redhat.com>
1 parent c1bb162 commit f2ee3bf

1 file changed

Lines changed: 17 additions & 1 deletion

File tree

core/che-core-api-core/src/main/java/org/eclipse/che/api/core/util/UnixProcessManager.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import com.sun.jna.Native;
1515
import java.io.IOException;
1616
import java.lang.reflect.Field;
17+
import java.lang.reflect.InvocationTargetException;
18+
import java.lang.reflect.Method;
1719
import java.util.ArrayList;
1820
import java.util.List;
1921
import java.util.regex.Pattern;
@@ -35,10 +37,12 @@ class UnixProcessManager extends ProcessManager {
3537
private static final CLibrary C_LIBRARY;
3638

3739
private static final Field PID_FIELD;
40+
private static final Method PID_METHOD;
3841

3942
static {
4043
CLibrary lib = null;
4144
Field pidField = null;
45+
Method pidMethod = null;
4246
if (SystemInfo.isUnix()) {
4347
try {
4448
lib = ((CLibrary) Native.loadLibrary("c", CLibrary.class));
@@ -53,11 +57,17 @@ class UnixProcessManager extends ProcessManager {
5357
.getDeclaredField("pid");
5458
pidField.setAccessible(true);
5559
} catch (Exception e) {
56-
LOG.error(e.getMessage(), e);
60+
// try with Java9
61+
try {
62+
pidMethod = Process.class.getDeclaredMethod("pid");
63+
} catch (NoSuchMethodException e1) {
64+
LOG.error(e1.getMessage(), e1);
65+
}
5766
}
5867
}
5968
C_LIBRARY = lib;
6069
PID_FIELD = pidField;
70+
PID_METHOD = pidMethod;
6171
}
6272

6373
private static interface CLibrary extends Library {
@@ -173,6 +183,12 @@ int getPid(Process process) {
173183
} catch (IllegalAccessException e) {
174184
throw new IllegalStateException("Can't get process' pid. Not unix system?", e);
175185
}
186+
} else if (PID_METHOD != null) {
187+
try {
188+
return ((Long) PID_METHOD.invoke(process)).intValue();
189+
} catch (IllegalAccessException | InvocationTargetException e) {
190+
throw new IllegalStateException("Can't get process' pid. Not unix system?", e);
191+
}
176192
} else {
177193
throw new IllegalStateException("Can't get process' pid. Not unix system?");
178194
}

0 commit comments

Comments
 (0)