Feat: add classloader JNI layer for the new agent

- Re-add addToBootstrap/SystemClassLoaderSearch, addToClassLoader, defineClass,
  getLoadedClasses, getClassByName on the new Agent singleton
- Add utils.hpp with check_env/get_class_name helpers
- Pull utils.hpp into jni_common.hpp umbrella header
This commit is contained in:
xiaozhou233 committed 2026-08-16 22:49:13 +08:00
1 parent 2ab1c134ed
commit 6afa44f90a
3 files changed
+319

No files matched your search

+1
View File
@@ -4,6 +4,7 @@
#include <jvmti.h>
#include <JuiceAgent/Logger.hpp>
#include <JuiceAgent.hpp>
#include <utils.hpp>
// Generated by JuiceAgent-API
#include <JuiceAgent/cn_xiaozhou233_juiceagent_api_JuiceAgent.h>
+43
View File
@@ -0,0 +1,43 @@
#pragma once
#include <JuiceAgent.hpp>
inline bool check_env(JuiceAgent::Agent& agent) {
if (agent.getJavaVM() == nullptr) {
spdlog::error("Failed to get JavaVM instance");
return false;
}
if (agent.getJVMTI() == nullptr) {
spdlog::error("Failed to get JVMTI environment");
return false;
}
return true;
}
inline std::string get_class_name(JNIEnv* env, jclass clazz) {
if (!env || !clazz) return "";
jclass clsClass = env->FindClass("java/lang/Class");
if (!clsClass) return "";
jmethodID mid = env->GetMethodID(clsClass, "getName", "()Ljava/lang/String;");
if (!mid) {
env->DeleteLocalRef(clsClass);
return "";
}
jstring nameStr = static_cast<jstring>(env->CallObjectMethod(clazz, mid));
std::string result;
if (nameStr) {
const char* tmp = env->GetStringUTFChars(nameStr, nullptr);
if (tmp) {
result = tmp; // copy to std::string
env->ReleaseStringUTFChars(nameStr, tmp);
}
env->DeleteLocalRef(nameStr);
}
env->DeleteLocalRef(clsClass);
return result;
}