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

Allow enums to implement NativeMapped #1003

Closed
Closed
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Features

Bug Fixes
---------
* [#1003](https://github.com/java-native-access/jna/pull/1003): Allow `NativeMapped` to be used with enums - [@koraktor](https://github.com/koraktor)
* [#652](https://github.com/java-native-access/jna/issues/652): Dead Lock in class initialization - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#843](https://github.com/java-native-access/jna/pull/843): Correctly bind `com.sun.jna.platform.win32.SecBufferDesc` and add convenience binding as `com.sun.jna.platform.win32.SspiUtil.ManagedSecBufferDesc`. Bind SSPI functions `InitializeSecurityContext`, `AcceptSecurityContext`, `QueryCredentialsAttributes`, `QuerySecurityPackageInfo`, `EncryptMessage`, `DecryptMessage`, `MakeSignature`, `VerifySignature` in `com.sun.jna.platform.win32.Secur32` - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#863](https://github.com/java-native-access/jna/pull/863): Fix ARM softfloat/hardfloat detection by modifying armSoftFloat condition in ELFAnalyser. Before this fix a softfloat binary could be misdetected as hardfloat. - [@kunkun26](https://github.com/kunkun26).
Expand Down
4 changes: 4 additions & 0 deletions src/com/sun/jna/NativeMappedConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public NativeMappedConverter(Class<?> type) {
}

public NativeMapped defaultValue() {
if (type.isEnum()) {
return (NativeMapped) type.getEnumConstants()[0];
}

try {
return (NativeMapped)type.newInstance();
} catch (InstantiationException e) {
Expand Down
26 changes: 26 additions & 0 deletions test/com/sun/jna/NativeMappedTestClass.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.sun.jna;

class NativeMappedTestClass implements NativeMapped {

private String name;

public NativeMappedTestClass() {}

@Override
public Object fromNative(Object nativeValue, FromNativeContext context) {
NativeMappedTestClass object = new NativeMappedTestClass();
object.name = (String) nativeValue;

return object;
}

@Override
public Object toNative() {
return name;
}

@Override
public Class<?> nativeType() {
return String.class;
}
}
37 changes: 37 additions & 0 deletions test/com/sun/jna/NativedMappedConverterTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.sun.jna;

import junit.framework.TestCase;

public class NativedMappedConverterTest extends TestCase {

public void testDefaultValueForClass() {
NativeMappedConverter converter = new NativeMappedConverter(NativeMappedTestClass.class);

assertTrue(converter.defaultValue() instanceof NativeMappedTestClass);
}

public void testDefaultValueForEnum() {
NativeMappedConverter converter = new NativeMappedConverter(TestEnum.class);

assertSame(converter.defaultValue(), TestEnum.VALUE1);
}

private enum TestEnum implements NativeMapped { VALUE1, VALUE2;

@Override
public Object fromNative(Object nativeValue, FromNativeContext context) {
return values()[(Integer) nativeValue];
}

@Override
public Object toNative() {
return ordinal();
}

@Override
public Class<?> nativeType() {
return Integer.class;
}
}

}