typed_jni/builtin/
throwable.rs

1use core::fmt::{Debug, Display, Formatter};
2
3use typed_jni_core::{GlobalRef, LocalRef, Ref};
4
5use crate::{Object, ObjectType, Signature, Type, TypedObjectExt};
6
7pub struct JavaThrowable;
8
9impl Type for JavaThrowable {
10    const SIGNATURE: Signature = Signature::Object("java/lang/Throwable");
11}
12
13impl ObjectType for JavaThrowable {}
14
15impl<'env> Display for Object<LocalRef<'env>, JavaThrowable> {
16    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
17        let s = self.env().typed_to_string(self);
18
19        match s {
20            Ok(s) => f.write_str(&s),
21            Err(_) => f.write_str("<exception>"),
22        }
23    }
24}
25
26impl<'vm> Display for Object<GlobalRef<'vm>, JavaThrowable> {
27    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
28        let s = self
29            .vm()
30            .with_attached_thread(true, |env| env.typed_to_string(self).ok())
31            .ok()
32            .flatten();
33
34        match s {
35            Some(s) => f.write_str(&s),
36            None => f.write_str("<exception>"),
37        }
38    }
39}
40
41impl<R: Ref> Debug for Object<R, JavaThrowable>
42where
43    Self: Display,
44{
45    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
46        Display::fmt(self, f)
47    }
48}
49
50#[cfg(feature = "std")]
51impl<R: Ref> std::error::Error for Object<R, JavaThrowable> where Self: Display {}