box
box

Reputation: 3246

get jclass from jfieldID

I want to iterate over all field values of an object of a given class using JNI. I found the JVMTI method GetClassFields to get the jfieldIDs of the fields of an object. In the JNI API I found the methods Get<type>Field. However, to use these methods I first have to know if I'm dealing with one of the primitives, arrays of a certain type, or objects. I found a JNI method ToReflectedField so I could call the Java methods of the reflection API. However, this seems overly complicated and error prone.

Is there a way to achieve this with native functions of JNI or JVMTI?

Upvotes: 0

Views: 853

Answers (1)

Jan Gassen
Jan Gassen

Reputation: 3534

You can use the JVMTI to get the type of every field and then call the respective GetField method:

jvmtiError
GetFieldName(jvmtiEnv* env,
        jclass klass,
        jfieldID field,
        char** name_ptr,
        char** signature_ptr,
        char** generic_ptr)

Upvotes: 1

Related Questions