zhztheplayer commented on code in PR #13246:
URL: https://github.com/apache/arrow/pull/13246#discussion_r913970270
##########
java/dataset/src/main/cpp/jni_util.cc:
##########
@@ -162,11 +165,105 @@ std::shared_ptr<ReservationListener>
ReservationListenableMemoryPool::get_listen
ReservationListenableMemoryPool::~ReservationListenableMemoryPool() {}
+std::string Describe(JNIEnv* env, jthrowable t) {
+ jclass describer_class =
+ env->FindClass("org/apache/arrow/dataset/jni/JniExceptionDescriber");
+ jmethodID describe_method = env->GetStaticMethodID(
+ describer_class, "describe",
"(Ljava/lang/Throwable;)Ljava/lang/String;");
+ std::string description = JStringToCString(
+ env, (jstring)env->CallStaticObjectMethod(describer_class,
describe_method, t));
+ return description;
+}
+
+bool IsErrorInstanceOf(JNIEnv* env, jthrowable t, std::string class_name) {
+ jclass jclass = env->FindClass(class_name.c_str());
+ if (jclass == nullptr) {
+ ARROW_LOG(WARNING) << "Could not find Java class " << class_name;
+ return false;
+ }
+ return env->IsInstanceOf(t, jclass);
+}
+
+arrow::StatusCode MapJavaError(JNIEnv* env, jthrowable t) {
+ StatusCode code;
+ if (IsErrorInstanceOf(env, t,
"org/apache/arrow/memory/OutOfMemoryException") ==
+ JNI_TRUE) {
Review Comment:
done
##########
java/dataset/src/main/cpp/jni_util.cc:
##########
@@ -162,11 +165,105 @@ std::shared_ptr<ReservationListener>
ReservationListenableMemoryPool::get_listen
ReservationListenableMemoryPool::~ReservationListenableMemoryPool() {}
+std::string Describe(JNIEnv* env, jthrowable t) {
+ jclass describer_class =
+ env->FindClass("org/apache/arrow/dataset/jni/JniExceptionDescriber");
+ jmethodID describe_method = env->GetStaticMethodID(
+ describer_class, "describe",
"(Ljava/lang/Throwable;)Ljava/lang/String;");
+ std::string description = JStringToCString(
+ env, (jstring)env->CallStaticObjectMethod(describer_class,
describe_method, t));
+ return description;
+}
+
+bool IsErrorInstanceOf(JNIEnv* env, jthrowable t, std::string class_name) {
+ jclass jclass = env->FindClass(class_name.c_str());
+ if (jclass == nullptr) {
+ ARROW_LOG(WARNING) << "Could not find Java class " << class_name;
+ return false;
+ }
+ return env->IsInstanceOf(t, jclass);
+}
+
+arrow::StatusCode MapJavaError(JNIEnv* env, jthrowable t) {
+ StatusCode code;
+ if (IsErrorInstanceOf(env, t,
"org/apache/arrow/memory/OutOfMemoryException") ==
+ JNI_TRUE) {
+ code = StatusCode::OutOfMemory;
+ } else if (IsErrorInstanceOf(env, t,
"java/lang/UnsupportedOperationException") ==
+ JNI_TRUE) {
+ code = StatusCode::NotImplemented;
+ } else if (IsErrorInstanceOf(env, t, "java/io/NotSerializableException") ==
JNI_TRUE) {
+ code = StatusCode::SerializationError;
+ } else if (IsErrorInstanceOf(env, t, "java/io/IOException") == JNI_TRUE) {
+ code = StatusCode::IOError;
+ } else if (IsErrorInstanceOf(env, t, "java/lang/IllegalArgumentException") ==
+ JNI_TRUE) {
+ code = StatusCode::Invalid;
+ } else if (IsErrorInstanceOf(env, t, "java/lang/IllegalStateException") ==
JNI_TRUE) {
+ code = StatusCode::Invalid;
+ } else {
+ code = StatusCode::UnknownError;
+ }
+ return code;
+}
+
+JNIEnv* GetEnvOrAttach(JavaVM* vm) {
+ JNIEnv* env;
+ int getEnvStat = vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION);
+ if (getEnvStat == JNI_EDETACHED) {
+ // Reattach current thread to JVM
+ getEnvStat = vm->AttachCurrentThread(reinterpret_cast<void**>(&env),
nullptr);
+ if (getEnvStat != JNI_OK) {
+ ARROW_LOG(FATAL) << "Failed to attach current thread to JVM";
+ }
+ }
+ return env;
+}
+
+JavaErrorDetail::JavaErrorDetail(JavaVM* vm, jthrowable cause) : vm_(vm) {
+ JNIEnv* env = GetEnvOrAttach(vm_);
+ if (env == nullptr) {
+ this->cause_ = nullptr;
+ return;
+ }
+ this->cause_ = (jthrowable)env->NewGlobalRef(cause);
+}
+
+JavaErrorDetail::~JavaErrorDetail() {
+ JNIEnv* env = GetEnvOrAttach(vm_);
+ if (env == nullptr || this->cause_ == nullptr) {
+ return;
+ }
+ env->DeleteGlobalRef(cause_);
+}
+
+jthrowable JavaErrorDetail::GetCause() const {
+ JNIEnv* env = GetEnvOrAttach(vm_);
+ if (env == nullptr || this->cause_ == nullptr) {
+ return nullptr;
+ }
+ return (jthrowable)env->NewLocalRef(cause_);
+}
+
+std::string JavaErrorDetail::ToString() const {
+ JNIEnv* env = GetEnvOrAttach(vm_);
+ if (env == nullptr) {
+ return "Java Exception, ID: " +
std::to_string(reinterpret_cast<size_t>(cause_));
Review Comment:
done
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]