In JNI, how do I cache the class, methodID, and fieldIDs per IBM's performance recommendations?

前端 未结 5 1785
庸人自扰
庸人自扰 2020-12-02 12:40

I read on on IBM that

To access Java objects\' fields and invoke their methods, native code must make calls to FindClass(), GetFieldID(), GetMethod

5条回答
  •  清歌不尽
    2020-12-02 13:02

    For simple case, I use static fields in the C code, initialized with an initId method called from my java classes :

    package demo;
    public class WithNatives {
    
     static {
       initIDs();
     }
    
     private static native void initIDs();
    
    }
    

    And in C :

    static jmethodID methodId;
    
    void JNICALL Java_demo_WithNatives_initIDs(JNIEnv *env, jclass clazz)
    {
       // initialize methodId
    }
    

提交回复
热议问题