美文网首页
jni 获取变量

jni 获取变量

作者: Sanvar | 来源:发表于2018-02-13 15:14 被阅读0次
    #define JINT "I"
    #define JLONG "J"
    #define JFLOAT "F"
    #define JSTR "Ljava/lang/String;"
    #define JBOOL "Z"
    #define JBYTES "[B"
    ///获取变量ID
    jfieldID GetFieldID_(int line,
                         JNIEnv* jni,
                         jclass c,
                         const char* name,
                         const char* signature);
    ///获取变量ID
    jfieldID GetFieldID_(int line,
                         JNIEnv* jni,
                         jclass c,
                         const char* name,
                         const char* signature) {
      jfieldID f = jni->GetFieldID(c, name, signature);
      CHECK_EXCEPTION(jni) << "error during GetFieldID"
                           << " line: " << line;
      CHECK(f) << name << ", " << signature << " line: " << line;
      return f;
    }
    
    ///获取变量ID
    #define _GetFieldID(_jni_, _c_, _name_, _signature_) \
      GetFieldID_(__LINE__, _jni_, _c_, _name_, _signature_)
    
    
    ///获取整型变量
    jint GetIntField_(int line,
                      JNIEnv* jni,
                      jobject object,
                      jclass cls,
                      const char* name);
    ///获取整型变量
    jint GetIntField_(int line,
                      JNIEnv* jni,
                      jobject object,
                      jclass cls,
                      const char* name) {
     // 重要的是这一句, 其他类型的调用其他的名称,如:GetLongField....
      jint i = jni->GetIntField(object, _GetFieldID(jni, cls, name, JINT));
      CHECK_EXCEPTION(jni) << "error during GetIntField"
                           << " line: " << line;
      return i;
    }
    
    #define _GetIntField(_jni_, _obj_, _cls_, _name_) \
      GetIntField_(__LINE__, _jni_, _obj_, _cls_, _name_)
    
    • 使用方式
      获取一个对象的整形变量
    jint sn  = _GetIntField(env, msg, cls, "sn");
    

    相关文章

      网友评论

          本文标题:jni 获取变量

          本文链接:https://www.haomeiwen.com/subject/epgrtftx.html