summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2007-08-12 16:02:10 +0000
committerBrian S. O'Neill <bronee@gmail.com>2007-08-12 16:02:10 +0000
commit6bceb388eefa18085a467ecd9d96ba13009694c2 (patch)
tree39a34ceb3015d64cbaea6b859d41b853e15aa104 /src
parent613111cb295aa0a16f12014af99e7e538b4b47ab (diff)
Storable methods to access properties by name disallow access to methods which declare throwing checked exceptions.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/amazon/carbonado/Storable.java8
-rw-r--r--src/main/java/com/amazon/carbonado/gen/StorableGenerator.java47
2 files changed, 50 insertions, 5 deletions
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<S extends Storable<S>> {
*
* @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<S extends Storable<S>> {
*
* @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<S extends Storable> {
Label joinMatch = null;
Label unreadable = null;
Label unwritable = null;
+ Label readException = null;
+ Label writeException = null;
for (int i=0; i<caseCount; i++) {
List<StorableProperty<?>> matches = caseMatches[i];
@@ -2919,6 +2921,11 @@ public final class StorableGenerator<S extends Storable> {
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<S extends Storable> {
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<S extends Storable> {
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,