1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::{
    descriptors::Desc,
    errors::*,
    objects::{JClass, JObject, JThrowable, JValue},
    strings::JNIString,
    JNIEnv,
};

const DEFAULT_EXCEPTION_CLASS: &str = "java/lang/RuntimeException";

impl<'a, 'c, C, M> Desc<'a, JThrowable<'a>> for (C, M)
where
    C: Desc<'a, JClass<'c>>,
    M: Into<JNIString>,
{
    fn lookup(self, env: &JNIEnv<'a>) -> Result<JThrowable<'a>> {
        let jmsg: JObject = env.new_string(self.1)?.into();
        let obj: JThrowable = env
            .new_object(self.0, "(Ljava/lang/String;)V", &[JValue::from(jmsg)])?
            .into();
        Ok(obj)
    }
}

impl<'a> Desc<'a, JThrowable<'a>> for Exception {
    fn lookup(self, env: &JNIEnv<'a>) -> Result<JThrowable<'a>> {
        (self.class, self.msg).lookup(env)
    }
}

impl<'a, 'b> Desc<'a, JThrowable<'a>> for &'b str {
    fn lookup(self, env: &JNIEnv<'a>) -> Result<JThrowable<'a>> {
        (DEFAULT_EXCEPTION_CLASS, self).lookup(env)
    }
}

impl<'a> Desc<'a, JThrowable<'a>> for String {
    fn lookup(self, env: &JNIEnv<'a>) -> Result<JThrowable<'a>> {
        (DEFAULT_EXCEPTION_CLASS, self).lookup(env)
    }
}

impl<'a, 'b> Desc<'a, JThrowable<'a>> for JNIString {
    fn lookup(self, env: &JNIEnv<'a>) -> Result<JThrowable<'a>> {
        (DEFAULT_EXCEPTION_CLASS, self).lookup(env)
    }
}