Skip to content

Commit 811d614

Browse files
committed
fix(loader): anchor native-library resource path to fixed package root
LlamaLoader.getNativeResourcePath() derived the classpath location of the bundled native libraries from the loader's OWN Java package via LlamaLoader.class.getPackage(). The layered restructure moved LlamaLoader from net.ladenthin.llama to net.ladenthin.llama.loader, so the lookup became /net/ladenthin/llama/loader/<os>/<arch>/libjllama.so — one directory too deep. CMakeLists.txt and the publish workflow emit the libs to the fixed layout /net/ladenthin/llama/<os>/<arch>/, so getResource(...) returned null and every native-backed test failed at runtime with "No native library found" (and the packaged JAR would fail identically for real consumers). Fix: anchor the resource root to a NATIVE_RESOURCE_BASE constant ("/net/ladenthin/llama") independent of the loader's Java package, matching the build/CI layout. Adds a regression test pinning the exact prefix and asserting the path never contains "/loader/" (the pre-existing contains-substring test stayed green through the bug, which is why it slipped in). Same class of latent "path derived from something that moved in the restructure" bug as the spotbugs-exclude / PIT-target / CMake-OSInfo FQN repairs.
1 parent 78cfef1 commit 811d614

2 files changed

Lines changed: 30 additions & 8 deletions

File tree

src/main/java/net/ladenthin/llama/loader/LlamaLoader.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,15 @@ public class LlamaLoader {
4848
private static final LlamaSystemProperties systemProperties = new LlamaSystemProperties();
4949
private static final NativeLibraryPermissionSetter permissionSetter = new NativeLibraryPermissionSetter(System.err);
5050

51+
/**
52+
* Canonical classpath root for the bundled native libraries. Fixed by
53+
* {@code CMakeLists.txt} and the publish workflow (both emit to
54+
* {@code resources/net/ladenthin/llama/<os>/<arch>/}); it must NOT be
55+
* derived from this loader's own Java package, which moved to
56+
* {@code net.ladenthin.llama.loader} during the layered restructure.
57+
*/
58+
private static final String NATIVE_RESOURCE_BASE = "/net/ladenthin/llama";
59+
5160
/** Static utility holder; not instantiable. */
5261
private LlamaLoader() {}
5362

@@ -267,14 +276,7 @@ static File getTempDir() {
267276
}
268277

269278
static String getNativeResourcePath() {
270-
final Package pkg = LlamaLoader.class.getPackage();
271-
// LlamaLoader is in a named package, so Class.getPackage() is never null here.
272-
if (pkg == null) {
273-
throw new IllegalStateException("LlamaLoader.class.getPackage() returned null (classLoader="
274-
+ LlamaLoader.class.getClassLoader() + ")");
275-
}
276-
String packagePath = pkg.getName().replace('.', '/');
277-
return String.format("/%s/%s", packagePath, OSInfo.getNativeLibFolderPathForCurrentOS());
279+
return String.format("%s/%s", NATIVE_RESOURCE_BASE, OSInfo.getNativeLibFolderPathForCurrentOS());
278280
}
279281

280282
private static boolean hasNativeLib(String path, String libraryName) {

src/test/java/net/ladenthin/llama/loader/LlamaLoaderTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,24 @@ public void testGetNativeResourcePathContainsOsAndArch() {
187187
String osArch = OSInfo.getNativeLibFolderPathForCurrentOS();
188188
assertTrue(path.endsWith(osArch), "Resource path should end with OS/arch: " + path);
189189
}
190+
191+
/**
192+
* Regression for the layered-restructure bug: the native-library classpath
193+
* root is fixed at {@code /net/ladenthin/llama/<os>/<arch>} by CMakeLists +
194+
* the publish workflow, so it must NOT track the loader's own Java package
195+
* (which moved to {@code net.ladenthin.llama.loader}). Deriving it from
196+
* {@code LlamaLoader.class.getPackage()} produced {@code .../llama/loader/...},
197+
* one level too deep, so {@code getResource(...)} returned null and every
198+
* native-backed test failed with "No native library found".
199+
*/
200+
@Test
201+
public void testGetNativeResourcePathIsPackageIndependent() {
202+
String path = LlamaLoader.getNativeResourcePath();
203+
String osArch = OSInfo.getNativeLibFolderPathForCurrentOS();
204+
assertEquals("/net/ladenthin/llama/" + osArch, path);
205+
assertFalse(
206+
path.contains("/loader/"),
207+
"Resource path must not include the loader subpackage — the native libs live at "
208+
+ "/net/ladenthin/llama/<os>/<arch>, not under the loader package: " + path);
209+
}
190210
}

0 commit comments

Comments
 (0)