Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 4 additions & 15 deletions be/src/util/jni-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ Status JniUtil::GetJniExceptionMsg(JNIEnv* env, bool log_stack, const string& pr
}

jobject JniUtil::convert_to_java_map(JNIEnv* env, const std::map<std::string, std::string>& map) {
//TODO: ADD EXCEPTION CHECK.
jclass hashmap_class = env->FindClass("java/util/HashMap");
jmethodID hashmap_constructor = env->GetMethodID(hashmap_class, "<init>", "(I)V");
jobject hashmap_object = env->NewObject(hashmap_class, hashmap_constructor, map.size());
Expand Down Expand Up @@ -400,26 +399,16 @@ std::map<std::string, std::string> JniUtil::convert_to_cpp_map(JNIEnv* env, jobj

Status JniUtil::GetGlobalClassRef(JNIEnv* env, const char* class_str, jclass* class_ref) {
*class_ref = NULL;
JNI_CALL_METHOD_CHECK_EXCEPTION_DELETE_REF(jclass, local_cl, env, FindClass(class_str));
jclass local_cl = env->FindClass(class_str);
RETURN_ERROR_IF_EXC(env);
RETURN_IF_ERROR(LocalToGlobalRef(env, local_cl, reinterpret_cast<jobject*>(class_ref)));
env->DeleteLocalRef(local_cl);
RETURN_ERROR_IF_EXC(env);
return Status::OK();
}

Status JniUtil::LocalToGlobalRef(JNIEnv* env, jobject local_ref, jobject* global_ref) {
*global_ref = env->NewGlobalRef(local_ref);
// NewGlobalRef:
// Returns a global reference to the given obj.
//
//May return NULL if:
// obj refers to null
// the system has run out of memory
// obj was a weak global reference and has already been garbage collected
if (*global_ref == NULL) {
return Status::InternalError(
"LocalToGlobalRef fail,global ref is NULL,maybe the system has run out of memory.");
}

//NewGlobalRef not throw exception,maybe we just need check NULL.
RETURN_ERROR_IF_EXC(env);
return Status::OK();
}
Expand Down
26 changes: 4 additions & 22 deletions be/src/util/jni-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

#include "common/status.h"
#include "jni_md.h"
#include "util/defer_op.h"
#include "util/thrift_util.h"

#ifdef USE_HADOOP_HDFS
Expand All @@ -39,25 +38,12 @@ extern "C" JNIEnv* getJNIEnv(void);
namespace doris {
class JniUtil;

#define RETURN_ERROR_IF_EXC(env) \
do { \
if (env->ExceptionCheck()) [[unlikely]] \
return JniUtil::GetJniExceptionMsg(env); \
#define RETURN_ERROR_IF_EXC(env) \
do { \
jthrowable exc = (env)->ExceptionOccurred(); \
if (exc != nullptr) return JniUtil::GetJniExceptionMsg(env); \
} while (false)

#define JNI_CALL_METHOD_CHECK_EXCEPTION_DELETE_REF(type, result, env, func) \
type result = env->func; \
DEFER(env->DeleteLocalRef(result)); \
RETURN_ERROR_IF_EXC(env)

#define JNI_CALL_METHOD_CHECK_EXCEPTION(type, result, env, func) \
type result = env->func; \
RETURN_ERROR_IF_EXC(env)

//In order to reduce the potential risks caused by not handling exceptions,
// you need to refer to https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html
// to confirm whether the jni method will throw an exception.

class JniUtil {
public:
static Status Init() WARN_UNUSED_RESULT;
Expand All @@ -79,10 +65,6 @@ class JniUtil {
return Status::OK();
}

//jclass is generally a local reference.
//Method ID and field ID values are forever.
//If you want to use the jclass across multiple threads or multiple calls into the JNI code you need
// to create a global reference to it with GetGlobalClassRef().
static Status GetGlobalClassRef(JNIEnv* env, const char* class_str,
jclass* class_ref) WARN_UNUSED_RESULT;

Expand Down
Loading