Using JNI / JVMTI - How to determine if jfieldID is static or not
-
14-04-2021 - |
Question
If I use jvmti_env->GetClassFields(objClass, &numOfFields, &fieldIds);
I still have to decide whether to call GetStatic<Type>Field or Get<Type>Field.
How do I determine which one to use? I know that in java.lang.reflect.Field I can query fields' modifiers for static.
I've found a workaround here:
http://zoo.cs.yale.edu/classes/cs433/programming/examples-java-socket/jdk1.5/hotspot/src/share/vm/runtime/jfieldIDWorkaround.hpp
But I prefer a solution from the JVM.
Thanks!
Solution
Use function GetFieldModifiers in JVMTI.
Values returned by this function are described here: http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#1480
So for static you should check like this:
jint accessFlags
...
(*jvmti)->GetFieldModifiers(jvmti, objClass, fieldIds[i], &accessFlags);
if (accessFlags & 0x0008)
printf("STATIC\n");
else
printf("NOT STATIC\n");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow