Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix enum custom converters #137

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/main/java/jnr/ffi/provider/jffi/AsmLibraryLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ private <T> T generateInterfaceImpl(final NativeLibrary library, Class<T> interf
typeMapper = new NullTypeMapper();
}

CompositeTypeMapper closureTypeMapper = new CompositeTypeMapper(typeMapper,
new CachingTypeMapper(new InvokerTypeMapper(null, classLoader, NativeLibraryLoader.ASM_ENABLED)),
new CachingTypeMapper(new AnnotationTypeMapper()));
typeMapper = new CompositeTypeMapper(typeMapper,
new CachingTypeMapper(new InvokerTypeMapper(new NativeClosureManager(runtime, closureTypeMapper, classLoader), classLoader, NativeLibraryLoader.ASM_ENABLED)),
new CachingTypeMapper(new AnnotationTypeMapper()));
CompositeTypeMapper closureTypeMapper = new CompositeTypeMapper(typeMapper,
new CachingTypeMapper(new AnnotationTypeMapper()),
new CachingTypeMapper(new InvokerTypeMapper(null, classLoader, NativeLibraryLoader.ASM_ENABLED)));

typeMapper = new CompositeTypeMapper(typeMapper,
new CachingTypeMapper(new AnnotationTypeMapper()),
new CachingTypeMapper(new InvokerTypeMapper(new NativeClosureManager(runtime, closureTypeMapper, classLoader), classLoader, NativeLibraryLoader.ASM_ENABLED)));

CallingConvention libraryCallingConvention = getCallingConvention(interfaceClass, libraryOptions);

StubCompiler compiler = StubCompiler.newCompiler(runtime);
Expand Down
50 changes: 49 additions & 1 deletion src/test/java/jnr/ffi/mapper/AnnotatedMappedTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@
import jnr.ffi.annotations.In;
import jnr.ffi.annotations.LongLong;
import jnr.ffi.annotations.Out;
import jnr.ffi.provider.converters.CharSequenceParameterConverter;
import jnr.ffi.provider.converters.EnumSetConverter;
import jnr.ffi.provider.converters.StringResultConverter;
import jnr.ffi.types.size_t;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import java.nio.ByteBuffer;

import static junit.framework.TestCase.*;

public class AnnotatedMappedTypeTest {
Expand All @@ -50,10 +54,46 @@ public static CustomPointer fromNative(Pointer value, FromNativeContext context)
return value != null ? new CustomPointer(value) : null;
}
}


public enum CustomEnum {
FIRST("FIRST"),
SECOND("SECOND");

@ToNativeConverter.ToNative(nativeType = ByteBuffer.class)
public static ByteBuffer toNative(CustomEnum value, ToNativeContext context) {
if (value != null) {
ByteBuffer byteBuffer = CharSequenceParameterConverter.getInstance(context)
.toNative(value.text, context);
return byteBuffer;
} else {
return null;
}
}

@FromNativeConverter.FromNative(nativeType = Pointer.class)
public static CustomEnum fromNative(Pointer value, FromNativeContext context) {
if (value != null) {
String fromNative = StringResultConverter.getInstance(context)
.fromNative(value, context);
for (CustomEnum customEnum : CustomEnum.values()) {
if (customEnum.text.equals(fromNative)) {
return customEnum;
}
}
}
return null;
}

private final String text;

CustomEnum(String text) {this.text = text;}
}

public static interface TestLib {
CustomPointer ptr_malloc(@size_t int size);
void ptr_free(CustomPointer ptr);
boolean string_equals(CustomEnum s1, String s2);
CustomEnum string_duplicate(CustomEnum s1);
}

static TestLib testlib;
Expand All @@ -72,4 +112,12 @@ public static void setUpClass() throws Exception {
@Test public void toNative() {
testlib.ptr_free(testlib.ptr_malloc(1));
}


@Test public void enumToNative() {
assertTrue(testlib.string_equals(CustomEnum.FIRST, CustomEnum.FIRST.text));
}
@Test public void enumFromNative() {
assertEquals(CustomEnum.SECOND,testlib.string_duplicate(CustomEnum.SECOND));
}
}