From 999b51b026be031ee34fa127e9338c29b135c7d3 Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Mon, 13 Jan 2014 22:28:28 +0900 Subject: [PATCH] Get the PID properly on Android - Related: #2109 --- .../java/io/netty/channel/DefaultChannelId.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/transport/src/main/java/io/netty/channel/DefaultChannelId.java b/transport/src/main/java/io/netty/channel/DefaultChannelId.java index 0e7e2c1d33..4e9e799e45 100644 --- a/transport/src/main/java/io/netty/channel/DefaultChannelId.java +++ b/transport/src/main/java/io/netty/channel/DefaultChannelId.java @@ -228,9 +228,10 @@ final class DefaultChannelId implements ChannelId { } private static int defaultProcessId() { + final ClassLoader loader = ClassLoader.getSystemClassLoader(); String value; try { - ClassLoader loader = ClassLoader.getSystemClassLoader(); + // Invoke java.lang.management.ManagementFactory.getRuntimeMXBean().getName() Class mgmtFactoryType = Class.forName("java.lang.management.ManagementFactory", true, loader); Class runtimeMxBeanType = Class.forName("java.lang.management.RuntimeMXBean", true, loader); @@ -240,7 +241,15 @@ final class DefaultChannelId implements ChannelId { value = (String) getName.invoke(bean, null); } catch (Exception e) { logger.debug("Could not invoke ManagementFactory.getRuntimeMXBean().getName(); Android?", e); - value = ""; + try { + // Invoke android.os.Process.myPid() + Class processType = Class.forName("android.os.Process", true, loader); + Method myPid = processType.getMethod("myPid", null); + value = myPid.invoke(null, null).toString(); + } catch (Exception e2) { + logger.debug("Could not invoke Process.myPid(); not Android?", e2); + value = ""; + } } int atIndex = value.indexOf('@');