How do I know if a class has been initialized or not?

Either using the CustomClassloader, or using the Java agent + Instrumentation API, it is quite simple and simple to get all the classes loaded by the JVM. However, the list of classes that were initialized does not look so simple. (I really wonder if there is a way to get it)

So, is there a way to find out if the class has been initialized?

- Thanks in advance

+4
source share
1 answer

I'm not sure about the API Instrumentation, but one possible way is to use the JVMTI function GetClassStatus.

, JVM, JVMTI_CLASS_STATUS_INITIALIZED

JavaVM *jvm;
jvmtiEnv *jvmti;
jvmtiError err;

env->GetJavaVM(&jvm);
jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_2);

jint classCount = 0;
jclass * classes;

jvmti->GetLoadedClasses(&classCount, &classes);
for (int i = 0; i < classCount; i++) {
    jint classStatus = 0;
    jvmti->GetClassStatus(classes[i], &classStatus);

    if (classStatus != JVMTI_CLASS_STATUS_PRIMITIVE
        && classStatus != JVMTI_CLASS_STATUS_ARRAY
        && classStatus != JVMTI_CLASS_STATUS_ERROR
        && !(classStatus & JVMTI_CLASS_STATUS_INITIALIZED)) {
        // static initializer is not finished yet
    }
}
+3

Source: https://habr.com/ru/post/1652514/


All Articles