Allow to use Java9 method to get pid (avoid internal reflection)
Change-Id: I50b05b86d53d21b6b8006a78c8aca722ce2372f2 Signed-off-by: Florent BENOIT <fbenoit@redhat.com>6.19.x
parent
c1bb1621a0
commit
f2ee3bf834
|
|
@ -14,6 +14,8 @@ import com.sun.jna.Library;
|
|||
import com.sun.jna.Native;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Pattern;
|
||||
|
|
@ -35,10 +37,12 @@ class UnixProcessManager extends ProcessManager {
|
|||
private static final CLibrary C_LIBRARY;
|
||||
|
||||
private static final Field PID_FIELD;
|
||||
private static final Method PID_METHOD;
|
||||
|
||||
static {
|
||||
CLibrary lib = null;
|
||||
Field pidField = null;
|
||||
Method pidMethod = null;
|
||||
if (SystemInfo.isUnix()) {
|
||||
try {
|
||||
lib = ((CLibrary) Native.loadLibrary("c", CLibrary.class));
|
||||
|
|
@ -53,11 +57,17 @@ class UnixProcessManager extends ProcessManager {
|
|||
.getDeclaredField("pid");
|
||||
pidField.setAccessible(true);
|
||||
} catch (Exception e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
// try with Java9
|
||||
try {
|
||||
pidMethod = Process.class.getDeclaredMethod("pid");
|
||||
} catch (NoSuchMethodException e1) {
|
||||
LOG.error(e1.getMessage(), e1);
|
||||
}
|
||||
}
|
||||
}
|
||||
C_LIBRARY = lib;
|
||||
PID_FIELD = pidField;
|
||||
PID_METHOD = pidMethod;
|
||||
}
|
||||
|
||||
private static interface CLibrary extends Library {
|
||||
|
|
@ -173,6 +183,12 @@ class UnixProcessManager extends ProcessManager {
|
|||
} catch (IllegalAccessException e) {
|
||||
throw new IllegalStateException("Can't get process' pid. Not unix system?", e);
|
||||
}
|
||||
} else if (PID_METHOD != null) {
|
||||
try {
|
||||
return ((Long) PID_METHOD.invoke(process)).intValue();
|
||||
} catch (IllegalAccessException | InvocationTargetException e) {
|
||||
throw new IllegalStateException("Can't get process' pid. Not unix system?", e);
|
||||
}
|
||||
} else {
|
||||
throw new IllegalStateException("Can't get process' pid. Not unix system?");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue