开发者

JNI how to access Java Object (Integer)

I have a JNI method to access java method which returns an Integer object. I do not want to return the primitive int type because this code will be modified to handle Generic objects. The following is what I have. I am not able to get the value of the Integer that I pass. The output at C++ side is something like

value = 0x4016f3d0

How can I get the actual value of Integer object that I pass at C++ end?

Please help.

Thanks,

-H

GenericPeer.cpp

JNIEXPORT void JNICALL Java_GenericPeer_print (JNIEnv *jenv, jclass jcls, j开发者_C百科object data){
 jclass peerCls = jenv->GetObjectClass(data);
 jmethodID mGetValue = jenv->GetMethodID(peerCls, "getValue","()Ljava/lang/Integer;");
 if(mGetValue == NULL){
   return (-1);
 } 
 jobject value = jenv->CallObjectMethod(data, mGetValue);
 cout<<"value = "<<value<<endl;

}

GenericPeer.java

public class GenericPeer {
 public static native void print(Data d);
 static {
  System.load("/home/usr/workspace/GenericJni/src/libGenericJni.so");
 }
}

Data.java

public class Data {
 private Integer value;
 pubilc Data(Integer v){ 
  this.value = v;
 }
 public Integer getValue() { return value; }
    public void setValue(Integer value) {
 this.value = value;
 }
}

Test.java (Main class)

public class Test {
 public static void main(String[] args){
       Integer i = new Integer(1);
  Data d = new Data(i);
  GenericPeer.print(d);
      }
}


You have to invoke the intValue method on the Integer instance to get its primitive value. Use FindClass instead of GetObjectClass (as in your code) to get a reference to the class java.lang.Integer and then GetMethodID and CallObjectMethod to actually invoke the intValue method.


Thanks Jarnbjo,

It works now! This is what I have:

    JNIEXPORT jint JNICALL Java_GenericPeer_print (JNIEnv *jenv, jclass jcls, jobject data){
      jclass peerCls = jenv->GetObjectClass(data);

     jmethodID mGetValue = jenv->GetMethodID(peerCls, "getValue","()Ljava/lang/Integer;");
     if (mGetValue == NULL){
       return(-1);
     }

     jobject value = jenv->CallObjectMethod(data, mGetValue);
     if(value == NULL){
      cout<<"jobject value = NULL"<<endl;
      return(-1);
     }

    //getValue()

     jclass cls = jenv->FindClass("java/lang/Integer");
     if(cls == NULL){
       outFile<<"cannot find FindClass(java/lang/Integer)"<<endl;
     }
       jmethodID getVal = jenv->GetMethodID(cls, "intValue", "()I");
       if(getVal == NULL){
         outFile<<"Couldnot find Int getValue()"<<endl;
       }
       int i = jenv->CallIntMethod(value, getVal);
}   


You have to call intValue() method through jni interface.

  1. First use FindClass to get Integer class reference.
  2. To invoke intValue method, use CallIntMethod function. Here the method type signature is ()I.
static int GetIntegerValue(JNIEnv *env, jobject value) {
    jclass integer_class = env->FindClass("java/lang/Integer");
    return env->CallIntMethod(value, env->GetMethodID(integer_class, "intValue", "()I"));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜