Skip to content

Commit

Permalink
Allow enums to implement NativeMapped
Browse files Browse the repository at this point in the history
This will allow enums to be used inside structures.
  • Loading branch information
koraktor authored and matthiasblaesing committed Sep 1, 2018
1 parent eb4830a commit 155d8ae
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,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;
}
}

}

0 comments on commit 155d8ae

Please sign in to comment.