Skip to content

Commit

Permalink
Direct Mapping support for OPTION_ALLOW_OBJECTS.
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed May 18, 2017
1 parent 3a5787b commit 86c21b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions native/dispatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
11 changes: 7 additions & 4 deletions src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -1657,6 +1658,7 @@ public static void register(Class<?> cls, NativeLibrary lib) {
List<Method> mlist = new ArrayList<Method>();
Map<String, ?> 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) {
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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 + ")");
Expand Down
18 changes: 14 additions & 4 deletions test/com/sun/jna/DirectReturnTypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.sun.jna;

import java.util.Map;
import java.util.Collections;

/** Exercise a range of native methods.
*
Expand Down Expand Up @@ -96,8 +97,13 @@ protected void setUp() {
}

public static class DirectObjectTestLibrary extends DirectTestLibrary {
public DirectObjectTestLibrary(Map<String, ?> 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)));
}
}

Expand All @@ -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
Expand Down

0 comments on commit 86c21b8

Please sign in to comment.