Do not try to retrieve domain search list via reflection hack on windows when using Java9 and later (#9511)

Motivation:

We currently try to access the the domain search list via reflection on windows which will print a illegal access warning when using Java9 and later.

Modifications:

Add a guard against the used java version.

Result:

Fixes https://github.com/netty/netty/issues/9500.
This commit is contained in:
Norman Maurer 2019-08-28 08:08:01 +02:00
parent bf086c1aa3
commit 711c1a45aa

View File

@ -178,14 +178,19 @@ public class DnsNameResolver extends InetNameResolver {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private static List<String> getSearchDomainsHack() throws Exception { private static List<String> getSearchDomainsHack() throws Exception {
// This code on Java 9+ yields a warning about illegal reflective access that will be denied in // Only try if not using Java9 and later
// a future release. There doesn't seem to be a better way to get search domains for Windows yet. // See https://github.com/netty/netty/issues/9500
Class<?> configClass = Class.forName("sun.net.dns.ResolverConfiguration"); if (PlatformDependent.javaVersion() < 9) {
Method open = configClass.getMethod("open"); // This code on Java 9+ yields a warning about illegal reflective access that will be denied in
Method nameservers = configClass.getMethod("searchlist"); // a future release. There doesn't seem to be a better way to get search domains for Windows yet.
Object instance = open.invoke(null); Class<?> configClass = Class.forName("sun.net.dns.ResolverConfiguration");
Method open = configClass.getMethod("open");
Method nameservers = configClass.getMethod("searchlist");
Object instance = open.invoke(null);
return (List<String>) nameservers.invoke(instance); return (List<String>) nameservers.invoke(instance);
}
return Collections.emptyList();
} }
private static final DatagramDnsResponseDecoder DATAGRAM_DECODER = new DatagramDnsResponseDecoder() { private static final DatagramDnsResponseDecoder DATAGRAM_DECODER = new DatagramDnsResponseDecoder() {