From d371b1bbaa3b98f957f6b025673098ad3adb5131 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 28 Oct 2020 19:22:08 +0100 Subject: [PATCH] Rethrow NoSuchMethodError with more hints about incompatible native library versions (#10740) Motivation: 03aafb9cff352269a7fc73e4cf0c281770676be9 did ensure we fail while loading a natibe library which is not compatible. While this is great it is still sometimes hard for people to understand what NoSuchMethodError means in this context. Modifications: If possible rethrow the NoSuchMethodError and provide some more hints about multiple versions of the shared library Result: Easier to understand for people why loading fails --- .../netty/util/internal/NativeLibraryLoader.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java b/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java index 589818da03..ed52e612d5 100644 --- a/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java +++ b/common/src/main/java/io/netty/util/internal/NativeLibraryLoader.java @@ -350,6 +350,11 @@ public final class NativeLibraryLoader { } NativeLibraryUtil.loadLibrary(name, absolute); // Fallback to local helper class. logger.debug("Successfully loaded the library {}", name); + } catch (NoSuchMethodError nsme) { + if (suppressed != null) { + ThrowableUtil.addSuppressed(nsme, suppressed); + } + rethrowWithMoreDetailsIfPossible(name, nsme); } catch (UnsatisfiedLinkError ule) { if (suppressed != null) { ThrowableUtil.addSuppressed(ule, suppressed); @@ -358,6 +363,15 @@ public final class NativeLibraryLoader { } } + @SuppressJava6Requirement(reason = "Guarded by version check") + private static void rethrowWithMoreDetailsIfPossible(String name, NoSuchMethodError error) { + if (PlatformDependent.javaVersion() >= 7) { + throw new LinkageError( + "Possible multiple incompatible native libraries on the classpath for '" + name + "'?", error); + } + throw error; + } + private static void loadLibraryByHelper(final Class helper, final String name, final boolean absolute) throws UnsatisfiedLinkError { Object ret = AccessController.doPrivileged(new PrivilegedAction() {