2828import java .util .List ;
2929import java .util .Objects ;
3030import java .util .concurrent .TimeUnit ;
31+ import org .jspecify .annotations .NullMarked ;
32+ import org .jspecify .annotations .Nullable ;
3133import org .openqa .selenium .Platform ;
3234import org .openqa .selenium .WebDriverException ;
35+ import org .openqa .selenium .internal .Require ;
3336
37+ @ NullMarked
3438public class NetworkUtils {
3539
36- private static InetAddress cachedIp4NonLoopbackAddressOfThisMachine ;
37- private static String cachedIp4NonLoopbackAddressHostName ;
40+ private static @ Nullable InetAddress cachedIp4NonLoopbackAddressOfThisMachine ;
41+ private static @ Nullable String cachedIp4NonLoopbackAddressHostName ;
3842
3943 private final NetworkInterfaceProvider networkInterfaceProvider ;
40- private volatile String hostname ;
41- private volatile String address ;
44+ private volatile @ Nullable String hostname ;
45+ private volatile @ Nullable String address ;
4246
4347 NetworkUtils (NetworkInterfaceProvider networkInterfaceProvider ) {
4448 this .networkInterfaceProvider = networkInterfaceProvider ;
@@ -56,13 +60,13 @@ public NetworkUtils() {
5660 public String getHostname () {
5761 determineHostnameAndAddress ();
5862
59- return hostname ;
63+ return Require . nonNull ( "Hostname" , hostname ) ;
6064 }
6165
6266 public String getHostAddress () {
6367 determineHostnameAndAddress ();
6468
65- return address ;
69+ return Require . nonNull ( "Address" , address ) ;
6670 }
6771
6872 public String getPrivateLocalAddress () {
@@ -81,7 +85,7 @@ public String getPrivateLocalAddress() {
8185 *
8286 * @return A String representing the host name or non-loopback IP4 address of this machine.
8387 */
84- public String getNonLoopbackAddressOfThisMachine () {
88+ public @ Nullable String getNonLoopbackAddressOfThisMachine () {
8589 InetAddress ip4NonLoopbackAddressOfThisMachine = getIp4NonLoopbackAddressOfThisMachine ();
8690 if (!Objects .equals (
8791 cachedIp4NonLoopbackAddressOfThisMachine , ip4NonLoopbackAddressOfThisMachine )) {
@@ -113,10 +117,13 @@ public InetAddress getIp4NonLoopbackAddressOfThisMachine() {
113117 *
114118 * @return The address part og such an address
115119 */
116- public String obtainLoopbackIp4Address () {
120+ public @ Nullable String obtainLoopbackIp4Address () {
117121 final NetworkInterface networkInterface = getLoopBackAndIp4Only ();
118122 if (networkInterface != null ) {
119- return networkInterface .getIp4LoopbackOnly ().getHostName ();
123+ InetAddress loopback = networkInterface .getIp4LoopbackOnly ();
124+ if (loopback != null ) {
125+ return loopback .getHostName ();
126+ }
120127 }
121128
122129 final String ipOfIp4LoopBack = getIpOfLoopBackIp4 ();
@@ -156,7 +163,7 @@ private InetAddress grabFirstNetworkAddress() {
156163 return firstAddress ;
157164 }
158165
159- public String getIpOfLoopBackIp4 () {
166+ public @ Nullable String getIpOfLoopBackIp4 () {
160167 for (NetworkInterface iface : networkInterfaceProvider .getNetworkInterfaces ()) {
161168 final InetAddress netAddress = iface .getIp4LoopbackOnly ();
162169 if (netAddress != null ) {
@@ -166,7 +173,7 @@ public String getIpOfLoopBackIp4() {
166173 return null ;
167174 }
168175
169- private NetworkInterface getLoopBackAndIp4Only () {
176+ private @ Nullable NetworkInterface getLoopBackAndIp4Only () {
170177 for (NetworkInterface iface : networkInterfaceProvider .getNetworkInterfaces ()) {
171178 if (iface .isIp4AddressBindingOnly () && iface .isLoopBack ()) {
172179 return iface ;
0 commit comments