From 720a0e0af294e3611b464caf301f69e0b48dbf41 Mon Sep 17 00:00:00 2001 From: Sebastian Staudt Date: Tue, 14 Aug 2018 18:06:16 +0200 Subject: [PATCH 1/3] Allow enums to implement NativeMapped This will allow enums to be used inside structures. --- src/com/sun/jna/NativeMappedConverter.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/com/sun/jna/NativeMappedConverter.java b/src/com/sun/jna/NativeMappedConverter.java index c2cd5e5789..d91baa6973 100644 --- a/src/com/sun/jna/NativeMappedConverter.java +++ b/src/com/sun/jna/NativeMappedConverter.java @@ -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) { From 21f2b3175493b58edd42f9e2cab6d3bcddea672c Mon Sep 17 00:00:00 2001 From: Sebastian Staudt Date: Thu, 30 Aug 2018 06:45:22 +0200 Subject: [PATCH 2/3] Add tests for NativeMappedConverter#defaultValue --- test/com/sun/jna/NativeMappedTestClass.java | 26 +++++++++++++ .../sun/jna/NativedMappedConverterTest.java | 37 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 test/com/sun/jna/NativeMappedTestClass.java create mode 100644 test/com/sun/jna/NativedMappedConverterTest.java diff --git a/test/com/sun/jna/NativeMappedTestClass.java b/test/com/sun/jna/NativeMappedTestClass.java new file mode 100644 index 0000000000..74e12f9016 --- /dev/null +++ b/test/com/sun/jna/NativeMappedTestClass.java @@ -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; + } +} diff --git a/test/com/sun/jna/NativedMappedConverterTest.java b/test/com/sun/jna/NativedMappedConverterTest.java new file mode 100644 index 0000000000..0f72b2fd13 --- /dev/null +++ b/test/com/sun/jna/NativedMappedConverterTest.java @@ -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; + } + } + +} From 3986a3b7922b9e13655f319d4fc1bcef16d66219 Mon Sep 17 00:00:00 2001 From: Sebastian Staudt Date: Fri, 31 Aug 2018 06:13:37 +0200 Subject: [PATCH 3/3] Update CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 5af6d5f5ae..47de6dc1c0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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).