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

TypeMapper bug in direct-mapped library #467

Closed
twall opened this issue Jul 15, 2015 · 0 comments
Closed

TypeMapper bug in direct-mapped library #467

twall opened this issue Jul 15, 2015 · 0 comments

Comments

@twall
Copy link
Contributor

twall commented Jul 15, 2015

Sample failing test case:

public static enum Enumeration {
    STATUS_0(0), STATUS_1(1), STATUS_ERROR(-1);
    private final int code;
    Enumeration(int code) { this.code = code; }
    public int getCode() { return code; }
    public static Enumeration fromCode(int code) {
        switch(code) {
        case 0: return STATUS_0;
        case 1: return STATUS_1;
        default: return STATUS_ERROR;
        }
    }
}
public static class DirectTypeMappedEnumerationTestLibrary {
    public native Enumeration returnInt32Argument(Enumeration e);
    static {
        DefaultTypeMapper mapper = new DefaultTypeMapper();
        mapper.addTypeConverter(Enumeration.class, new TypeConverter() {
            public Object toNative(Object arg, ToNativeContext ctx) {
                return new Integer(((Enumeration)arg).getCode());
            }
            public Object fromNative(Object value, FromNativeContext context) {
                return Enumeration.fromCode(((Integer)value).intValue());
            }
            public Class nativeType() {
                return Integer.class;
            }
        });
        Map options = new HashMap();
        options.put(Library.OPTION_TYPE_MAPPER, mapper);

        Native.register(NativeLibrary.getInstance("testlib", options));
    }
}
public void testEnumerationConversion() {
    DirectTypeMappedEnumerationTestLibrary lib = new DirectTypeMappedEnumerationTestLibrary();
    Enumeration e = lib.returnInt32Argument(Enumeration.STATUS_1);
    assertEquals("Failed to convert enumeration", Enumeration.STATUS_1, e);
}
java -cp build/classes:build/test-classes:lib/junit.jar -Djna.library.path=build/native-darwin com.sun.jna.DirectTypeMapperTest
..E.....
Time: 0.06
There was 1 error:
1) testEnumerationConversion(com.sun.jna.DirectTypeMapperTest)java.lang.ExceptionInInitializerError
    at com.sun.jna.DirectTypeMapperTest.testEnumerationConversion(DirectTypeMapperTest.java:225)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at com.sun.jna.DirectTypeMapperTest.main(DirectTypeMapperTest.java:231)
Caused by: java.lang.IllegalArgumentException: Unsupported Structure field type class com.sun.jna.DirectTypeMapperTest$Enumeration
    at com.sun.jna.Structure$FFIType.get(Structure.java:1843)
    at com.sun.jna.Structure$FFIType.get(Structure.java:1798)
    at com.sun.jna.Native.register(Native.java:1447)
    at com.sun.jna.Native.register(Native.java:1175)
    at com.sun.jna.DirectTypeMapperTest$DirectTypeMappedEnumerationTestLibrary.<clinit>(DirectTypeMapperTest.java:221)
    ... 18 more

FAILURES!!!
Tests run: 7,  Failures: 0,  Errors: 1
twall added a commit that referenced this issue Jul 16, 2015
Add changelog entry, fixes #467
@twall twall closed this as completed in 8815d56 Jul 16, 2015
twall added a commit that referenced this issue Jul 16, 2015
Fixes #467, enum type mapping for direct-mapped libraries.   Should also improve type mapper lookups for direct-mapped libraries.
mstyura pushed a commit to mstyura/jna that referenced this issue Sep 9, 2024
Motivation:

We should use the latest java release on the CI

Modifications:

Upgrade to 8.0.362-zulu

Result:

Test with latest JDK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant