2024-09-21 17:52:20 +02:00
|
|
|
pub use jni_toolbox_macro::jni;
|
2024-09-23 00:23:42 +02:00
|
|
|
use jni::objects::{JObject, JObjectArray, JString};
|
2024-09-21 17:01:00 +02:00
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
/// An error that is meant to be used with jni-toolbox.
|
2024-09-21 17:52:20 +02:00
|
|
|
pub trait JniToolboxError: std::error::Error {
|
2024-09-23 00:50:52 +02:00
|
|
|
/// The Java class for the matching exception.
|
2024-09-21 17:52:20 +02:00
|
|
|
fn jclass(&self) -> String;
|
2024-09-21 17:01:00 +02:00
|
|
|
}
|
2024-09-21 18:48:30 +02:00
|
|
|
|
|
|
|
impl JniToolboxError for jni::errors::Error {
|
|
|
|
fn jclass(&self) -> String {
|
|
|
|
"java/lang/RuntimeException".to_string()
|
|
|
|
}
|
|
|
|
}
|
2024-09-21 20:15:04 +02:00
|
|
|
|
|
|
|
impl JniToolboxError for jni::errors::JniError {
|
|
|
|
fn jclass(&self) -> String {
|
|
|
|
"java/lang/RuntimeException".to_string()
|
|
|
|
}
|
|
|
|
}
|
2024-09-22 00:09:46 +02:00
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
/// Used in the generated code to have proper type bindings. You probably didn't want
|
|
|
|
/// to call this directly.
|
2024-09-22 00:09:46 +02:00
|
|
|
pub fn from_java_static<'j, T: FromJava<'j>>(env: &mut jni::JNIEnv<'j>, val: T::T) -> Result<T, jni::errors::Error> {
|
|
|
|
T::from_java(env, val)
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
/// Specifies how a Java type should be converted before being fed to Rust.
|
2024-09-22 00:09:46 +02:00
|
|
|
pub trait FromJava<'j> : Sized {
|
2024-09-23 00:50:52 +02:00
|
|
|
/// The JNI type representing the input.
|
2024-09-22 00:09:46 +02:00
|
|
|
type T : Sized;
|
2024-09-23 00:50:52 +02:00
|
|
|
/// Attempts to convert this Java object into its Rust counterpart.
|
2024-09-22 00:09:46 +02:00
|
|
|
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error>;
|
|
|
|
}
|
|
|
|
|
2024-09-22 01:37:03 +02:00
|
|
|
macro_rules! auto_from_java {
|
2024-09-22 20:40:13 +02:00
|
|
|
($t: ty, $j: ty) => {
|
2024-09-22 01:37:03 +02:00
|
|
|
impl<'j> FromJava<'j> for $t {
|
|
|
|
type T = $j;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from_java(_: &mut jni::JNIEnv, value: Self::T) -> Result<Self, jni::errors::Error> {
|
|
|
|
Ok(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2024-09-22 00:09:46 +02:00
|
|
|
|
2024-09-22 01:37:03 +02:00
|
|
|
auto_from_java!(i64, jni::sys::jlong);
|
|
|
|
auto_from_java!(i32, jni::sys::jint);
|
|
|
|
auto_from_java!(i16, jni::sys::jshort);
|
2024-09-23 00:23:42 +02:00
|
|
|
auto_from_java!(i8, jni::sys::jbyte);
|
2024-09-22 01:37:03 +02:00
|
|
|
auto_from_java!(f32, jni::sys::jfloat);
|
|
|
|
auto_from_java!(f64, jni::sys::jdouble);
|
2024-09-23 00:23:42 +02:00
|
|
|
auto_from_java!(JObject<'j>, JObject<'j>);
|
2024-09-22 00:09:46 +02:00
|
|
|
|
|
|
|
impl<'j> FromJava<'j> for bool {
|
|
|
|
type T = jni::sys::jboolean;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn from_java(_: &mut jni::JNIEnv, value: Self::T) -> Result<Self, jni::errors::Error> {
|
|
|
|
Ok(value == 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'j> FromJava<'j> for String {
|
2024-09-22 20:40:13 +02:00
|
|
|
type T = JString<'j>;
|
2024-09-22 00:09:46 +02:00
|
|
|
|
|
|
|
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error> {
|
|
|
|
if value.is_null() { return Err(jni::errors::Error::NullPtr("string can't be null")) };
|
|
|
|
Ok(env.get_string(&value)?.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:23:42 +02:00
|
|
|
impl<'j, T: FromJava<'j, T: std::convert::AsRef<JObject<'j>>>> FromJava<'j> for Option<T> {
|
|
|
|
type T = T::T;
|
2024-09-22 00:09:46 +02:00
|
|
|
|
|
|
|
fn from_java(env: &mut jni::JNIEnv<'j>, value: Self::T) -> Result<Self, jni::errors::Error> {
|
2024-09-23 00:23:42 +02:00
|
|
|
if value.as_ref().is_null() { return Ok(None) };
|
2024-09-22 01:37:03 +02:00
|
|
|
Ok(Some(T::from_java(env, value)?))
|
2024-09-22 00:09:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-22 16:41:11 +02:00
|
|
|
#[cfg(feature = "uuid")]
|
|
|
|
impl<'j> FromJava<'j> for uuid::Uuid {
|
2024-09-22 20:40:13 +02:00
|
|
|
type T = JObject<'j>;
|
2024-09-22 16:41:11 +02:00
|
|
|
fn from_java(env: &mut jni::JNIEnv<'j>, uuid: Self::T) -> Result<Self, jni::errors::Error> {
|
|
|
|
let lsb = u64::from_ne_bytes(
|
|
|
|
env.call_method(&uuid, "getLeastSignificantBits", "()J", &[])?
|
|
|
|
.j()?
|
|
|
|
.to_ne_bytes()
|
|
|
|
);
|
|
|
|
|
|
|
|
let msb = u64::from_ne_bytes(
|
|
|
|
env.call_method(&uuid, "getMostSignificantBits", "()J", &[])?
|
|
|
|
.j()?
|
|
|
|
.to_ne_bytes()
|
|
|
|
);
|
|
|
|
|
|
|
|
Ok(uuid::Uuid::from_u64_pair(msb, lsb))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
/// Specifies how a Rust type should be converted into a Java primitive.
|
2024-09-22 20:40:13 +02:00
|
|
|
pub trait IntoJavaPrimitive<'j> {
|
2024-09-23 00:50:52 +02:00
|
|
|
/// The JNI type representing the output.
|
2024-09-22 20:40:13 +02:00
|
|
|
type T;
|
2024-09-23 00:50:52 +02:00
|
|
|
/// Attempts to convert this Rust object into a Java primitive.
|
2024-09-23 00:23:42 +02:00
|
|
|
fn into_java(self, _: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error>;
|
2024-09-22 01:37:03 +02:00
|
|
|
}
|
2024-09-22 00:09:46 +02:00
|
|
|
|
2024-09-22 01:37:03 +02:00
|
|
|
macro_rules! auto_into_java {
|
2024-09-22 20:40:13 +02:00
|
|
|
($t: ty, $j: ty) => {
|
|
|
|
impl<'j> IntoJavaPrimitive<'j> for $t {
|
2024-09-22 01:37:03 +02:00
|
|
|
type T = $j;
|
|
|
|
|
2024-09-23 00:23:42 +02:00
|
|
|
fn into_java(self, _: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
|
2024-09-22 01:37:03 +02:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2024-09-22 00:09:46 +02:00
|
|
|
|
2024-09-23 00:23:42 +02:00
|
|
|
// TODO: primitive arrays!
|
|
|
|
|
2024-09-22 01:37:03 +02:00
|
|
|
auto_into_java!(i64, jni::sys::jlong);
|
|
|
|
auto_into_java!(i32, jni::sys::jint);
|
|
|
|
auto_into_java!(i16, jni::sys::jshort);
|
2024-09-23 00:23:42 +02:00
|
|
|
auto_into_java!(i8, jni::sys::jbyte);
|
2024-09-22 01:37:03 +02:00
|
|
|
auto_into_java!(f32, jni::sys::jfloat);
|
|
|
|
auto_into_java!(f64, jni::sys::jdouble);
|
|
|
|
auto_into_java!((), ());
|
2024-09-22 00:09:46 +02:00
|
|
|
|
2024-09-22 20:40:13 +02:00
|
|
|
impl<'j> IntoJavaPrimitive<'j> for bool {
|
2024-09-22 01:37:03 +02:00
|
|
|
type T = jni::sys::jboolean;
|
2024-09-22 00:09:46 +02:00
|
|
|
|
2024-09-22 01:37:03 +02:00
|
|
|
#[inline]
|
2024-09-23 00:23:42 +02:00
|
|
|
fn into_java(self, _: &mut jni::JNIEnv) -> Result<Self::T, jni::errors::Error> {
|
2024-09-22 01:37:03 +02:00
|
|
|
Ok(if self { 1 } else { 0 })
|
|
|
|
}
|
|
|
|
}
|
2024-09-22 00:09:46 +02:00
|
|
|
|
2024-09-23 00:50:52 +02:00
|
|
|
/// Specifies how a Rust type should be converted into a Java object.
|
2024-09-22 20:40:13 +02:00
|
|
|
pub trait IntoJavaObject<'j> {
|
|
|
|
type T: std::convert::AsRef<JObject<'j>>;
|
2024-09-23 00:50:52 +02:00
|
|
|
/// The Java class associated with this type.
|
2024-09-22 20:40:13 +02:00
|
|
|
const CLASS: &'static str;
|
2024-09-23 00:50:52 +02:00
|
|
|
/// Attempts to convert this Rust object into a Java object.
|
2024-09-22 20:40:13 +02:00
|
|
|
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'j> IntoJavaObject<'j> for &str {
|
|
|
|
type T = JString<'j>;
|
|
|
|
const CLASS: &'static str = "java/lang/String";
|
2024-09-22 01:37:03 +02:00
|
|
|
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
|
2024-09-22 20:40:13 +02:00
|
|
|
env.new_string(self)
|
2024-09-22 01:37:03 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-22 00:09:46 +02:00
|
|
|
|
2024-09-22 20:40:13 +02:00
|
|
|
impl<'j> IntoJavaObject<'j> for String {
|
|
|
|
type T = JString<'j>;
|
|
|
|
const CLASS: &'static str = "java/lang/String";
|
2024-09-22 16:16:42 +02:00
|
|
|
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
|
|
|
|
self.as_str().into_java(env)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-22 20:40:13 +02:00
|
|
|
impl<'j, E: IntoJavaObject<'j>> IntoJavaObject<'j> for Vec<E> {
|
|
|
|
type T = JObjectArray<'j>;
|
|
|
|
const CLASS: &'static str = E::CLASS;
|
2024-09-22 01:37:03 +02:00
|
|
|
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
|
2024-09-22 20:40:13 +02:00
|
|
|
let mut array = env.new_object_array(self.len() as i32, E::CLASS, JObject::null())?;
|
2024-09-22 01:37:03 +02:00
|
|
|
for (n, el) in self.into_iter().enumerate() {
|
2024-09-22 18:53:39 +02:00
|
|
|
let el = el.into_java(env)?;
|
|
|
|
env.set_object_array_element(&mut array, n as i32, &el)?;
|
2024-09-22 01:37:03 +02:00
|
|
|
}
|
2024-09-22 20:40:13 +02:00
|
|
|
Ok(array)
|
2024-09-22 01:37:03 +02:00
|
|
|
}
|
2024-09-22 00:09:46 +02:00
|
|
|
}
|
|
|
|
|
2024-09-23 00:23:42 +02:00
|
|
|
impl<'j, E: std::convert::AsRef<JObject<'j>> + FromJavaRaw, T: IntoJavaObject<'j, T = E>> IntoJavaObject<'j> for Option<T> {
|
2024-09-22 20:40:13 +02:00
|
|
|
type T = E;
|
|
|
|
const CLASS: &'static str = T::CLASS;
|
2024-09-22 00:09:46 +02:00
|
|
|
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
|
2024-09-22 01:37:03 +02:00
|
|
|
match self {
|
|
|
|
Some(x) => x.into_java(env),
|
2024-09-23 00:23:42 +02:00
|
|
|
None => Ok(unsafe { E::from_java_raw(std::ptr::null_mut()) }) // safe, that's what JObject::null does
|
2024-09-22 01:37:03 +02:00
|
|
|
}
|
2024-09-22 00:09:46 +02:00
|
|
|
}
|
|
|
|
}
|
2024-09-22 16:41:11 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "uuid")]
|
2024-09-22 20:40:13 +02:00
|
|
|
impl<'j> IntoJavaObject<'j> for uuid::Uuid {
|
|
|
|
type T = jni::objects::JObject<'j>;
|
|
|
|
const CLASS: &'static str = "java/util/UUID";
|
2024-09-22 16:41:11 +02:00
|
|
|
fn into_java(self, env: &mut jni::JNIEnv<'j>) -> Result<Self::T, jni::errors::Error> {
|
2024-09-22 20:40:13 +02:00
|
|
|
let class = env.find_class(Self::CLASS)?;
|
2024-09-22 16:41:11 +02:00
|
|
|
let (msb, lsb) = self.as_u64_pair();
|
|
|
|
let msb = i64::from_ne_bytes(msb.to_ne_bytes());
|
|
|
|
let lsb = i64::from_ne_bytes(lsb.to_ne_bytes());
|
|
|
|
env.new_object(&class, "(JJ)V", &[jni::objects::JValueGen::Long(msb), jni::objects::JValueGen::Long(lsb)])
|
|
|
|
}
|
|
|
|
}
|
2024-09-22 18:53:39 +02:00
|
|
|
|
2024-09-22 20:40:13 +02:00
|
|
|
/// Needed internally to perform some operations.
|
2024-09-23 00:23:42 +02:00
|
|
|
trait FromJavaRaw {
|
|
|
|
unsafe fn from_java_raw(raw: jni::sys::jobject) -> Self;
|
2024-09-22 18:53:39 +02:00
|
|
|
}
|
|
|
|
|
2024-09-22 20:40:13 +02:00
|
|
|
macro_rules! auto_from_raw {
|
|
|
|
($type: ty) => {
|
2024-09-23 00:23:42 +02:00
|
|
|
impl FromJavaRaw for $type {
|
2024-09-22 20:40:13 +02:00
|
|
|
#[inline]
|
2024-09-23 00:23:42 +02:00
|
|
|
unsafe fn from_java_raw(raw: jni::sys::jobject) -> Self {
|
2024-09-22 20:40:13 +02:00
|
|
|
Self::from_raw(raw)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2024-09-22 18:53:39 +02:00
|
|
|
}
|
2024-09-22 20:40:13 +02:00
|
|
|
|
|
|
|
auto_from_raw!(JObject<'_>);
|
|
|
|
auto_from_raw!(JString<'_>);
|
|
|
|
auto_from_raw!(JObjectArray<'_>);
|
2024-09-23 00:23:42 +02:00
|
|
|
|
|
|
|
/// Intermediate trait used to guess the JNI return type.
|
|
|
|
/// Usually doesn't need to be manually implemented.
|
|
|
|
pub trait IntoJavaRaw {
|
|
|
|
type T;
|
|
|
|
fn into_java_raw(self) -> Self::T;
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! auto_into_raw_primitive {
|
|
|
|
($type: ty) => {
|
|
|
|
impl IntoJavaRaw for $type {
|
|
|
|
type T = $type;
|
|
|
|
fn into_java_raw(self) -> Self::T {
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_into_raw_primitive!(jni::sys::jlong);
|
|
|
|
auto_into_raw_primitive!(jni::sys::jint);
|
|
|
|
auto_into_raw_primitive!(jni::sys::jshort);
|
|
|
|
auto_into_raw_primitive!(jni::sys::jbyte);
|
|
|
|
auto_into_raw_primitive!(jni::sys::jdouble);
|
|
|
|
auto_into_raw_primitive!(jni::sys::jfloat);
|
|
|
|
auto_into_raw_primitive!(jni::sys::jboolean);
|
|
|
|
auto_into_raw_primitive!(());
|
|
|
|
|
|
|
|
macro_rules! auto_into_raw_object {
|
|
|
|
($lt: lifetime, $type: ty) => {
|
|
|
|
impl<'j> IntoJavaRaw for $type {
|
|
|
|
type T = jni::sys::jobject;
|
|
|
|
fn into_java_raw(self) -> Self::T {
|
|
|
|
self.as_raw()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto_into_raw_object!('j, JObject<'j>);
|
|
|
|
auto_into_raw_object!('j, JString<'j>);
|
|
|
|
auto_into_raw_object!('j, JObjectArray<'j>);
|