JNI,如何列出所有当前实例? [英] JNI, How to list all the current instances?
本文介绍了JNI,如何列出所有当前实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道JNI apis是否可以列出当前JVM中所有当前可用的实例(作为jobject)。
意思是:
jvm-> AttachCurrentThreadAsDaemon((void **)& env,0);
jobject * instances;
int count = env-> GetInstances(& instances);
我的任务是通过它们搜索实现某个接口的对象( env-> IsInstanceOf()
),我必须在没有类名的情况下动态和全局。
解决方案
JVMTI 会有所帮助。
- 致电
IterateOverInstancesOfClass
标记所有必需物件; - 呼叫
GetObjectsWithTags
复制所有 注意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\\\
,count);
jvmti-> Deallocate((unsigned char *)instances);
}
I would like to know if it is possible with JNI apis to list ALL the current available instances(as jobject) in the current JVM.
Example of what I mean:
jvm->AttachCurrentThreadAsDaemon((void**)&env,0); jobject* instances; int count = env->GetInstances(&instances);
My task would be to search through them for objects which implement a certain interface(
env->IsInstanceOf()
), I have to do this dynamically and globally without class names解决方案JVMTI will help.
- Call
IterateOverInstancesOfClass
to tag all required objects; - Call
GetObjectsWithTags
to copy all tagged objects tojobject*
array.
Here is an example. Note that
targetClass
can be also an interface.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); }
这篇关于JNI,如何列出所有当前实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Call
查看全文