summaryrefslogtreecommitdiff
path: root/src/cvc4.i
diff options
context:
space:
mode:
authorMorgan Deters <mdeters@gmail.com>2012-09-20 18:59:26 +0000
committerMorgan Deters <mdeters@gmail.com>2012-09-20 18:59:26 +0000
commit107988db066b3265c1cb80662e06f240def2a2c0 (patch)
tree6811034f6bf7c4fcd44cf6f7975b761642f18953 /src/cvc4.i
parent2c7da77682998d520136249897f69ceed53d49a9 (diff)
map C++ exceptions to Java exceptions correctly when they are thrown, and give a build error for Java component if it is misconfigured
Diffstat (limited to 'src/cvc4.i')
-rw-r--r--src/cvc4.i53
1 files changed, 50 insertions, 3 deletions
diff --git a/src/cvc4.i b/src/cvc4.i
index b13a555e6..cb8a7ba06 100644
--- a/src/cvc4.i
+++ b/src/cvc4.i
@@ -45,6 +45,8 @@ using namespace CVC4;
#include <set>
#include <string>
#include <ext/hash_map>
+#include <typeinfo>
+#include <cassert>
#include "util/sexpr.h"
#include "util/exception.h"
@@ -74,12 +76,57 @@ using namespace CVC4;
try {
$action
} catch(CVC4::Exception& e) {
- ::std::cerr << e << ::std::endl;
- jclass clazz = jenv->FindClass("java/lang/RuntimeException");
- jenv->ThrowNew(clazz, e.toString().c_str());
+ std::stringstream ss;
+ ss << e.what() << ": " << e.getMessage();
+ std::string explanation = ss.str();
+ SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, explanation.c_str());
}
}
+// Create a mapping from C++ Exceptions to Java Exceptions.
+// This is in a couple of throws typemaps, simply because it's sensitive to SWIG's concept of which namespace we're in.
+%typemap(throws) Exception %{
+ jclass clazz = jenv->FindClass("edu/nyu/acsys/CVC4/$1_type");
+ assert(clazz != NULL && jenv->ExceptionOccurred() == NULL);
+ jmethodID method = jenv->GetMethodID(clazz, "<init>", "(JZ)V");
+ assert(method != NULL && jenv->ExceptionOccurred() == NULL);
+ jthrowable t = static_cast<jthrowable>(jenv->NewObject(clazz, method, reinterpret_cast<long>(new $1_type($1)), true));
+ assert(t != NULL && jenv->ExceptionOccurred() == NULL);
+ int status = jenv->Throw(t);
+ assert(status == 0);
+%}
+%typemap(throws) CVC4::Exception %{
+ std::string name = "edu/nyu/acsys/$1_type";
+ for(size_t i = name.find("::"); i != std::string::npos; i = name.find("::")) {
+ name.replace(i, 2, "/");
+ }
+ jclass clazz = jenv->FindClass(name.c_str());
+ assert(clazz != NULL && jenv->ExceptionOccurred() == NULL);
+ jmethodID method = jenv->GetMethodID(clazz, "<init>", "(JZ)V");
+ assert(method != NULL && jenv->ExceptionOccurred() == NULL);
+ jthrowable t = static_cast<jthrowable>(jenv->NewObject(clazz, method, reinterpret_cast<long>(new $1_type($1)), true));
+ assert(t != NULL && jenv->ExceptionOccurred() == NULL);
+ int status = jenv->Throw(t);
+ assert(status == 0);
+%}
+
+%typemap(throws) ModalException = Exception;
+%typemap(throws) OptionException = Exception;
+%typemap(throws) IllegalArgumentException = Exception;
+%typemap(throws) AssertionException = Exception;
+
+%typemap(throws) CVC4::TypeCheckingException = CVC4::Exception;
+%typemap(throws) CVC4::ScopeException = CVC4::Exception;
+%typemap(throws) CVC4::IllegalArgumentException = CVC4::Exception;
+%typemap(throws) CVC4::AssertionException = CVC4::Exception;
+%typemap(throws) CVC4::parser::InputStreamException = CVC4::Exception;
+%typemap(throws) CVC4::parser::ParserException = CVC4::Exception;
+
+// Generate an error if the mapping from C++ CVC4 Exception to Java CVC4 Exception doesn't exist above
+%typemap(throws) SWIGTYPE, SWIGTYPE &, SWIGTYPE *, SWIGTYPE [], SWIGTYPE [ANY] %{
+#error "exception $1_type doesn't map to Java correctly"
+%}
+
%include "java/typemaps.i" // primitive pointers and references
%include "java/std_string.i" // map std::string to java.lang.String
%include "java/arrays_java.i" // C arrays to Java arrays
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback