JNI; How to list all current instances?

I would like to know if it is possible using the JNI apis to display ALL current available instances (like jobject) in the current JVM.

An example of what I mean:

jvm->AttachCurrentThreadAsDaemon((void**)&env,0); jobject* instances; int count = env->GetInstances(&instances); 

My task would be to find them for objects that implement a specific interface ( env->IsInstanceOf() ), I have to do this dynamically and globally without class names

+1
source share
1 answer

JVMTI will help.

Here is an example. Note that targetClass can also be 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); } 
+3
source

All Articles