From 27c7d3ad492e9b13d6361ac1f9e7f60a0d84b5c0 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Fri, 15 Nov 2013 22:54:20 +0100 Subject: [PATCH 1/2] Exception chaining for com.sun.jna.Structure --- src/com/sun/jna/Structure.java | 56 +++++++++++++++------------------- 1 file changed, 24 insertions(+), 32 deletions(-) diff --git a/src/com/sun/jna/Structure.java b/src/com/sun/jna/Structure.java index 9e5b56cf02..4a00d85f1f 100644 --- a/src/com/sun/jna/Structure.java +++ b/src/com/sun/jna/Structure.java @@ -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); } } @@ -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); } } } @@ -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); } } @@ -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); } } @@ -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); } } @@ -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); } } } @@ -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 @@ -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); } } } @@ -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)) { @@ -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) { @@ -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); } } From 32447ce4886daf5a3e1a3f37a9025ad6d2946139 Mon Sep 17 00:00:00 2001 From: Emmanuel Bourg Date: Wed, 9 Apr 2014 17:51:49 +0200 Subject: [PATCH 2/2] Added an entry in the changelog for #290 --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index aeee3b7c15..0fb961111a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -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 ===========