#include #include #include #include "../src/mlp_priv.h" #include "mlp_java_priv.h" JNIEnv *mlp_java_get_env(JavaVM *jvm){ JNIEnv *env ; int rc = (*jvm)->AttachCurrentThread(jvm, ((void **)&env), NULL) ; if (rc < 0){ mlp_fatal("mlp_java: Can't attach current thread to JVM") ; } return env ; } int mlp_java_check_exception(JNIEnv *env){ return (*env)->ExceptionCheck(env) == JNI_TRUE ; } void mlp_java_set_error_from_exception(JNIEnv *env, mlp_context *ctx, mlp_error err, const char *fmt, ...){ char *format = strdup(fmt) ; jthrowable exc = (*env)->ExceptionOccurred(env) ; if (exc != NULL){ (*env)->ExceptionClear(env) ; /* We must get the exception class name and message and reallocate a new format string */ jclass exc_class = (*env)->GetObjectClass(env, exc) ; jmethodID mid = (*env)->GetMethodID(env, exc_class, "toString", "()Ljava/lang/String;") ; if ((mid == NULL)||(mlp_java_check_exception(env))){ (*env)->ExceptionDescribe(env) ; mlp_fatal("mlp_java: Can't find method toString() for exception object") ; } jstring str = (jstring)(*env)->CallObjectMethod(env, exc_class, mid) ; if (mlp_java_check_exception(env)){ (*env)->ExceptionDescribe(env) ; mlp_fatal("mlp_java: Can't call method toString() for exception object") ; } const char *c_str = (*env)->GetStringUTFChars(env, str, NULL) ; free(format) ; format = (char *)malloc((strlen(fmt) + strlen(c_str) + 32) * sizeof(char)) ; sprintf(format, "%s [%s]", fmt, c_str) ; (*env)->ReleaseStringUTFChars(env, str, c_str) ; } va_list va ; va_start(va, fmt) ; char *info = (char *)malloc(MLP_MAX_ERROR_MSG_LENGTH * sizeof(char)) ; vsnprintf(info, MLP_MAX_ERROR_MSG_LENGTH - 1, format, va) ; va_end(va) ; mlp_context_set_error(ctx, err, "%s", info) ; free(format) ; free(info) ; }