From 86c21b8e971bbc8b3b744837bd0fad9ee30fd6f4 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Thu, 18 May 2017 03:34:51 +0100 Subject: [PATCH] Direct Mapping support for OPTION_ALLOW_OBJECTS. --- native/dispatch.h | 1 + src/com/sun/jna/Native.java | 11 +++++++---- test/com/sun/jna/DirectReturnTypesTest.java | 18 ++++++++++++++---- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/native/dispatch.h b/native/dispatch.h index ceb1813bf5..91ddd27fb4 100644 --- a/native/dispatch.h +++ b/native/dispatch.h @@ -115,6 +115,7 @@ enum { CVT_TYPE_MAPPER = com_sun_jna_Native_CVT_TYPE_MAPPER, CVT_TYPE_MAPPER_STRING = com_sun_jna_Native_CVT_TYPE_MAPPER_STRING, CVT_TYPE_MAPPER_WSTRING = com_sun_jna_Native_CVT_TYPE_MAPPER_WSTRING, + CVT_OBJECT = com_sun_jna_Native_CVT_OBJECT, }; /* callback behavior flags */ diff --git a/src/com/sun/jna/Native.java b/src/com/sun/jna/Native.java index 274bb78fa0..cc28856157 100644 --- a/src/com/sun/jna/Native.java +++ b/src/com/sun/jna/Native.java @@ -1534,8 +1534,9 @@ static String replace(String s1, String s2, String str) { private static final int CVT_TYPE_MAPPER = 23; private static final int CVT_TYPE_MAPPER_STRING = 24; private static final int CVT_TYPE_MAPPER_WSTRING = 25; + private static final int CVT_OBJECT = 26; - private static int getConversion(Class type, TypeMapper mapper) { + private static int getConversion(Class type, TypeMapper mapper, boolean allowObjects) { if (type == Boolean.class) type = boolean.class; else if (type == Byte.class) type = byte.class; else if (type == Short.class) type = short.class; @@ -1624,7 +1625,7 @@ private static int getConversion(Class type, TypeMapper mapper) { } return CVT_NATIVE_MAPPED; } - return CVT_UNSUPPORTED; + return allowObjects ? CVT_OBJECT : CVT_UNSUPPORTED; } /** @@ -1657,6 +1658,7 @@ public static void register(Class cls, NativeLibrary lib) { List mlist = new ArrayList(); Map options = lib.getOptions(); TypeMapper mapper = (TypeMapper) options.get(Library.OPTION_TYPE_MAPPER); + boolean allowObjects = Boolean.TRUE.equals(options.get(Library.OPTION_ALLOW_OBJECTS)); options = cacheOptions(cls, options, null); for (Method m : methods) { @@ -1677,7 +1679,7 @@ public static void register(Class cls, NativeLibrary lib) { int[] cvt = new int[ptypes.length]; ToNativeConverter[] toNative = new ToNativeConverter[ptypes.length]; FromNativeConverter fromNative = null; - int rcvt = getConversion(rclass, mapper); + int rcvt = getConversion(rclass, mapper, allowObjects); boolean throwLastError = false; switch (rcvt) { case CVT_UNSUPPORTED: @@ -1701,6 +1703,7 @@ public static void register(Class cls, NativeLibrary lib) { rtype = FFIType.get(NativeMappedConverter.getInstance(rclass).nativeType()).peer; break; case CVT_STRUCTURE: + case CVT_OBJECT: closure_rtype = rtype = FFIType.get(Pointer.class).peer; break; case CVT_STRUCTURE_BYVAL: @@ -1714,7 +1717,7 @@ public static void register(Class cls, NativeLibrary lib) { for (int t=0;t < ptypes.length;t++) { Class type = ptypes[t]; sig += getSignature(type); - int conversionType = getConversion(type, mapper); + int conversionType = getConversion(type, mapper, allowObjects); cvt[t] = conversionType; if (conversionType == CVT_UNSUPPORTED) { throw new IllegalArgumentException(type + " is not a supported argument type (in method " + method.getName() + " in " + cls + ")"); diff --git a/test/com/sun/jna/DirectReturnTypesTest.java b/test/com/sun/jna/DirectReturnTypesTest.java index 087471b1b7..afe0778018 100644 --- a/test/com/sun/jna/DirectReturnTypesTest.java +++ b/test/com/sun/jna/DirectReturnTypesTest.java @@ -24,6 +24,7 @@ package com.sun.jna; import java.util.Map; +import java.util.Collections; /** Exercise a range of native methods. * @@ -96,8 +97,13 @@ protected void setUp() { } public static class DirectObjectTestLibrary extends DirectTestLibrary { - public DirectObjectTestLibrary(Map options) { - Native.register(getClass(), NativeLibrary.getInstance("testlib", options)); + @Override + public native Object returnObjectArgument(Object s); + @Override + public native TestObject returnObjectArgument(TestObject s); + public DirectObjectTestLibrary() { + Native.register(getClass(), NativeLibrary.getInstance("testlib", + Collections.singletonMap(Library.OPTION_ALLOW_OBJECTS, Boolean.TRUE))); } } @@ -117,9 +123,13 @@ protected NativeMappedLibrary loadNativeMappedLibrary() { return new DirectNativeMappedLibrary(); } - // Override not-yet-supported tests + @Override - public void testReturnObject() { } + public void testReturnObject() { + lib = new DirectObjectTestLibrary(); + } + + // Override not-yet-supported tests @Override public void testReturnPointerArray() { } @Override