int
ThreadAsyncUtils::threadAsync(BaseThreadAsyncArgument& argument) {
pthread_t
thread
;
int
ret = pthread_create(&
thread
, NULL, threadAsyncInternal, &argument);
if
(0 != ret) {
LOGE(
"thread async create error: %d, %s"
,
errno
,
strerror
(
errno
))
return
ret;
}
ret = pthread_join(
thread
, NULL);
if
(0 != ret) {
LOGE(
"thread async join error: %d, %s"
,
errno
,
strerror
(
errno
))
return
ret;
}
return
0;
}
static
void
reflect_getDeclaredMethod_internal(BaseThreadAsyncArgument* _args) {
ReflectThreadAsyncArgument* args = (ReflectThreadAsyncArgument*)_args;
jobject clazz = args->jcls_clazz;
jstring method_name = args->jcls_name;
jobjectArray params = args->jcls_params;
bool
attach =
false
;
JNIEnv *env = jni_get_env(attach);
if
(!env)
return
;
jclass clazz_class = env->GetObjectClass(clazz);
jmethodID get_declared_method_id = env->GetMethodID(clazz_class,
"getDeclaredMethod"
,
"(Ljava/lang/String;[Ljava/lang/Class;)Ljava/lang/reflect/Method;"
);
jobject res = env->CallObjectMethod(clazz, get_declared_method_id, method_name, params);
if
(env->ExceptionCheck()) {
LOG_JNI_CLEAR_EXCEPTION(env)
}
if
(res != nullptr) {
args->jcls_result = env->NewGlobalRef(res);
}
jni_env_thread_detach();
}
jobject ReflectUtils::getDeclaredMethod(JNIEnv *env, jclass interface, jobject clazz, jstring method_name, jobjectArray params) {
auto
global_clazz = env->NewGlobalRef(clazz);
jstring global_method_name = (jstring) env->NewGlobalRef(method_name);
int
arg_length = env->GetArrayLength(params);
jobjectArray global_params = nullptr;
if
(params != nullptr) {
jobject element;
for
(
int
i = 0; i < arg_length; i++) {
element = (jobject) env->GetObjectArrayElement(params, i);
env->SetObjectArrayElement(params, i, env->NewGlobalRef(element));
}
global_params = (jobjectArray) env->NewGlobalRef(params);
}
ReflectThreadAsyncArgument argument(reflect_getDeclaredMethod_internal);
argument.setMethod(global_clazz, global_method_name, global_params);
if
(0 == ThreadAsyncUtils::threadAsync(argument)) {
return
argument.jcls_result;
}
return
NULL;
}