From eaff9bd176d0c08789197ddbdbee81090ea9ecb4 Mon Sep 17 00:00:00 2001 From: Timothy Wall Date: Fri, 5 Oct 2012 06:58:14 -0400 Subject: [PATCH] fixes #129 --- CHANGES.md | 2 ++ src/com/sun/jna/Native.java | 2 +- src/com/sun/jna/Pointer.java | 3 +++ src/com/sun/jna/PointerType.java | 2 +- src/com/sun/jna/Structure.java | 5 ++++- test/com/sun/jna/StructureTest.java | 12 ++++++++++++ 6 files changed, 23 insertions(+), 3 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 70fdd7da48..6c64679859 100755 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,8 @@ Bug Fixes * Fix crash in direct mode callbacks with certain type conversions - [@twall](https://github.com/twall). * More thoroughly propagate unexpected exceptions generated in jnidispatch - [@twall](https://github.com/twall). * Cleanup maven poms and publishing to central repo - [@bhamail](https://github.com/bhamail). +* [#129](https://github.com/twall/jna/issues/129): Allow `Memory` field in structure - [@twall](https://github.com/twall). +* Preserve `PointerType` fields on `Structure.read()` if unchanged - [@twall](https://github.com/twall). Release 3.4.2 ============= diff --git a/src/com/sun/jna/Native.java b/src/com/sun/jna/Native.java index e38f20b478..bf209f484b 100644 --- a/src/com/sun/jna/Native.java +++ b/src/com/sun/jna/Native.java @@ -1025,7 +1025,7 @@ public static int getNativeSize(Class cls) { return POINTER_SIZE; } throw new IllegalArgumentException("Native size for type \"" + cls.getName() - + "\" is unknown"); + + "\" is unknown"); } /** Indicate whether the given class is supported as a native argument diff --git a/src/com/sun/jna/Pointer.java b/src/com/sun/jna/Pointer.java index 4fa1523030..c672d72fab 100644 --- a/src/com/sun/jna/Pointer.java +++ b/src/com/sun/jna/Pointer.java @@ -452,6 +452,9 @@ else if (NativeMapped.class.isAssignableFrom(type)) { if (nm != null) { Object value = getValue(offset, nm.nativeType(), null); result = nm.fromNative(value, new FromNativeContext(type)); + if (nm.equals(result)) { + result = nm; + } } else { NativeMappedConverter tc = NativeMappedConverter.getInstance(type); diff --git a/src/com/sun/jna/PointerType.java b/src/com/sun/jna/PointerType.java index b04b5cd928..f74ef9352c 100644 --- a/src/com/sun/jna/PointerType.java +++ b/src/com/sun/jna/PointerType.java @@ -98,6 +98,6 @@ public boolean equals(Object o) { } public String toString() { - return pointer == null ? "NULL" : pointer.toString(); + return pointer == null ? "NULL" : pointer.toString() + " (" + super.toString() + ")"; } } diff --git a/src/com/sun/jna/Structure.java b/src/com/sun/jna/Structure.java index 4476906eae..12a25f5a98 100644 --- a/src/com/sun/jna/Structure.java +++ b/src/com/sun/jna/Structure.java @@ -626,6 +626,9 @@ Object readField(StructField structField) { Object result = memory.getValue(offset, fieldType, currentValue); if (readConverter != null) { result = readConverter.fromNative(result, structField.context); + if (currentValue != null && currentValue.equals(result)) { + result = currentValue; + } } if (fieldType.equals(String.class) @@ -1193,7 +1196,7 @@ protected int getNativeAlignment(Class type, Object value, boolean isFirstElemen || Float.class == type || Double.class == type) { alignment = size; } - else if (Pointer.class == type + else if ((Pointer.class.isAssignableFrom(type) && !Function.class.isAssignableFrom(type)) || (Platform.HAS_BUFFERS && Buffer.class.isAssignableFrom(type)) || Callback.class.isAssignableFrom(type) || WString.class == type diff --git a/test/com/sun/jna/StructureTest.java b/test/com/sun/jna/StructureTest.java index 6fd7e446ec..4508ea20ec 100644 --- a/test/com/sun/jna/StructureTest.java +++ b/test/com/sun/jna/StructureTest.java @@ -553,6 +553,16 @@ protected List getFieldOrder() { } } + public void testMemoryField() { + class MemoryFieldStructure extends Structure { + public Memory m; + protected List getFieldOrder() { + return Arrays.asList(new String[] { "m" }); + } + } + new MemoryFieldStructure().size(); + } + public void testDisallowFunctionPointerAsField() { class BadFieldStructure extends Structure { public Function cb; @@ -785,9 +795,11 @@ protected List getFieldOrder() { } TestStructure s = new TestStructure(); final Pointer p = s.p; + final TestPointer p2 = s.p2; s.write(); s.read(); assertSame("Should preserve Pointer references if peer unchanged", p, s.p); + assertSame("Should preserve PointerType references if peer unchanged", p2, s.p2); } public void testPreserveStringFields() {