{"record":{"id":"370e788461c064a9","repo":"java-native-access/jna","slug":"library-object-passed-to-getnativelibrary-in-not-a-proxy","errorCode":null,"errorMessage":"library object passed to getNativeLibrary in not a proxy","messagePattern":"library object passed to getNativeLibrary in not a proxy","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/com/sun/jna/Native.java","lineNumber":2041,"sourceCode":"            registeredClasses.put(cls, handles);\n            registeredLibraries.put(cls, lib);\n        }\n    }\n\n    /**\n     * Get the {@link NativeLibrary} instance that is wrapped by the given\n     * {@link Library} interface instance.\n     *\n     * @param library the {@link Library} interface instance, which was created\n     * by the {@link Native#load Native.load()} method\n     * @return the wrapped {@link NativeLibrary} instance\n     */\n    public static NativeLibrary getNativeLibrary(final Library library) {\n        if(library == null) {\n            throw new IllegalArgumentException(\"null passed to getNativeLibrary\");\n        }\n        if(! Proxy.isProxyClass(library.getClass())) {\n            throw new IllegalArgumentException(\"library object passed to getNativeLibrary in not a proxy\");\n        }\n        final InvocationHandler handler = Proxy.getInvocationHandler(library);\n        if (!(handler instanceof Library.Handler)) {\n            throw new IllegalArgumentException(\"Object is not a properly initialized Library interface instance\");\n        }\n        return ((Library.Handler) handler).getNativeLibrary();\n    }\n\n    /**\n     * Get the {@link NativeLibrary} instance to which the given \"registered\"\n     * class is bound.\n     *\n     * @param cls the \"registered\" class, which was previously registered via\n     * the {@link Native#register register()} method\n     * @return the {@link NativeLibrary} instance to which the \"registered\"\n     * class is bound\n     */\n    public static NativeLibrary getNativeLibrary(final Class<?> cls) {","sourceCodeStart":2023,"sourceCodeEnd":2059,"githubUrl":"https://github.com/java-native-access/jna/blob/d036ad9781adad4b66693e8fa7098e4ac665e0a3/src/com/sun/jna/Native.java#L2023-L2059","documentation":"Native.getNativeLibrary(Library) requires a JDK dynamic proxy created by Native.load(); any other object throws this IllegalArgumentException. The check is Proxy.isProxyClass(library.getClass()), which fails for hand-written implementations or mocks.","triggerScenarios":"Passing a concrete class, anonymous implementation, Mockito/EasyMock mock, or a proxy from another framework (e.g. cglib, Spring) that implements the Library interface.","commonSituations":"Unit tests substituting mocks for the loaded library; wrapping the library in a decorator; using a JDK dynamic proxy created by unrelated code.","solutions":["Pass only the object returned by Native.load()/Native.loadLibrary().","In tests, test against the interface returned from load() rather than a mock of Library, or abstract the accessor behind your own seam.","Check the call path for accidental wrapping/unwrapping before the getNativeLibrary call."],"exampleFix":"// before\nMyLib lib = new MyLibImpl(); // not a JNA proxy\nNativeLibrary nl = Native.getNativeLibrary(lib);\n// after\nMyLib lib = Native.load(\"mylib\", MyLib.class);\nNativeLibrary nl = Native.getNativeLibrary(lib);","handlingStrategy":"type-guard","validationCode":"if (!Proxy.isProxyClass(lib.getClass())) {\n    throw new IllegalStateException(\"Pass the object returned by Native.load(), not \"\n        + lib.getClass().getName());\n}","typeGuard":"boolean isJnaLibraryProxy(Object o) {\n    return o != null && Proxy.isProxyClass(o.getClass())\n        && Proxy.getInvocationHandler(o) instanceof Library.Handler;\n}","tryCatchPattern":"try {\n    NativeLibrary nl = Native.getNativeLibrary(lib);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"not a proxy\")) {\n        throw new IllegalStateException(\"Use the Native.load()-returned proxy\", e);\n    }\n    throw e;\n}","preventionTips":["Only call getNativeLibrary with the exact object returned from Native.load().","Avoid mocks/decorators at the layer that unwraps the NativeLibrary.","Capture NativeLibrary.getInstance() separately at load time if you need it elsewhere."],"tags":["jna","illegal-argument","proxy","type-mismatch"],"backgroundTag":"invalid-argument-value","analyzedSha":"d036ad9781adad4b66693e8fa7098e4ac665e0a3","analyzedAt":"2026-09-12T06:50:59.239Z","contentChangedAt":"2026-09-12T06:50:59.239Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}