Skip to content

Commit fea197a

Browse files
Collect both getNativeLibrary variants into Native and add test
1 parent 3bedcfb commit fea197a

File tree

3 files changed

+116
-12
lines changed

3 files changed

+116
-12
lines changed

src/com/sun/jna/Library.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -272,16 +272,4 @@ public Object invoke(Object proxy, Method method, Object[] inArgs)
272272
}
273273
}
274274

275-
/**
276-
* Get the {@link NativeLibrary} instance that is wrapped by the given {@link Library} interface instance.
277-
* @param library the {@link Library} interface instance, which was created by the {@link Native#load Native.load()} method
278-
* @return the wrapped {@link NativeLibrary} instance
279-
*/
280-
static NativeLibrary getNativeLibrary(final Library library) {
281-
final InvocationHandler handler = Proxy.getInvocationHandler(library);
282-
if (!(handler instanceof Handler)) {
283-
throw new IllegalArgumentException("Object is not a properly initialized Library interface instance");
284-
}
285-
return ((Handler)handler).getNativeLibrary();
286-
}
287275
}

src/com/sun/jna/Native.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,12 +1918,34 @@ public static void register(Class<?> cls, NativeLibrary lib) {
19181918
}
19191919
}
19201920

1921+
/**
1922+
* Get the {@link NativeLibrary} instance that is wrapped by the given {@link Library} interface instance.
1923+
* @param library the {@link Library} interface instance, which was created by the {@link Native#load Native.load()} method
1924+
* @return the wrapped {@link NativeLibrary} instance
1925+
*/
1926+
public static NativeLibrary getNativeLibrary(final Library library) {
1927+
if(library == null) {
1928+
throw new IllegalArgumentException("null passed to getNativeLibrary");
1929+
}
1930+
if(! Proxy.isProxyClass(library.getClass())) {
1931+
throw new IllegalArgumentException("library object passed to getNativeLibrary in not a proxy");
1932+
}
1933+
final InvocationHandler handler = Proxy.getInvocationHandler(library);
1934+
if (!(handler instanceof Library.Handler)) {
1935+
throw new IllegalArgumentException("Object is not a properly initialized Library interface instance");
1936+
}
1937+
return ((Library.Handler) handler).getNativeLibrary();
1938+
}
1939+
19211940
/**
19221941
* Get the {@link NativeLibrary} instance to which the given "registered" class is bound.
19231942
* @param cls the "registered" class, which was previously registered via the {@link Native#register register()} method
19241943
* @return the {@link NativeLibrary} instance to which the "registered" class is bound
19251944
*/
19261945
public static NativeLibrary getNativeLibrary(final Class<?> cls) {
1946+
if(cls == null) {
1947+
throw new IllegalArgumentException("null passed to getNativeLibrary");
1948+
}
19271949
final Class<?> mappedClass = findDirectMappedClass(cls);
19281950
synchronized(registeredClasses) {
19291951
final NativeLibrary nativeLibrary = registeredLibraries.get(mappedClass);
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/* Copyright (c) 2024 Matthias Bläsing, All Rights Reserved
2+
*
3+
* The contents of this file is dual-licensed under 2
4+
* alternative Open Source/Free licenses: LGPL 2.1 or later and
5+
* Apache License 2.0. (starting with JNA version 4.0.0).
6+
*
7+
* You can freely decide which license you want to apply to
8+
* the project.
9+
*
10+
* You may obtain a copy of the LGPL License at:
11+
*
12+
* http://www.gnu.org/licenses/licenses.html
13+
*
14+
* A copy is also included in the downloadable source code package
15+
* containing JNA, in file "LGPL2.1".
16+
*
17+
* You may obtain a copy of the Apache License at:
18+
*
19+
* http://www.apache.org/licenses/
20+
*
21+
* A copy is also included in the downloadable source code package
22+
* containing JNA, in file "AL2.0".
23+
*/
24+
package com.sun.jna;
25+
26+
import java.util.Collections;
27+
import junit.framework.TestCase;
28+
29+
/**
30+
* Check getNativeLibrary functions in Native
31+
*/
32+
public class NativeGetNativeLibraryTest extends TestCase {
33+
34+
private NativeLibrary libUTF8;
35+
private TestLib libUTF8Interface;
36+
37+
@Override
38+
protected void setUp() {
39+
libUTF8 = NativeLibrary.getInstance("testlib",
40+
Collections.singletonMap(Library.OPTION_STRING_ENCODING, "UTF-8"));
41+
Native.register(TestLibUTF8.class, libUTF8);
42+
libUTF8Interface = Native.load("testlib", TestLib.class,
43+
Collections.singletonMap(Library.OPTION_STRING_ENCODING, "UTF-8"));
44+
}
45+
46+
public void testGetNativeLibraryInterface() {
47+
NativeLibrary nl = Native.getNativeLibrary(libUTF8Interface);
48+
assertTrue(nl instanceof NativeLibrary);
49+
}
50+
51+
public void testGetNativeLibraryDirect() {
52+
NativeLibrary nl = Native.getNativeLibrary(TestLibUTF8.class);
53+
assertTrue(nl instanceof NativeLibrary);
54+
// This only makes sense for the direct case, as that directly wraps
55+
// a supplied instance
56+
assertEquals(libUTF8, nl);
57+
}
58+
59+
public void testGetNativeLibraryOnUnboundShouldFail() {
60+
try {
61+
Native.getNativeLibrary(new TestLib() {
62+
@Override
63+
public String returnStringArgument(Pointer input) {
64+
return "";
65+
}
66+
});
67+
assertTrue("Exception not thrown", false);
68+
} catch (IllegalArgumentException ex) {
69+
// This should be reached
70+
}
71+
}
72+
73+
public void testGetNativeLibraryOnNullShouldFail() {
74+
try {
75+
Native.getNativeLibrary((Class) null);
76+
assertTrue("Exception not thrown", false);
77+
} catch (IllegalArgumentException ex) {
78+
// This should be reached
79+
}
80+
}
81+
82+
public static void main(java.lang.String[] argList) {
83+
junit.textui.TestRunner.run(NativeGetNativeLibraryTest.class);
84+
}
85+
86+
private static class TestLibUTF8 implements Library {
87+
native String returnStringArgument(Pointer input);
88+
}
89+
90+
private interface TestLib extends Library {
91+
public String returnStringArgument(Pointer input);
92+
}
93+
94+
}

0 commit comments

Comments
 (0)