From 6bceb388eefa18085a467ecd9d96ba13009694c2 Mon Sep 17 00:00:00 2001 From: "Brian S. O'Neill" Date: Sun, 12 Aug 2007 16:02:10 +0000 Subject: Storable methods to access properties by name disallow access to methods which declare throwing checked exceptions. --- src/main/java/com/amazon/carbonado/Storable.java | 8 ++-- .../amazon/carbonado/gen/StorableGenerator.java | 47 +++++++++++++++++++++- 2 files changed, 50 insertions(+), 5 deletions(-) (limited to 'src/main/java/com/amazon/carbonado') diff --git a/src/main/java/com/amazon/carbonado/Storable.java b/src/main/java/com/amazon/carbonado/Storable.java index b3372c3..e745040 100644 --- a/src/main/java/com/amazon/carbonado/Storable.java +++ b/src/main/java/com/amazon/carbonado/Storable.java @@ -387,7 +387,8 @@ public interface Storable> { * * @param propertyName name of property to get value of * @return property value, which is boxed if property type is primitive - * @throws IllegalArgumentException if property is unknown + * @throws IllegalArgumentException if property is unknown or if accessor + * method declares throwing any checked exceptions * @throws UnsupportedOperationException if property is independent but unsupported * @throws NullPointerException if property name is null * @since 1.2 @@ -400,8 +401,9 @@ public interface Storable> { * * @param propertyName name of property to set value to * @param value new value for property - * @throws IllegalArgumentException if property is unknown or if value is - * unsupported due to a constraint + * @throws IllegalArgumentException if property is unknown, or if value is + * unsupported due to a constraint, or if mutator method declares throwing + * any checked exceptions * @throws UnsupportedOperationException if property is independent but unsupported * @throws ClassCastException if value is of wrong type * @throws NullPointerException if property name is null or if primitive diff --git a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java index 3319d5c..41ba8b9 100644 --- a/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java +++ b/src/main/java/com/amazon/carbonado/gen/StorableGenerator.java @@ -2859,6 +2859,8 @@ public final class StorableGenerator { Label joinMatch = null; Label unreadable = null; Label unwritable = null; + Label readException = null; + Label writeException = null; for (int i=0; i> matches = caseMatches[i]; @@ -2919,6 +2921,11 @@ public final class StorableGenerator { unreadable = b.createLabel(); } b.branch(unreadable); + } else if (throwsCheckedException(prop.getReadMethod())) { + if (readException == null) { + readException = b.createLabel(); + } + b.branch(readException); } else { b.loadThis(); b.invoke(prop.getReadMethod()); @@ -2932,6 +2939,11 @@ public final class StorableGenerator { unwritable = b.createLabel(); } b.branch(unwritable); + } else if (throwsCheckedException(prop.getWriteMethod())) { + if (writeException == null) { + writeException = b.createLabel(); + } + b.branch(writeException); } else { b.loadThis(); b.loadLocal(b.getParameter(1)); @@ -2965,13 +2977,44 @@ public final class StorableGenerator { if (unreadable != null) { unreadable.setLocation(); - throwIllegalArgException(b, "Property cannot be read: ", b.getParameter(0)); + throwIllegalArgException(b, "No accessor method for property: ", b.getParameter(0)); } if (unwritable != null) { unwritable.setLocation(); - throwIllegalArgException(b, "Property cannot be written: ", b.getParameter(0)); + throwIllegalArgException(b, "No mutator method for property: ", b.getParameter(0)); + } + + if (readException != null) { + readException.setLocation(); + throwIllegalArgException(b, "Accessor method declares throwing a checked exception: ", + b.getParameter(0)); + } + + if (writeException != null) { + writeException.setLocation(); + throwIllegalArgException(b, "Mutator method declares throwing a checked exception: ", + b.getParameter(0)); + } + } + + private static boolean throwsCheckedException(Method method) { + Class[] exceptionTypes = method.getExceptionTypes(); + if (exceptionTypes == null) { + return false; + } + + for (Class exceptionType : exceptionTypes) { + if (RuntimeException.class.isAssignableFrom(exceptionType)) { + continue; + } + if (Error.class.isAssignableFrom(exceptionType)) { + continue; + } + return true; } + + return false; } private static void throwIllegalArgException(CodeBuilder b, String message, -- cgit v1.2.3