You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by jb...@apache.org on 2022/04/06 20:16:07 UTC

[geode] branch develop updated: GEODE-10127: Corrects NullPointerException in hashCode(). (#7559)

This is an automated email from the ASF dual-hosted git repository.

jbarrett pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 44293ffa54 GEODE-10127: Corrects NullPointerException in hashCode(). (#7559)
44293ffa54 is described below

commit 44293ffa54ac85b3fd7681af93aff4107d955fa9
Author: Jacob Barrett <jb...@pivotal.io>
AuthorDate: Wed Apr 6 13:16:01 2022 -0700

    GEODE-10127: Corrects NullPointerException in hashCode(). (#7559)
    
    When unmarshalling an instance with a host name that can't be resolved a
    NullPointerException is thrown in hashCode(). Use Objects.hash() to
    resolve hash code to avoid NullPointerException.
---
 .../geode/internal/admin/remote/DistributionLocatorId.java     | 10 ++--------
 .../geode/internal/admin/remote/DistributionLocatorIdTest.java |  9 +++++++++
 2 files changed, 11 insertions(+), 8 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/internal/admin/remote/DistributionLocatorId.java b/geode-core/src/main/java/org/apache/geode/internal/admin/remote/DistributionLocatorId.java
index 84b5cbe978..9286b5b1e6 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/admin/remote/DistributionLocatorId.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/admin/remote/DistributionLocatorId.java
@@ -16,6 +16,7 @@
 package org.apache.geode.internal.admin.remote;
 
 import static java.lang.String.format;
+import static java.util.Objects.hash;
 import static org.apache.commons.lang3.ObjectUtils.defaultIfNull;
 import static org.apache.commons.lang3.ObjectUtils.getIfNull;
 import static org.apache.commons.lang3.StringUtils.isEmpty;
@@ -335,14 +336,7 @@ public class DistributionLocatorId implements java.io.Serializable {
 
   @Override
   public int hashCode() {
-    int result = 17;
-    final int mult = 37;
-
-    result = mult * result + host.hashCode();
-    result = mult * result + port;
-    result = mult * result + bindAddress.hashCode();
-
-    return result;
+    return hash(host, port, bindAddress);
   }
 
   /**
diff --git a/geode-core/src/test/java/org/apache/geode/internal/admin/remote/DistributionLocatorIdTest.java b/geode-core/src/test/java/org/apache/geode/internal/admin/remote/DistributionLocatorIdTest.java
index 8ba7d5dc27..4e1b9b149c 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/admin/remote/DistributionLocatorIdTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/admin/remote/DistributionLocatorIdTest.java
@@ -16,6 +16,7 @@
 package org.apache.geode.internal.admin.remote;
 
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatNoException;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -138,4 +139,12 @@ class DistributionLocatorIdTest {
 
     assertThat(locatorId.marshalForClients()).isEqualTo("hostname-for-clients.example.com[1234]");
   }
+
+  @SuppressWarnings("ResultOfMethodCallIgnored")
+  @Test
+  void hashCodeDoesNotThrowWhenHostIsNull() {
+    final DistributionLocatorId locatorId =
+        DistributionLocatorId.unmarshal("unknown.invalid[1234]");
+    assertThatNoException().isThrownBy(locatorId::hashCode);
+  }
 }