summaryrefslogtreecommitdiff
path: root/src/main/java/com/amazon/carbonado
diff options
context:
space:
mode:
authorBrian S. O'Neill <bronee@gmail.com>2009-01-01 16:50:56 +0000
committerBrian S. O'Neill <bronee@gmail.com>2009-01-01 16:50:56 +0000
commit05951450cc7059a2fdf32b6c26a68408d52f8c33 (patch)
treede2b7c2798ae260561d5b5a0e2dd8bd5b78be3d7 /src/main/java/com/amazon/carbonado
parentb807ac8cdc3475b3d6dd6fbaf9f4c3f1f8263d10 (diff)
Add utility to concat exception message at runtime.
Diffstat (limited to 'src/main/java/com/amazon/carbonado')
-rw-r--r--src/main/java/com/amazon/carbonado/gen/CodeBuilderUtil.java34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/main/java/com/amazon/carbonado/gen/CodeBuilderUtil.java b/src/main/java/com/amazon/carbonado/gen/CodeBuilderUtil.java
index f25d6f1..7fad91f 100644
--- a/src/main/java/com/amazon/carbonado/gen/CodeBuilderUtil.java
+++ b/src/main/java/com/amazon/carbonado/gen/CodeBuilderUtil.java
@@ -100,6 +100,40 @@ public class CodeBuilderUtil {
}
/**
+ * Generate code to throw an exception with a message concatenated at runtime.
+ *
+ * @param b {@link CodeBuilder} to which to add code
+ * @param type type of the object to throw
+ * @param messages messages to concat at runtime
+ */
+ public static void throwConcatException(CodeBuilder b, Class type, String... messages) {
+ if (messages == null || messages.length == 0) {
+ throwException(b, type, null);
+ return;
+ }
+ if (messages.length == 1) {
+ throwException(b, type, messages[0]);
+ return;
+ }
+
+ TypeDesc desc = TypeDesc.forClass(type);
+ b.newObject(desc);
+ b.dup();
+
+ TypeDesc[] params = new TypeDesc[] {TypeDesc.STRING};
+
+ for (int i=0; i<messages.length; i++) {
+ b.loadConstant(String.valueOf(messages[i]));
+ if (i > 0) {
+ b.invokeVirtual(TypeDesc.STRING, "concat", TypeDesc.STRING, params);
+ }
+ }
+
+ b.invokeConstructor(desc, params);
+ b.throwObject();
+ }
+
+ /**
* Collect a set of all the interfaces and recursively all superclasses for the leaf
* (genericised class) and root (genericised base class). Eg, for Object<foo>, all
* classes and implemented interfaces for every superclass between foo (the leaf) and