use std::convert::From;
use std::sync::Arc;
use JavaVM;
use JNIEnv;
use errors::Result;
use objects::JObject;
use sys;
#[derive(Clone)]
pub struct GlobalRef {
inner: Arc<GlobalRefGuard>
}
struct GlobalRefGuard {
obj: JObject<'static>,
vm: JavaVM,
}
unsafe impl Send for GlobalRef {}
impl<'a> From<&'a GlobalRef> for JObject<'a> {
fn from(other: &'a GlobalRef) -> JObject<'a> {
other.as_obj()
}
}
impl GlobalRef {
pub(crate) unsafe fn new(vm: JavaVM, obj: sys::jobject) -> Self {
GlobalRef {
inner: Arc::new(GlobalRefGuard::new(vm, obj)),
}
}
pub fn as_obj<'a>(&'a self) -> JObject<'a> {
self.inner.as_obj()
}
}
impl GlobalRefGuard {
unsafe fn new(vm: JavaVM, obj: sys::jobject) -> Self {
GlobalRefGuard {
obj: JObject::from(obj),
vm,
}
}
pub fn as_obj<'a>(&'a self) -> JObject<'a> {
self.obj
}
}
impl Drop for GlobalRefGuard {
fn drop(&mut self) {
fn drop_impl(env: &JNIEnv, global_ref: JObject) -> Result<()> {
let internal = env.get_native_interface();
unsafe {
jni_unchecked!(internal, DeleteGlobalRef, global_ref.into_inner());
check_exception!(internal);
}
Ok(())
}
let res = match self.vm.get_env() {
Ok(env) => drop_impl(&env, self.as_obj()),
Err(_) => {
warn!("Dropping a GlobalRef in a detached thread. Fix your code if this message appears frequently (see the GlobalRef docs).");
self.vm
.attach_current_thread()
.and_then(|env| drop_impl(&env, self.as_obj()))
}
};
if let Err(err) = res {
debug!("error dropping global ref: {:#?}", err);
}
}
}