I'm currently working with Djinni and would like to call Java methods from C++.
I have the following interface description file:
ExampleSO = interface +j {
PerformAddition(a: i32, b: i32): i32;
}
It generates these files:
src/main/cpp/ExampleSO.hpp
: C++ExampleSO
class containing a virtual destructor and a virtualPerformAddition
method.src/main/java/com/name/group/ExampleSO.java
: JavaExampleSO
abtract class containing apublic abstract PerformAddition
method.src/main/jni/NativeExampleSO.hpp
/.cpp
: JNI bindings.
What I want to do is create a new Java class that will extend the ExampleSO
Java class (as specified in the interface description with +j
), and be able to call these methods from a c++ file.
I can see in the JNI bindings that there is a public using CppType = std::shared_ptr<::ExampleSO>;
. Given the name, I assumed that this would be the way to call Java methods through the JNI bridge, but it results in a segfault when I'm trying to do something like :
// SampleClass.hpp
#include "ExampleSO.hpp"
class SampleClass: ExampleSO {
private:
NativeExampleSO::CppType neso;
public:
int32_t PerformAddition(int32_t a, int32_t b) override;
}
// SampleClass.cpp
#include "SampleClass.hpp"
int32_t SampleClass::PerformAddition(int32_t a, int32_t b) {
neso->PerformAddition(a, b); // Crash
}
Do I have to initialize this neso
field in some way?
Thanks in advance.
Edit: Here is the content of NativeExampleSO.hpp
(JNI bridge), it could make answering easier :
// AUTOGENERATED FILE - DO NOT MODIFY!
// This file generated by Djinni from ExampleSO.djinni
#pragma once
#include "ExampleSO.hpp"
#include "djinni_support.hpp"
namespace djinni_generated {
class NativeExampleSO final : ::djinni::JniInterface<::ExampleSO, NativeExampleSO> {
public:
using CppType = std::shared_ptr<::ExampleSO>;
using CppOptType = std::shared_ptr<::ExampleSO>;
using JniType = jobject;
using Boxed = NativeExampleSO;
~NativeExampleSO();
static CppType toCpp(JNIEnv* jniEnv, JniType j) { return ::djinni::JniClass<NativeExampleSO>::get()._fromJava(jniEnv, j); }
static ::djinni::LocalRef<JniType> fromCppOpt(JNIEnv* jniEnv, const CppOptType& c) { return {jniEnv, ::djinni::JniClass<NativeExampleSO>::get()._toJava(jniEnv, c)}; }
static ::djinni::LocalRef<JniType> fromCpp(JNIEnv* jniEnv, const CppType& c) { return fromCppOpt(jniEnv, c); }
private:
NativeExampleSO();
friend ::djinni::JniClass<NativeExampleSO>;
friend ::djinni::JniInterface<::ExampleSO, NativeExampleSO>;
class JavaProxy final : ::djinni::JavaProxyHandle<JavaProxy>, public ::ExampleSO
{
public:
JavaProxy(JniType j);
~JavaProxy();
int32_t PerformAddition(int32_t a, int32_t b) override;
private:
friend ::djinni::JniInterface<::ExampleSO, ::djinni_generated::NativeExampleSO>;
};
const ::djinni::GlobalRef<jclass> clazz { ::djinni::jniFindClass("com/name/group/ExampleSO") };
const jmethodID method_PerformAddition { ::djinni::jniGetMethodID(clazz.get(), "PerformAddition", "(II)I") };
};
} // namespace djinni_generated