fixed direct-mapping type-mapped pointer result types

Esse commit está contido em:
Taco
2014-04-08 18:23:44 +02:00
commit d7bb1e189f
4 arquivos alterados com 56 adições e 1 exclusões
+3
Ver Arquivo
@@ -1150,6 +1150,9 @@ fromNativeTypeMapped(JNIEnv* env, jobject from_native, void* resp, ffi_type* typ
if (type->type != FFI_TYPE_POINTER) {
extract_value(env, obj, result, type->size, JNI_TRUE);
}
else {
*(jobject*)result = obj;
}
}
}
}
+8
Ver Arquivo
@@ -158,6 +158,14 @@ returnInt32Argument(int32_t arg) {
return arg;
}
EXPORT int*
returnPoint(int x, int y) {
int *p = malloc(2 * sizeof(int));
p[0] = x;
p[1] = y;
return p;
}
EXPORT int64_t
returnInt64Zero() {
int64_t value = 0;
+1 -1
Ver Arquivo
@@ -117,7 +117,7 @@ public class DefaultTypeMapper implements TypeMapper {
/** Add a {@link TypeConverter} to provide bidirectional mapping between
* a native and Java type.
*/
protected void addTypeConverter(Class cls, TypeConverter converter) {
public void addTypeConverter(Class cls, TypeConverter converter) {
addFromNativeConverter(cls, converter);
addToNativeConverter(cls, converter);
}
+44
Ver Arquivo
@@ -13,10 +13,12 @@
package com.sun.jna;
import java.awt.Point;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
@@ -156,6 +158,48 @@ public class DirectTypeMapperTest extends TestCase {
assertFalse("Failed to convert integer return to boolean FALSE",
lib.returnInt32Argument(false));
}
public static class DirectTypeMappedResultTypeTestLibrary {
public native Point returnPoint(int x, int y);
static {
Map options = new HashMap();
DefaultTypeMapper mapper = new DefaultTypeMapper();
mapper.addTypeConverter(Point.class, new TypeConverter() {
public Object fromNative(Object value, FromNativeContext context) {
Pointer p = (Pointer) value;
int x = p.getInt(0), y = p.getInt(4);
Native.free(Pointer.nativeValue(p));
return new Point(x, y);
}
public Object toNative(Object value, ToNativeContext context) {
return Pointer.NULL; // dummy implementation (not called)
}
public Class nativeType() {
return Pointer.class;
}
});
options.put(Library.OPTION_TYPE_MAPPER, mapper);
// Can't extend java.awt.Point; can't add:
// public final static TypeMapper TYPE_MAPPER = mapper;
// -> Extend Native.options via reflection:
try {
Field f = Native.class.getDeclaredField("options");
f.setAccessible(true);
((Map) f.get(null)).put(Point.class, options);
}
catch (Exception e) {
throw new RuntimeException(e);
}
Native.register(NativeLibrary.getInstance("testlib", options));
}
}
public void testTypeMapperResultTypeConversion() throws Exception {
DirectTypeMappedResultTypeTestLibrary lib = new DirectTypeMappedResultTypeTestLibrary();
Point p = lib.returnPoint(1234, 5678);
assertEquals("Failed to convert int* return to java.awt.Point", 1234, p.x);
assertEquals("Failed to convert int* return to java.awt.Point", 5678, p.y);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(DirectTypeMapperTest.class);