Skip to content

Commit

Permalink
Merge pull request #290 from ebourg/master
Browse files Browse the repository at this point in the history
Exception chaining for com.sun.jna.Structure
  • Loading branch information
twall committed Apr 10, 2014
2 parents 9340b94 + 32447ce commit 323a913
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Next Release
Features
--------
* Updated AIX natives and build - [@twall](https://github.com/twall).
* [#290](https://github.com/twall/jna/pull/290): Improved the stacktrace for the exceptions thrown by `com.sun.jna.Structure` - [@ebourg](https://github.com/ebourg).

Release 4.1
===========
Expand Down
56 changes: 24 additions & 32 deletions src/com/sun/jna/Structure.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void useMemory(Pointer m, int offset, boolean force) {
this.readCalled = false;
}
catch(IndexOutOfBoundsException e) {
throw new IllegalArgumentException("Structure exceeds provided memory bounds");
throw new IllegalArgumentException("Structure exceeds provided memory bounds", e);
}
}

Expand All @@ -346,7 +346,7 @@ else if (size == CALCULATE_SIZE) {
this.memory = this.memory.share(0, this.size);
}
catch(IndexOutOfBoundsException e) {
throw new IllegalArgumentException("Structure exceeds provided memory bounds");
throw new IllegalArgumentException("Structure exceeds provided memory bounds", e);
}
}
}
Expand Down Expand Up @@ -575,9 +575,7 @@ Object getFieldValue(Field field) {
return field.get(this);
}
catch (Exception e) {
throw new Error("Exception reading field '"
+ field.getName() + "' in " + getClass()
+ ": " + e);
throw new Error("Exception reading field '" + field.getName() + "' in " + getClass(), e);
}
}

Expand All @@ -596,13 +594,11 @@ private void setFieldValue(Field field, Object value, boolean overrideFinal) {
if (overrideFinal) {
// WARNING: setAccessible(true) on J2ME does *not* allow
// overwriting of a final field.
throw new UnsupportedOperationException("This VM does not support Structures with final fields (field '" + field.getName() + "' within " + getClass() + ")");
throw new UnsupportedOperationException("This VM does not support Structures with final fields (field '" + field.getName() + "' within " + getClass() + ")", e);
}
throw new UnsupportedOperationException("Attempt to write to read-only field '" + field.getName() + "' within " + getClass());
throw new UnsupportedOperationException("Attempt to write to read-only field '" + field.getName() + "' within " + getClass(), e);
}
throw new Error("Unexpectedly unable to write to field '"
+ field.getName() + "' within " + getClass()
+ ": " + e);
throw new Error("Unexpectedly unable to write to field '" + field.getName() + "' within " + getClass(), e);
}
}

Expand Down Expand Up @@ -805,7 +801,7 @@ protected void writeField(StructField structField) {
+ (structField.type == fieldType
? "" : " (native type " + fieldType + ")")
+ ", which is not supported within a Structure";
throw new IllegalArgumentException(msg);
throw new IllegalArgumentException(msg, e);
}
}

Expand Down Expand Up @@ -1034,8 +1030,8 @@ private void validateField(String name, Class type) {
getNativeSize(type);
}
catch(IllegalArgumentException e) {
String msg = "Invalid Structure field in " + getClass() + ", field name '" + name + "' (" + type + "): " + e.getMessage();
throw new IllegalArgumentException(msg);
String msg = "Invalid Structure field in " + getClass() + ", field name '" + name + "' (" + type + ")";
throw new IllegalArgumentException(msg, e);
}
}
}
Expand Down Expand Up @@ -1154,8 +1150,8 @@ else if (writeConverter != null || readConverter != null) {
if (!force && typeMapper == null) {
return null;
}
String msg = "Invalid Structure field in " + getClass() + ", field name '" + structField.name + "' (" + structField.type + "): " + e.getMessage();
throw new IllegalArgumentException(msg);
String msg = "Invalid Structure field in " + getClass() + ", field name '" + structField.name + "' (" + structField.type + ")";
throw new IllegalArgumentException(msg, e);
}

// Align fields as appropriate
Expand Down Expand Up @@ -1217,9 +1213,7 @@ private void initializeFields() {
}
}
catch (Exception e) {
throw new Error("Exception reading field '"
+ f.getName() + "' in " + getClass()
+ ": " + e);
throw new Error("Exception reading field '" + f.getName() + "' in " + getClass(), e);
}
}
}
Expand All @@ -1233,9 +1227,8 @@ private Object initializeField(Field field, Class type) {
setFieldValue(field, value);
}
catch(IllegalArgumentException e) {
String msg = "Can't determine size of nested structure: "
+ e.getMessage();
throw new IllegalArgumentException(msg);
String msg = "Can't determine size of nested structure";
throw new IllegalArgumentException(msg, e);
}
}
else if (NativeMapped.class.isAssignableFrom(type)) {
Expand Down Expand Up @@ -1621,18 +1614,17 @@ public static Structure newInstance(Class type, Pointer init) throws IllegalArgu
// Might as well try the fallback
}
catch(InstantiationException e) {
String msg = "Can't instantiate " + type + " (" + e + ")";
throw new IllegalArgumentException(msg);
String msg = "Can't instantiate " + type;
throw new IllegalArgumentException(msg, e);
}
catch(IllegalAccessException e) {
String msg = "Instantiation of " + type
+ "(Pointer) not allowed, is it public? (" + e + ")";
throw new IllegalArgumentException(msg);
String msg = "Instantiation of " + type + " (Pointer) not allowed, is it public?";
throw new IllegalArgumentException(msg, e);
}
catch(InvocationTargetException e) {
String msg = "Exception thrown while instantiating an instance of " + type + " (" + e + ")";
String msg = "Exception thrown while instantiating an instance of " + type;
e.printStackTrace();
throw new IllegalArgumentException(msg);
throw new IllegalArgumentException(msg, e);
}
Structure s = newInstance(type);
if (init != PLACEHOLDER_MEMORY) {
Expand All @@ -1655,13 +1647,13 @@ public static Structure newInstance(Class type) throws IllegalArgumentException
return s;
}
catch(InstantiationException e) {
String msg = "Can't instantiate " + type + " (" + e + ")";
throw new IllegalArgumentException(msg);
String msg = "Can't instantiate " + type;
throw new IllegalArgumentException(msg, e);
}
catch(IllegalAccessException e) {
String msg = "Instantiation of " + type
+ " not allowed, is it public? (" + e + ")";
throw new IllegalArgumentException(msg);
+ " not allowed, is it public?";
throw new IllegalArgumentException(msg, e);
}
}

Expand Down

0 comments on commit 323a913

Please sign in to comment.