如何使用自定义 class 类型参数在 JNI 中调用 Java 函数

How to call a Java function in JNI with custom class type argument

我正在尝试使用自定义 class 类型参数从 C++ 调用 Java 函数。问题是 Java 函数在从 C++ 调用时获取垃圾值(代码如下所示)。如果我使用任何其他数据类型(String、float、int)的参数,相同的函数 printClassVar 可以正常工作。

这是Java代码

/*Filename: CallbacksExample.java*/

public class CallbacksExample {
    static {
        System.loadLibrary("myjni");
    }
    
    public static void main(String[] args) {
        new CallbacksExample().callback();
    }

    public native void callback(); 

    public static void printClassVar(Person person)
    {
        System.out.println("Got callback from C++: " + person.age );
    }
}
/*Filename: Person.java*/

public class Person {

    public int age;

    public Person() {
        this.age = 20;
    }

    public int value() {
        return age;
    }
}

这是 JNI 代码

/*Filename: CallbacksExample.cpp*/

#include <iostream>
#include <jni.h>
#include "CallbacksExample.h"

using namespace std;

class Person
{
    int age;

public:
    Person()
    {
        age = 5;
    }

    int value()
    {
        return age;
    }
};

JNIEXPORT void JNICALL
Java_CallbacksExample_callback(JNIEnv *env, jobject jthis)
{
    jclass thisClass = env->GetObjectClass(jthis);

    Person _per;
    Person* class_ptr = &_per;

    std::cout << class_ptr->value() << std::endl;     

    jmethodID printClassVar = env->GetStaticMethodID(thisClass, "printClassVar", "(LPerson;)V");
    if (NULL == printClassVar)
        return;

    env->CallVoidMethod(jthis, printClassVar, &class_ptr);
}

以上代码returns

Got callback from C++: 1679598160 (Or any garbage signed int value)

这是在 C++ 中使用 class 类型参数调用 Java 函数的正确方法

/*Filename: CallbacksExample.java*/

public class CallbacksExample {
    static {
        System.loadLibrary("myjni");
    }
    
    public static void main(String[] args) {
        new CallbacksExample().callback();
    }

    public native void callback(); 

    public static void printClassVar(Person person)
    {
        System.out.println("Got callback from C++: " + person.age );
    }
}
/*Filename: Person.java*/

public class Person {

    public int age;

    public Person() {
        this.age = 20;
    }

    public void set(int x)
    {
        age = x;
    }

    public int value() {
        return age;
    }

}
/*Filename: CallbacksExample.cpp*/

#include <iostream>
#include <jni.h>
#include "CallbacksExample.h"

JNIEXPORT void JNICALL
Java_CallbacksExample_callback(JNIEnv *env, jobject jthis)
{
    jclass thisClass = env->GetObjectClass(jthis);

    jclass personClass = env->FindClass("Person");

    jmethodID class_constructor = env->GetMethodID(personClass, "<init>", "()V"); // no parameters
    jobject personObj = env->NewObject(personClass, class_constructor);

    auto personMethod = env->GetMethodID(personClass, "set", "(I)V");
    env->CallVoidMethod(personObj, personMethod, 15);

    jmethodID printClassVar = env->GetStaticMethodID(thisClass, "printClassVar", "(LPerson;)V");
    if (NULL == printClassVar)
        return;

    env->CallVoidMethod(jthis, printClassVar, personObj);
}