Skip to content

Commit

Permalink
Call NetworkInterface.* w/o reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
pzygielo committed Dec 6, 2024
1 parent a853f74 commit 20da39d
Showing 1 changed file with 15 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
* Copyright (c) 2023, 2024 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -25,7 +25,6 @@
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
Expand Down Expand Up @@ -129,27 +128,7 @@ public class NetworkUtility {
}
}

private static Method isLoopbackMethod = null;
private static Method isUpMethod = null;
private static Method supportsMulticastMethod = null;

static {
// JDK 1.6
try {
isLoopbackMethod = NetworkInterface.class.getMethod("isLoopback");
} catch (Throwable t) {
isLoopbackMethod = null;
}
try {
isUpMethod = NetworkInterface.class.getMethod("isUp");
} catch (Throwable t) {
isUpMethod = null;
}
try {
supportsMulticastMethod = NetworkInterface.class.getMethod("supportsMulticast");
} catch (Throwable t) {
supportsMulticastMethod = null;
}
String vendor = System.getProperty("java.vendor");
IS_AIX_JDK = vendor == null ? false : vendor.startsWith("IBM");
}
Expand Down Expand Up @@ -455,11 +434,9 @@ public static boolean isLoopbackNetworkInterface(NetworkInterface anInterface) {
if (anInterface == null) {
return false;
}
if (isLoopbackMethod != null) {
try {
return (Boolean) isLoopbackMethod.invoke(anInterface);
} catch (Throwable t) {
}
try {
return anInterface.isLoopback();
} catch (Throwable t) {
}
boolean hasLoopback = false;
Enumeration<InetAddress> allIntfAddr = anInterface.getInetAddresses();
Expand All @@ -477,13 +454,11 @@ public static boolean supportsMulticast(NetworkInterface anInterface) {
if (anInterface == null) {
return false;
}
boolean result = true;
if (isUpMethod != null) {
try {
result = (Boolean) isUpMethod.invoke(anInterface);
} catch (Throwable t) {
result = false;
}
boolean result;
try {
result = anInterface.isUp();
} catch (Throwable t) {
result = false;
}
if (!result) {
return result;
Expand All @@ -494,9 +469,9 @@ public static boolean supportsMulticast(NetworkInterface anInterface) {
LOG.fine("Workaround for java.net.NetworkInterface.supportsMulticast() returning false on AIX");
}
return true;
} else if (supportsMulticastMethod != null) {
} else {
try {
return (Boolean) supportsMulticastMethod.invoke(anInterface);
return anInterface.supportsMulticast();
} catch (Throwable t) {
// will just return false in this case
}
Expand All @@ -509,12 +484,10 @@ public static boolean isUp(NetworkInterface anInterface) {
if (anInterface == null) {
return false;
}
if (isUpMethod != null) {
try {
return (Boolean) isUpMethod.invoke(anInterface);
} catch (Throwable t) {
// will just return false in this case
}
try {
return anInterface.isUp();
} catch (Throwable t) {
// will just return false in this case
}
return false;
}
Expand Down

0 comments on commit 20da39d

Please sign in to comment.