From 05951450cc7059a2fdf32b6c26a68408d52f8c33 Mon Sep 17 00:00:00 2001
From: "Brian S. O'Neill" <bronee@gmail.com>
Date: Thu, 1 Jan 2009 16:50:56 +0000
Subject: Add utility to concat exception message at runtime.

---
 .../com/amazon/carbonado/gen/CodeBuilderUtil.java  | 34 ++++++++++++++++++++++
 1 file changed, 34 insertions(+)

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
@@ -99,6 +99,40 @@ public class CodeBuilderUtil {
         b.throwObject();
     }
 
+    /**
+     * 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
-- 
cgit v1.2.3