我想知道JNI apis是否可以在当前JVM中列出所有当前可用的实例(作为jobject)。
我的意思是:
empty = X-Frame-Options: SAMEORIGINlock GMT
short = foorame-Options: SAMEORIGINlock GMT
long = thirty six character long string bla
我的任务是在它们中搜索实现某个接口(jvm->AttachCurrentThreadAsDaemon((void**)&env,0);
jobject* instances;
int count = env->GetInstances(&instances);
)的对象,我必须在没有类名的情况下动态地全局地执行此操作
答案 0 :(得分:4)
JVMTI会有所帮助。
IterateOverInstancesOfClass
以标记所有必需的对象; GetObjectsWithTags
将所有已标记的对象复制到jobject*
数组。这是一个例子。请注意,targetClass
也可以是一个界面。
static jvmtiIterationControl JNICALL
HeapObjectCallback(jlong class_tag, jlong size, jlong* tag_ptr, void* user_data) {
*tag_ptr = 1;
return JVMTI_ITERATION_CONTINUE;
}
JNIEXPORT void JNICALL
Java_Test_iterateInstances(JNIEnv* env, jclass ignored, jclass targetClass) {
JavaVM* vm;
env->GetJavaVM(&vm);
jvmtiEnv* jvmti;
vm->GetEnv((void**)&jvmti, JVMTI_VERSION_1_0);
jvmtiCapabilities capabilities = {0};
capabilities.can_tag_objects = 1;
jvmti->AddCapabilities(&capabilities);
jvmti->IterateOverInstancesOfClass(targetClass, JVMTI_HEAP_OBJECT_EITHER,
HeapObjectCallback, NULL);
jlong tag = 1;
jint count;
jobject* instances;
jvmti->GetObjectsWithTags(1, &tag, &count, &instances, NULL);
printf("Found %d objects with tag\n", count);
jvmti->Deallocate((unsigned char*)instances);
}