From caa1bef21f63efaeb8408fd3ed8009537d6f17b6 Mon Sep 17 00:00:00 2001 From: LoveSy Date: Thu, 1 Apr 2021 18:52:44 +0800 Subject: [PATCH] [core] Remove useless codes (#441) --- core/src/main/cpp/main/include/jni_helper.h | 44 +++++-------------- .../lsposed/lspd/service/BridgeService.java | 2 +- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/core/src/main/cpp/main/include/jni_helper.h b/core/src/main/cpp/main/include/jni_helper.h index 1b971e60..567958e7 100644 --- a/core/src/main/cpp/main/include/jni_helper.h +++ b/core/src/main/cpp/main/include/jni_helper.h @@ -133,46 +133,26 @@ public: return mLocalRef != nullptr; } + operator bool() const { + return mLocalRef; + } + private: JNIEnv *mEnv; T mLocalRef; DISALLOW_COPY_AND_ASSIGN(ScopedLocalRef); }; -[[maybe_unused]] -inline void JNIExceptionClear(JNIEnv *env) { - if (env->ExceptionCheck()) { - env->ExceptionClear(); - } -} - -[[maybe_unused]] -inline bool JNIExceptionCheck(JNIEnv *env) { - if (env->ExceptionCheck()) { - jthrowable e = env->ExceptionOccurred(); - env->Throw(e); - env->DeleteLocalRef(e); - return true; - } - return false; -} - -[[maybe_unused]] -inline void JNIExceptionClearAndDescribe(JNIEnv *env) { - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - env->ExceptionClear(); - } -} - -inline jstring ClearException(JNIEnv *env) { - static jmethodID toString = env->GetMethodID(env->FindClass("java/lang/Object"), "toString", - "()Ljava/lang/String;"); +inline ScopedLocalRef ClearException(JNIEnv *env) { if (auto exception = env->ExceptionOccurred()) { env->ExceptionClear(); - return (jstring) env->CallObjectMethod(exception, toString); + static jmethodID toString = env->GetMethodID(env->FindClass("java/lang/Object"), "toString", + "()Ljava/lang/String;"); + auto str = (jstring) env->CallObjectMethod(exception, toString); + env->DeleteLocalRef(exception); + return {env, str}; } - return nullptr; + return {env, nullptr}; } template @@ -191,7 +171,7 @@ inline auto JNI_SafeInvoke(JNIEnv *env, Func f, Args &&... args) { ~finally() { if (auto exception = ClearException(env_)) { - LOGE("%s", JUTFString(env_, exception).get()); + LOGE("%s", JUTFString(env_, exception.get()).get()); } } diff --git a/core/src/main/java/org/lsposed/lspd/service/BridgeService.java b/core/src/main/java/org/lsposed/lspd/service/BridgeService.java index c864aafa..f0eb6844 100644 --- a/core/src/main/java/org/lsposed/lspd/service/BridgeService.java +++ b/core/src/main/java/org/lsposed/lspd/service/BridgeService.java @@ -266,7 +266,7 @@ public class BridgeService { String processName = data.readString(); IBinder heartBeat = data.readStrongBinder(); var applicationService = service.requestApplicationService(Binder.getCallingUid(), Binder.getCallingPid(), processName, heartBeat); - if (applicationService != null) binder = service.asBinder(); + if (applicationService != null) binder = applicationService.asBinder(); } catch (RemoteException e) { Log.e(TAG, Log.getStackTraceString(e)); }