From 711c1a45aa4ac6fd3dd3e283624a932382173c22 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 28 Aug 2019 08:08:01 +0200 Subject: [PATCH] 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. --- .../netty/resolver/dns/DnsNameResolver.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java index 7e037c34d1..7bb5f2436d 100644 --- a/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java +++ b/resolver-dns/src/main/java/io/netty/resolver/dns/DnsNameResolver.java @@ -178,14 +178,19 @@ public class DnsNameResolver extends InetNameResolver { @SuppressWarnings("unchecked") private static List getSearchDomainsHack() throws Exception { - // This code on Java 9+ yields a warning about illegal reflective access that will be denied in - // a future release. There doesn't seem to be a better way to get search domains for Windows yet. - Class configClass = Class.forName("sun.net.dns.ResolverConfiguration"); - Method open = configClass.getMethod("open"); - Method nameservers = configClass.getMethod("searchlist"); - Object instance = open.invoke(null); + // Only try if not using Java9 and later + // See https://github.com/netty/netty/issues/9500 + if (PlatformDependent.javaVersion() < 9) { + // This code on Java 9+ yields a warning about illegal reflective access that will be denied in + // a future release. There doesn't seem to be a better way to get search domains for Windows yet. + 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) nameservers.invoke(instance); + return (List) nameservers.invoke(instance); + } + return Collections.emptyList(); } private static final DatagramDnsResponseDecoder DATAGRAM_DECODER = new DatagramDnsResponseDecoder() {