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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2022 Benjamin Fry <benjaminfry@me.com>
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use std::{
    any::Any,
    borrow::Cow,
    fmt,
    panic::{self, PanicInfo, UnwindSafe},
};

use jni::{
    objects::{JObject, JThrowable},
    strings::JNIString,
    sys::jarray,
    JNIEnv, JavaVM,
};

use crate::NullObject;

pub fn get_panic_message(message: &'_ (dyn Any + Send)) -> Cow<'_, str> {
    match message {
        _ if message.is::<&'static str>() => {
            let msg: &'static str = message.downcast_ref::<&str>().expect("failed to downcast");
            msg.into()
        }
        _ if message.is::<String>() => {
            let msg: &str = message
                .downcast_ref::<String>()
                .expect("failed to downcast");
            msg.into()
        }
        _ => format!("unknown panic: {:?}", message.type_id()).into(),
    }
}

/// This panic hook can add a bit more information than the catch_unwind, which doesn't get the full panic_info
pub fn register_panic_hook(vm: JavaVM) {
    panic::set_hook(Box::new(move |panic_info: &PanicInfo| {
        let env = vm.get_env().expect("not called in a JVM context");

        // we don't want to overwrite an existing exception...
        if !env.exception_check().unwrap_or(true) {
            let msg = get_panic_message(panic_info.payload());
            let (file, line, column) = panic_info
                .location()
                .map(|l| (l.file(), l.line(), l.column()))
                .unwrap_or_default();

            let msg = format!("panic '{msg}' at {file}:{line}:{column}");
            env.throw_new("java/lang/RuntimeException", msg)
                .expect("failed to throw exception");
        }
    }));
}

/// Catches and potential panics, and then converts them to a RuntimeException in Java.
///
/// * `R` - must implement `Default` in order to allow the (unused) default return value in the case of an exception
pub fn catch_panic_and_throw<F: FnOnce() -> R + UnwindSafe, R: NullObject>(
    env: JNIEnv<'_>,
    f: F,
) -> R {
    let result = std::panic::catch_unwind(f);

    match result {
        Ok(r) => r,
        Err(e) => {
            // we don't want to overwrite an existing exception...
            if !env.exception_check().unwrap_or(true) {
                let msg = get_panic_message(&e);

                let msg = format!("panic '{msg}'");
                env.throw_new("java/lang/RuntimeException", msg)
                    .expect("failed to throw exception");
            }
            R::null()
        }
    }
}

pub trait Throwable: Sized {
    /// Throw a new exception.
    #[track_caller]
    fn throw<S: Into<JNIString>>(&self, env: JNIEnv<'_>, msg: S) -> Result<(), jni::errors::Error>;

    /// Tests the exception against this type to see if it's a correct exception
    fn catch<'j>(_env: JNIEnv<'j>, exception: JThrowable<'j>) -> Result<Self, JThrowable<'j>>;
}

pub struct AnyThrowable;

impl Throwable for AnyThrowable {
    /// Throw a new exception.
    #[track_caller]
    fn throw<S: Into<JNIString>>(&self, env: JNIEnv<'_>, msg: S) -> Result<(), jni::errors::Error> {
        env.throw_new("java/lang/RuntimeException", msg)
    }

    /// Tests the exception against this type to see if it's a correct exception
    fn catch<'j>(_env: JNIEnv<'j>, _exception: JThrowable<'j>) -> Result<Self, JThrowable<'j>> {
        Ok(Self)
    }
}

pub struct Error<E: Throwable> {
    kind: E,
    msg: Cow<'static, str>,
}

impl<E: Throwable> Error<E> {
    pub fn new<S: Into<Cow<'static, str>>>(kind: E, msg: S) -> Self {
        let msg = msg.into();
        Self { kind, msg }
    }

    #[track_caller]
    pub fn throw(&self, env: JNIEnv<'_>) -> Result<(), jni::errors::Error> {
        <E as Throwable>::throw(&self.kind, env, &self.msg)
    }
}

/// A type that represents a known Exception type from Java.
pub struct Exception<'j, T: Throwable> {
    env: JNIEnv<'j>,
    exception: JThrowable<'j>,
    throwable: T,
}

impl<'j, T: Throwable + Copy> Exception<'j, T> {
    pub fn exception(&self) -> JThrowable<'j> {
        self.exception
    }

    pub fn throwable(&self) -> T {
        self.throwable
    }
}

impl<'j, T: Throwable> Exception<'j, T> {
    /// Throw a new exception.
    #[track_caller]
    pub fn throw<S: Into<JNIString>>(
        &self,
        env: JNIEnv<'_>,
        msg: S,
    ) -> Result<(), jni::errors::Error> {
        self.throwable.throw(env, msg)
    }

    /// Tests the exception against this type to see if it's a correct exception
    pub fn catch(env: JNIEnv<'j>, exception: JThrowable<'j>) -> Result<Self, JThrowable<'j>> {
        let throwable = T::catch(env, exception)?;

        Ok(Self {
            env,
            exception,
            throwable,
        })
    }
}

impl<'j, T: Throwable> fmt::Display for Exception<'j, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        if self.exception.is_null() {
            write!(f, "null exception thrown")?;
            return Ok(());
        }

        let mut exception = self.exception;

        // loop through all causes
        for i in 0usize.. {
            let ex_or_cause = if i == 0 { "exception" } else { "cause" };

            let clazz = crate::get_class_name(self.env, JObject::from(exception).into())
                .map_err(|_| fmt::Error)?;

            let message = crate::call_string_method(&self.env, exception.into(), "getMessage")
                .map_err(|_| fmt::Error)?;

            if let Some(message) = message {
                writeln!(f, "{ex_or_cause}: {clazz}: {}", Cow::from(&message))?;
            } else {
                writeln!(f, "{ex_or_cause}: {clazz}")?;
            };

            let trace = self
                .env
                .call_method(
                    JObject::from(exception),
                    "getStackTrace",
                    "()[Ljava/lang/StackTraceElement;",
                    &[],
                )
                .map_err(|_| fmt::Error)?
                .l()
                .map_err(|_| fmt::Error)?;

            if !trace.is_null() {
                let trace = *trace as jarray;
                let len = self.env.get_array_length(trace).map_err(|_| fmt::Error)?;

                for i in 0..len as usize {
                    let stack_element = self
                        .env
                        .get_object_array_element(trace, i as i32)
                        .map_err(|_| fmt::Error)?;

                    let stack_str = crate::call_string_method(&self.env, stack_element, "toString")
                        .map_err(|_| fmt::Error)?;

                    if let Some(stack_str) = stack_str {
                        writeln!(f, "\t{}", Cow::from(&stack_str))?;
                    }
                }
            }

            // continue the going through the causes
            let cause = self
                .env
                .call_method(
                    JObject::from(exception),
                    "getCause",
                    "()Ljava/lang/Throwable;",
                    &[],
                )
                .map_err(|_| fmt::Error)?;

            exception = cause.l().map(Into::into).map_err(|_| fmt::Error)?;
        }

        Ok(())
    }
}

impl<'j, T: Throwable> fmt::Debug for Exception<'j, T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
        <Self as fmt::Display>::fmt(self, f)
    }
}