[hal] Allocate CANStreamMessage in JNI if null (#6107)

This commit is contained in:
Thad House
2023-12-28 22:50:57 -08:00
committed by GitHub
parent 6a2d3c30a6
commit 9333951736
3 changed files with 17 additions and 2 deletions

View File

@@ -160,8 +160,15 @@ Java_edu_wpi_first_hal_can_CANJNI_readCANStreamSession
JLocal<jobject> elem{
env, static_cast<jstring>(env->GetObjectArrayElement(messages, i))};
if (!elem) {
// TODO decide if should throw
continue;
// If element doesn't exist, construct it in place. If that fails, we are
// OOM, just return
elem = JLocal<jobject>{env, CreateCANStreamMessage(env)};
if (elem) {
std::printf("Allocated and set object\n");
env->SetObjectArrayElement(messages, i, elem);
} else {
return 0;
}
}
JLocal<jbyteArray> toSetArray{
env, SetCANStreamObject(env, elem, msg->dataSize, msg->messageID,

View File

@@ -365,6 +365,12 @@ jobject CreatePowerDistributionVersion(JNIEnv* env, uint32_t firmwareMajor,
static_cast<jint>(hardwareMajor), static_cast<jint>(uniqueId));
}
jobject CreateCANStreamMessage(JNIEnv* env) {
static jmethodID constructor =
env->GetMethodID(canStreamMessageCls, "<init>", "()V");
return env->NewObject(canStreamMessageCls, constructor);
}
JavaVM* GetJVM() {
return jvm;
}

View File

@@ -93,6 +93,8 @@ jobject CreatePowerDistributionVersion(JNIEnv* env, uint32_t firmwareMajor,
uint32_t hardwareMajor,
uint32_t uniqueId);
jobject CreateCANStreamMessage(JNIEnv* env);
JavaVM* GetJVM();
} // namespace hal