/
DirectTest.java
349 lines (317 loc) · 13.4 KB
/
DirectTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/* Copyright (c) 2009 Timothy Wall, All Rights Reserved
*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna;
import junit.framework.*;
import java.lang.reflect.Method;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//@SuppressWarnings("unused")
public class DirectTest extends TestCase implements Paths {
public static void main(java.lang.String[] argList) {
junit.textui.TestRunner.run(DirectTest.class);
}
static class MathLibrary {
public static native double cos(double x);
static {
Native.register(Platform.MATH_LIBRARY_NAME);
}
}
interface MathInterface extends Library {
double cos(double x);
}
static class CLibrary {
public static class size_t extends IntegerType {
private static final long serialVersionUID = 1L;
public size_t() {
super(Native.POINTER_SIZE);
}
public size_t(long value) {
super(Native.POINTER_SIZE, value);
}
}
public static native Pointer memset(Pointer p, int v, size_t len);
public static native Pointer memset(Pointer p, int v, int len);
public static native Pointer memset(Pointer p, int v, long len);
public static native long memset(long p, int v, long len);
public static native int memset(int p, int v, int len);
public static native int strlen(String s1);
public static native int strlen(Pointer p);
public static native int strlen(byte[] b);
static {
Native.register(Platform.C_LIBRARY_NAME);
}
}
static interface CInterface extends Library {
Pointer memset(Pointer p, int v, int len);
int strlen(String s);
}
static interface TestInterface extends Library {
interface Int32Callback extends Callback {
int invoke(int arg1, int arg2);
}
interface NativeLongCallback extends Callback {
NativeLong invoke(NativeLong arg1, NativeLong arg2);
}
int callInt32CallbackRepeatedly(Int32Callback cb, int arg1, int arg2, int count);
NativeLong callLongCallbackRepeatedly(NativeLongCallback cb, NativeLong arg1, NativeLong arg2, int count);
}
static class TestLibrary implements TestInterface {
@Override
public native int callInt32CallbackRepeatedly(Int32Callback cb, int arg1, int arg2, int count);
@Override
public native NativeLong callLongCallbackRepeatedly(NativeLongCallback cb, NativeLong arg1, NativeLong arg2, int count);
static {
Native.register("testlib");
}
}
private static class TestLoader extends URLClassLoader {
public TestLoader() throws MalformedURLException {
this(null);
}
public TestLoader(ClassLoader parent) throws MalformedURLException {
super(Platform.isWindowsCE()
? new URL[] {
new File("/Storage Card/test.jar").toURI().toURL()
}
: new URL[] {
new File(BUILDDIR + "/classes").toURI().toURL(),
new File(BUILDDIR + "/test-classes").toURI().toURL(),
}, new CloverLoader(parent));
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String boot = System.getProperty("jna.boot.library.path");
if (boot != null) {
System.setProperty("jna.boot.library.path", "");
}
Class<?> cls = super.findClass(name);
if (boot != null) {
System.setProperty("jna.boot.library.path", boot);
}
return cls;
}
}
public void testRegisterMethods() throws Exception {
if (Platform.isAIX()) {
// https://stackoverflow.com/questions/8961441/java-system-loadlibrarym-fails-on-aix-6-1
System.out.println("Skip " + getName() + " - libm can't be dynamically linked on AIX");
return;
}
assertEquals("Math library call failed", 1., MathLibrary.cos(0), .01);
assertTrue("Library not registered",
Native.registered(MathLibrary.class));
Native.unregister(MathLibrary.class);
assertFalse("Registered methods not unregistered",
Native.registered(MathLibrary.class));
}
private Class<?> returnCallingClass() {
return Native.getCallingClass();
}
public void testFindCallingClass() {
assertEquals("Wrong calling class detected",
getClass(), returnCallingClass());
}
public void testFindNativeClass() {
class UnregisterLibrary {
class Inner {
public Class<?> findDirectMappedClass() {
return findDirectMappedClassInner();
}
public Class<?> findDirectMappedClassInner() {
return Native.findDirectMappedClass(Native.getCallingClass());
};
}
public native double cos(double x);
public Class<?> findDirectMappedClass() {
return new Inner().findDirectMappedClass();
};
}
assertEquals("Wrong native class found",
UnregisterLibrary.class, new UnregisterLibrary().findDirectMappedClass());
}
public static class DirectMapping {
public static class DirectStructure extends Structure {
public int field;
@Override
protected List<String> getFieldOrder() {
return Arrays.asList("field");
}
}
public static interface DirectCallback extends Callback {
void invoke();
}
public DirectMapping(Map<String, ?> options) {
Native.register(getClass(), NativeLibrary.getInstance("testlib", options));
}
}
public void testGetOptionsForDirectMappingWithMemberInitializer() {
Class<?>[] classes = {
DirectMapping.class,
DirectMapping.DirectStructure.class,
DirectMapping.DirectCallback.class,
};
final TypeMapper mapper = new DefaultTypeMapper();
final int alignment = Structure.ALIGN_NONE;
final String encoding = System.getProperty("file.encoding");
Map<String, Object> options = new HashMap<String, Object>();
options.put(Library.OPTION_TYPE_MAPPER, mapper);
options.put(Library.OPTION_STRUCTURE_ALIGNMENT, alignment);
options.put(Library.OPTION_STRING_ENCODING, encoding);
DirectMapping lib = new DirectMapping(options);
for (Class<?> cls : classes) {
assertEquals("Wrong type mapper for direct mapping " + cls,
mapper, Native.getTypeMapper(cls));
assertEquals("Wrong alignment for direct mapping " + cls,
alignment, Native.getStructureAlignment(cls));
assertEquals("Wrong encoding for direct mapping " + cls,
encoding, Native.getStringEncoding(cls));
Object last = Native.getLibraryOptions(cls);
assertSame("Options not cached", last, Native.getLibraryOptions(cls));
}
}
public static class DirectMappingStatic {
final static TypeMapper TEST_MAPPER = new DefaultTypeMapper();
final static int TEST_ALIGNMENT = Structure.ALIGN_DEFAULT;
final static String TEST_ENCODING = System.getProperty("file.encoding");
final static Map<String, Object> TEST_OPTIONS = new HashMap<String, Object>() {
private static final long serialVersionUID = 1L; // we're not serializing it
{
put(Library.OPTION_TYPE_MAPPER, TEST_MAPPER);
put(Library.OPTION_STRUCTURE_ALIGNMENT, TEST_ALIGNMENT);
put(Library.OPTION_STRING_ENCODING, TEST_ENCODING);
}
};
static {
Native.register(DirectMappingStatic.class, NativeLibrary.getInstance("testlib", TEST_OPTIONS));
}
public static class DirectStructure extends Structure {
public int field;
@Override
protected List<String> getFieldOrder() {
return Arrays.asList("field");
}
}
public static interface DirectCallback extends Callback {
void invoke();
}
}
public void testGetOptionsForDirectMappingWithStaticInitializer() {
Class<?>[] classes = {
DirectMappingStatic.class,
DirectMappingStatic.DirectStructure.class,
DirectMappingStatic.DirectCallback.class,
};
for (Class<?> cls : classes) {
assertEquals("Wrong type mapper for direct mapping " + cls,
DirectMappingStatic.TEST_MAPPER, Native.getTypeMapper(cls));
assertEquals("Wrong alignment for direct mapping " + cls,
DirectMappingStatic.TEST_ALIGNMENT, Native.getStructureAlignment(cls));
assertEquals("Wrong encoding for direct mapping " + cls,
DirectMappingStatic.TEST_ENCODING, Native.getStringEncoding(cls));
Object last = Native.getLibraryOptions(cls);
assertSame("Options not cached", last, Native.getLibraryOptions(cls));
}
}
static class RemappedCLibrary {
public static native int $$YJP$$strlen(String s);
public static native int _prefixed_strlen(String s);
}
public void testDirectMappingFunctionMapper() {
FunctionMapper MAPPER = new FunctionMapper() {
@Override
public String getFunctionName(NativeLibrary lib, Method method) {
String name = method.getName();
if (name.startsWith("_prefixed_")) {
return name.substring(10);
}
return name;
}
};
try {
Native.register(RemappedCLibrary.class,
NativeLibrary.getInstance(Platform.C_LIBRARY_NAME, Collections.singletonMap(Library.OPTION_FUNCTION_MAPPER, MAPPER)));
final String VALUE = getName();
int len;
len = RemappedCLibrary.$$YJP$$strlen(VALUE);
assertEquals("Mismatched YJP strlen value", VALUE.length(), len);
len = RemappedCLibrary._prefixed_strlen(VALUE);
assertEquals("Mismatched prefixed strlen value", VALUE.length(), len);
} catch(Exception e) {
fail("Native method was not properly mapped: " + e);
}
}
public static class PointerNativeMapped implements NativeMapped {
String nativeMethodName;
@Override
public PointerNativeMapped fromNative(Object nativeValue, FromNativeContext context) {
nativeMethodName = ((MethodResultContext)context).getMethod().getName();
return this;
}
@Override
public Object toNative() {
return null;
}
@Override
public Class<?> nativeType() {
return Pointer.class;
}
}
public static class PointerTypeMapped {
String nativeMethodName;
}
public static class FromNativeTests {
static native PointerNativeMapped returnPointerArgument(PointerNativeMapped arg);
static native PointerTypeMapped returnPointerArgument(PointerTypeMapped arg);
}
public void testDirectMappingFromNative() {
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addTypeConverter(PointerTypeMapped.class, new TypeConverter() {
@Override
public PointerTypeMapped fromNative(Object nativeValue, FromNativeContext context) {
PointerTypeMapped ret = new PointerTypeMapped();
ret.nativeMethodName = ((MethodResultContext)context).getMethod().getName();
return ret;
}
@Override
public Object toNative(Object value, ToNativeContext context) {
return null;
}
@Override
public Class<?> nativeType() {
return Pointer.class;
}
});
NativeLibrary lib = NativeLibrary.getInstance("testlib", Collections.singletonMap(Library.OPTION_TYPE_MAPPER, mapper));
Native.register(FromNativeTests.class, lib);
assertEquals("Failed to access MethodResultContext", "returnPointerArgument", FromNativeTests.returnPointerArgument(new PointerNativeMapped()).nativeMethodName);
assertEquals("Failed to access MethodResultContext", "returnPointerArgument", FromNativeTests.returnPointerArgument(new PointerTypeMapped()).nativeMethodName);
}
}