Skip to content

Exception chaining for com.sun.jna.Structure #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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