Skip to content

Commit 0b682c4

Browse files
authored
JAVA-2923: Detect and use Guava's new HostAndPort.getHost method (#1533)
1 parent e035569 commit 0b682c4

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

changelog/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
3.x versions get published.
66
-->
77

8-
### 3.11.0 (in progress)
8+
## 3.11.0 (in progress)
99

1010
- [improvement] JAVA-2705: Remove protocol v5 beta status, add v6-beta.
11+
- [bug] JAVA-2923: Detect and use Guava's new HostAndPort.getHost method.
1112

1213
## 3.10.2
1314

driver-core/src/main/java/com/datastax/driver/core/CloudConfigFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ protected InetSocketAddress getSniProxyAddress(JsonNode proxyMetadata) {
228228
throw new IllegalStateException(
229229
"Invalid proxy metadata: missing port from field sni_proxy_address");
230230
}
231-
return InetSocketAddress.createUnresolved(
232-
sniProxyHostAndPort.getHostText(), sniProxyHostAndPort.getPort());
231+
String host = GuavaCompatibility.INSTANCE.getHost(sniProxyHostAndPort);
232+
return InetSocketAddress.createUnresolved(host, sniProxyHostAndPort.getPort());
233233
} else {
234234
throw new IllegalStateException("Invalid proxy metadata: missing field sni_proxy_address");
235235
}

driver-core/src/main/java/com/datastax/driver/core/GuavaCompatibility.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.common.base.Function;
2020
import com.google.common.collect.BiMap;
2121
import com.google.common.collect.Maps;
22+
import com.google.common.net.HostAndPort;
2223
import com.google.common.reflect.TypeToken;
2324
import com.google.common.util.concurrent.AsyncFunction;
2425
import com.google.common.util.concurrent.FutureCallback;
@@ -180,6 +181,24 @@ public abstract <I, O> ListenableFuture<O> transformAsync(
180181
*/
181182
public abstract Executor sameThreadExecutor();
182183

184+
/**
185+
* Returns the portion of the given {@link HostAndPort} instance that should represent the
186+
* hostname or IPv4/IPv6 literal.
187+
*
188+
* <p>The method {@code HostAndPort.getHostText} has been replaced with {@code
189+
* HostAndPort.getHost} starting with Guava 20.0; it has been completely removed in Guava 22.0.
190+
*/
191+
@SuppressWarnings("JavaReflectionMemberAccess")
192+
public String getHost(HostAndPort hostAndPort) {
193+
try {
194+
// Guava >= 20.0
195+
return (String) HostAndPort.class.getMethod("getHost").invoke(hostAndPort);
196+
} catch (Exception e) {
197+
// Guava < 22.0
198+
return hostAndPort.getHostText();
199+
}
200+
}
201+
183202
private static GuavaCompatibility selectImplementation() {
184203
if (isGuava_19_0_OrHigher()) {
185204
logger.info("Detected Guava >= 19 in the classpath, using modern compatibility layer");

0 commit comments

Comments
 (0)