Style: move LocalRef to header / move check_and_clear_exception to header

This commit is contained in:
xiaozhou233 committed 2026-04-18 19:24:49 +08:00
1 parent 455986b08c
commit 8b0bf8f211
3 files changed
+34 -28

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
#pragma once
#include <jni.h>
#include <jvmti.h>
#include <JuiceAgent/Logger.hpp>
// Check for JNI exceptions and clear them
bool check_and_clear_exception(JNIEnv* env, const char* context) {
if (env->ExceptionCheck()) {
PLOGE << "JNI Exception occurred at: " << context;
env->ExceptionDescribe();
env->ExceptionClear();
return true;
}
return false;
}
+1 -28
View File
@@ -2,37 +2,10 @@
#include <libloader.hpp>
#include <JuiceAgent/Config.hpp>
#include <jvm.hpp>
#include <JuiceAgent/Utils.hpp>
namespace libloader {
namespace {
// Check for JNI exceptions and clear them
bool check_and_clear_exception(JNIEnv* env, const char* context) {
if (env->ExceptionCheck()) {
PLOGE << "JNI Exception occurred at: " << context;
env->ExceptionDescribe();
env->ExceptionClear();
return true;
}
return false;
}
// RAII wrapper for local JNI references
template<typename T>
class LocalRef {
private:
JNIEnv* env;
T ref;
public:
LocalRef(JNIEnv* env, T ref) : env(env), ref(ref) {}
~LocalRef() {
if (ref) env->DeleteLocalRef(ref);
}
T get() const { return ref; }
operator T() const { return ref; }
};
}
// Invoke JuiceAgentBootstrap.start(String) via JNI
bool invoke_juiceagent_init(JNIEnv* env, const InjectionInfo& info) {
const char* bootstrap_class = "cn/xiaozhou233/juiceagent/api/JuiceAgentBootstrap";
+17
View File
@@ -12,3 +12,20 @@ namespace libloader
bool invoke_juiceagent_init(JNIEnv* env, const InjectionInfo& info);
} // namespace libloader
namespace {
// RAII wrapper for local JNI references
template<typename T>
class LocalRef {
private:
JNIEnv* env;
T ref;
public:
LocalRef(JNIEnv* env, T ref) : env(env), ref(ref) {}
~LocalRef() {
if (ref) env->DeleteLocalRef(ref);
}
T get() const { return ref; }
operator T() const { return ref; }
};
}