Skip to content

Commit eaff9bd

Browse files
committed
fixes #129
1 parent da4d941 commit eaff9bd

File tree

6 files changed

+23
-3
lines changed

6 files changed

+23
-3
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Bug Fixes
1717
* Fix crash in direct mode callbacks with certain type conversions - [@twall](https://github.com/twall).
1818
* More thoroughly propagate unexpected exceptions generated in jnidispatch - [@twall](https://github.com/twall).
1919
* Cleanup maven poms and publishing to central repo - [@bhamail](https://github.com/bhamail).
20+
* [#129](https://github.com/twall/jna/issues/129): Allow `Memory` field in structure - [@twall](https://github.com/twall).
21+
* Preserve `PointerType` fields on `Structure.read()` if unchanged - [@twall](https://github.com/twall).
2022

2123
Release 3.4.2
2224
=============

src/com/sun/jna/Native.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ public static int getNativeSize(Class cls) {
10251025
return POINTER_SIZE;
10261026
}
10271027
throw new IllegalArgumentException("Native size for type \"" + cls.getName()
1028-
+ "\" is unknown");
1028+
+ "\" is unknown");
10291029
}
10301030

10311031
/** Indicate whether the given class is supported as a native argument

src/com/sun/jna/Pointer.java

+3
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,9 @@ else if (NativeMapped.class.isAssignableFrom(type)) {
452452
if (nm != null) {
453453
Object value = getValue(offset, nm.nativeType(), null);
454454
result = nm.fromNative(value, new FromNativeContext(type));
455+
if (nm.equals(result)) {
456+
result = nm;
457+
}
455458
}
456459
else {
457460
NativeMappedConverter tc = NativeMappedConverter.getInstance(type);

src/com/sun/jna/PointerType.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,6 @@ public boolean equals(Object o) {
9898
}
9999

100100
public String toString() {
101-
return pointer == null ? "NULL" : pointer.toString();
101+
return pointer == null ? "NULL" : pointer.toString() + " (" + super.toString() + ")";
102102
}
103103
}

src/com/sun/jna/Structure.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,9 @@ Object readField(StructField structField) {
626626
Object result = memory.getValue(offset, fieldType, currentValue);
627627
if (readConverter != null) {
628628
result = readConverter.fromNative(result, structField.context);
629+
if (currentValue != null && currentValue.equals(result)) {
630+
result = currentValue;
631+
}
629632
}
630633

631634
if (fieldType.equals(String.class)
@@ -1193,7 +1196,7 @@ protected int getNativeAlignment(Class type, Object value, boolean isFirstElemen
11931196
|| Float.class == type || Double.class == type) {
11941197
alignment = size;
11951198
}
1196-
else if (Pointer.class == type
1199+
else if ((Pointer.class.isAssignableFrom(type) && !Function.class.isAssignableFrom(type))
11971200
|| (Platform.HAS_BUFFERS && Buffer.class.isAssignableFrom(type))
11981201
|| Callback.class.isAssignableFrom(type)
11991202
|| WString.class == type

test/com/sun/jna/StructureTest.java

+12
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,16 @@ protected List getFieldOrder() {
553553
}
554554
}
555555

556+
public void testMemoryField() {
557+
class MemoryFieldStructure extends Structure {
558+
public Memory m;
559+
protected List getFieldOrder() {
560+
return Arrays.asList(new String[] { "m" });
561+
}
562+
}
563+
new MemoryFieldStructure().size();
564+
}
565+
556566
public void testDisallowFunctionPointerAsField() {
557567
class BadFieldStructure extends Structure {
558568
public Function cb;
@@ -785,9 +795,11 @@ protected List getFieldOrder() {
785795
}
786796
TestStructure s = new TestStructure();
787797
final Pointer p = s.p;
798+
final TestPointer p2 = s.p2;
788799
s.write();
789800
s.read();
790801
assertSame("Should preserve Pointer references if peer unchanged", p, s.p);
802+
assertSame("Should preserve PointerType references if peer unchanged", p2, s.p2);
791803
}
792804

793805
public void testPreserveStringFields() {

0 commit comments

Comments
 (0)