toad_jni/java/io/
io_exception.rs

1use crate::java::lang::Throwable;
2use crate::java::{self, Object};
3
4/// `java.io.IOException`
5pub struct IOException(java::lang::Object);
6
7impl IOException {
8  /// [`IOException(String)`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/io/IOException.html#<init>(java.lang.String))
9  pub fn new(e: &mut java::Env, message: impl ToString) -> Self {
10    static CTOR: java::Constructor<IOException, fn(String)> = java::Constructor::new();
11    CTOR.invoke(e, message.to_string())
12  }
13
14  /// [`IOException(String, Throwable)`](https://docs.oracle.com/en/java/javase/19/docs/api/java.base/java/io/IOException.html#<init>(java.lang.String,java.lang.Throwable))
15  pub fn new_caused_by(e: &mut java::Env, message: impl ToString, err: Throwable) -> Self {
16    static CTOR: java::Constructor<IOException, fn(String, Throwable)> = java::Constructor::new();
17    CTOR.invoke(e, message.to_string(), err)
18  }
19
20  /// Cast self to Throwable
21  pub fn to_throwable(&self, e: &mut java::Env) -> Throwable {
22    self.downcast_ref(e).upcast_to::<Throwable>(e)
23  }
24}
25
26impl core::fmt::Debug for IOException {
27  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
28    write!(f, "{:?}", self.to_throwable(&mut java::env()))
29  }
30}
31
32java::object_newtype!(IOException);
33impl java::Class for IOException {
34  const PATH: &'static str = "java/io/IOException";
35}
36
37impl<StepError> toad::platform::PlatformError<StepError, Throwable> for IOException
38  where StepError: core::fmt::Debug
39{
40  fn msg_to_bytes(e: toad_msg::to_bytes::MessageToBytesError) -> Self {
41    Self::new(&mut java::env(), format!("{:?}", e))
42  }
43
44  fn step(e: StepError) -> Self {
45    Self::new(&mut java::env(), format!("{:?}", e))
46  }
47
48  fn socket(e: Throwable) -> Self {
49    Self::new_caused_by(&mut java::env(), "", e)
50  }
51
52  fn clock(e: embedded_time::clock::Error) -> Self {
53    Self::new(&mut java::env(), format!("{:?}", e))
54  }
55}