Skip to content

Commit

Permalink
fixes #129
Browse files Browse the repository at this point in the history
  • Loading branch information
twall committed Oct 5, 2012
1 parent da4d941 commit eaff9bd
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
=============
Expand Down
2 changes: 1 addition & 1 deletion src/com/sun/jna/Native.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/com/sun/jna/Pointer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/com/sun/jna/PointerType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() + ")";
}
}
5 changes: 4 additions & 1 deletion src/com/sun/jna/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions test/com/sun/jna/StructureTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit eaff9bd

Please sign in to comment.