How can I set the value of “Double” type variable of my Class by JNI?

狂风中的少年 提交于 2019-12-23 01:19:18

问题


If I just want to set value to Double type variable, I may code like:

public static native int getDoubleVar(Double dobj);
JNIEXPORT jint JNICALL
test_jni_Native_testGet(JNIEnv *env, jclass type, jobject dobj)
{
    jclass DoubleClass = env->FindClass("java/lang/Double");
    jfieldID valueID = env->GetFieldID(DoubleClass, "value", "D");
    env->SetDoubleField(dobj, valueID, 2.3);
    return 0;
}

These codes toke effect.

But, when I tried to set the value of a "Double" variable of a class by JNI, I can't get what I want, and of course, it broke down, which makes me a bit confused.

Java codes:

public class TestDouble
{

    public Double value;
    public long num;
    public TestDouble(long num, Double value)
    {
        this.num = num;
        this.value = value;
    }
}

Native:

public static native int testGet(TestDouble tdobj);

c codes:

JNIEXPORT jint JNICALL
test_jni_Native_testGet(JNIEnv *env, jclass type, jobject tdobj)
{
    jclass tdobjClass = env->FindClass("xxxx/TestDouble");
    jfieldID valueID = env->GetFieldID(tdobjClass, "value", "D");
    env->SetDoubleField(tdobj, jValueID, 2.3);
    return 0;
}

I'd like to set the value of 'value' of class 'TestDouble', and type of 'value' is "Double" (not double).

E/dalvikvm: VM aborting
Fatal signal 6
Send stop signal to pid:5830 in void debuggerd_signal_handler(int, siginfo_t*, void*)

I just paste the key wrong words here, and it's obvious that the error occurs at:

env->SetDoubleField(tdobj, jValueID, 2.3);

What can I do to deal with this problem then? Thanks a lot !


回答1:


I get the correct answer :

http://stackoverflow.com/questions/34848605/how-to-set-the-value-of-a-double-integer-type-of-a-java-class-by-jni

JNIEXPORT jint JNICALL test_jni_Native_testSet(JNIEnv *env, jclass type, jobject tdobj)
{
    //Create Integer class, get constructor and create Integer object
    jclass intClass = env->FindClass(env, "java/lang/Integer");
   jmethodID initInt = env->GetMethodID(env, intClass, "<init>", "(I)V");
    if (NULL == initInt) return -1;
    jobject newIntObj = env->NewObject(env, intClass, initInt, 123);

//Now set your integer into value atttribute. For this, I would
//recommend you to have a java setter and call it in the same way 
//as shown above

//clean reference
env->DeleteLocalRef(env, newIntObj); 
return 0;
}

Integer/Double and other wrapper types can be handled in the same way..




回答2:


Your C code assumes that TestDouble.value is a primitve double and not an Object Double and therefore fails at runtime.

You could change the class definition to

public class TestDouble
{
    public double value;
    public long num;
    public TestDouble(long num, double value)
    {
        this.num = num;
        this.value = value;
    }
}



回答3:


In case your Double object was already created in the JAVA code, you do not need to create a new Double object in the C++ (sorry, not C) part. You can do following:

void setDoubleObjectField(JNIEnv* env, jobject PropertyDoubleObj, char* FieldName, double dValue)
{
    jclass PropertyClass = (env)->GetObjectClass(PropertyDoubleObj);

    //printf("%s\n",getClassName( env, PropertyDoubleObj));

    jfieldID fidNumber = (env)->GetFieldID(PropertyClass, FieldName, "Ljava/lang/Double;");
    if (fidNumber == 0)
    {

        CreateException(env, std::string("Failed to query field from PropertyDouble class: ") + FieldName);
        return;
    }

    jobject DoubleObject = env->GetObjectField(PropertyDoubleObj, fidNumber);

    if (DoubleObject == 0) // That means, the object was simply not created. Just create one.
    {
        jclass DoubleClass = env->FindClass("java/lang/Double");
        jmethodID initDouble = env->GetMethodID( DoubleClass, "<init>", "(D)V");
        if (NULL == initDouble)
        {
            CreateException(env, "Failed to create Double object.");
            return;

        }
        DoubleObject = env->NewObject( DoubleClass, initDouble, dValue);
        env->SetObjectField(PropertyDoubleObj, fidNumber, DoubleObject);
        env->DeleteLocalRef(DoubleObject);
    }
    else
    {
        jclass DoubleCls = (env)->GetObjectClass(DoubleObject);
        jfieldID fidNumber_double = (env)->GetFieldID(DoubleCls, "value", "D");
        if (fidNumber_double == 0)
        {
            CreateException(env, "Failed to query value field from Double class.");
            return;
        }

        env->SetDoubleField(DoubleObject, fidNumber_double, dValue);
    }

}


来源:https://stackoverflow.com/questions/34812975/how-can-i-set-the-value-of-double-type-variable-of-my-class-by-jni

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!