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
This commit is contained in:
Norman Maurer 2020-10-28 19:22:08 +01:00
parent 3d9d473685
commit 38adfd8316

View File

@ -347,6 +347,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);
@ -355,6 +360,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((PrivilegedAction<Object>) () -> {