Files
allwpilib/wpimath/src/main/native/cpp/jni/WPIMathJNI_Exceptions.cpp
Tyler Veness b7657a8e28 [wpimath] Split WPIMathJNI into logical chunks (#5552)
This makes things easier to find, and speeds up compilation.
2023-08-29 09:00:19 -07:00

57 lines
1.3 KiB
C++

// Copyright (c) FIRST and other WPILib contributors.
// Open Source Software; you can modify and/or share it under the terms of
// the WPILib BSD license file in the root directory of this project.
#include "WPIMathJNI_Exceptions.h"
#include <jni.h>
using namespace wpi::java;
//
// Globals and load/unload
//
JException illegalArgEx;
JException ioEx;
JException trajectorySerializationEx;
static const JExceptionInit exceptions[] = {
{"java/lang/IllegalArgumentException", &illegalArgEx},
{"java/io/IOException", &ioEx},
{"edu/wpi/first/math/trajectory/"
"TrajectoryUtil$TrajectorySerializationException",
&trajectorySerializationEx}};
extern "C" {
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return JNI_ERR;
}
// Cache references to exceptions
for (auto& c : exceptions) {
*c.cls = JException(env, c.name);
if (!*c.cls) {
return JNI_ERR;
}
}
return JNI_VERSION_1_6;
}
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
return;
}
// Delete global references
for (auto& c : exceptions) {
c.cls->free(env);
}
}
} // extern "C"