zhztheplayer commented on code in PR #13246:
URL: https://github.com/apache/arrow/pull/13246#discussion_r913820749


##########
java/dataset/src/main/cpp/jni_util.cc:
##########
@@ -162,11 +165,104 @@ 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;
+}
+
+arrow::Result<bool> IsErrorInstanceOf(JNIEnv* env, jthrowable t, std::string 
class_name) {
+  jclass jclass = env->FindClass(class_name.c_str());
+  if (jclass == nullptr) {
+    return arrow::Status::Invalid("Class not found: " + class_name);

Review Comment:
   Is `DCHECK` suitable for null-checking + returning? Anyway I have changed to 
return `false` in latest commit.



##########
java/dataset/src/main/cpp/jni_util.cc:
##########
@@ -162,11 +165,104 @@ 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;
+}
+
+arrow::Result<bool> IsErrorInstanceOf(JNIEnv* env, jthrowable t, std::string 
class_name) {
+  jclass jclass = env->FindClass(class_name.c_str());
+  if (jclass == nullptr) {
+    return arrow::Status::Invalid("Class not found: " + class_name);
+  }
+  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) {
+      std::cout << "Failed to attach current thread to JVM. " << std::endl;

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]

Reply via email to