Rethrow NoSuchMethodError with more hints about incompatible native library versions (#10740)

Motivation:

03aafb9cff 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
This commit is contained in:
Norman Maurer 2020-10-28 19:22:08 +01:00 committed by GitHub
parent d4defc30b8
commit d371b1bbaa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -350,6 +350,11 @@ public final class NativeLibraryLoader {
} }
NativeLibraryUtil.loadLibrary(name, absolute); // Fallback to local helper class. NativeLibraryUtil.loadLibrary(name, absolute); // Fallback to local helper class.
logger.debug("Successfully loaded the library {}", name); logger.debug("Successfully loaded the library {}", name);
} catch (NoSuchMethodError nsme) {
if (suppressed != null) {
ThrowableUtil.addSuppressed(nsme, suppressed);
}
rethrowWithMoreDetailsIfPossible(name, nsme);
} catch (UnsatisfiedLinkError ule) { } catch (UnsatisfiedLinkError ule) {
if (suppressed != null) { if (suppressed != null) {
ThrowableUtil.addSuppressed(ule, suppressed); 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) private static void loadLibraryByHelper(final Class<?> helper, final String name, final boolean absolute)
throws UnsatisfiedLinkError { throws UnsatisfiedLinkError {
Object ret = AccessController.doPrivileged(new PrivilegedAction<Object>() { Object ret = AccessController.doPrivileged(new PrivilegedAction<Object>() {