#[cfg(doc)]
use crate::errors::Error;
macro_rules! bind_exception {
($rust_type:ident => $java_type:literal $($rest:tt)*) => {
$crate::bind_java_type! {
pub $rust_type => $java_type,
is_instance_of = {
throwable: JThrowable
}
$($rest)*
}
impl<'local> $rust_type<'local> {
#[doc = concat!("Returns `Some(Cast<", stringify!($rust_type), ">)` if the throwable is an instance of this exception type")]
pub fn matches<'any, 'from>(
env: &crate::Env,
throwable: &'from crate::objects::JThrowable<'any>,
) -> $crate::errors::Result<Option<$crate::refs::Cast<'any, 'from, $rust_type<'any>>>>
{
if throwable.is_null() {
return Err($crate::errors::Error::NullPtr("Invalid null Throwable"));
}
let class = <$rust_type as $crate::refs::Reference>::lookup_class(
env,
&Default::default(),
)?;
let class: &$crate::objects::JClass = &class;
if env.is_instance_of_class(throwable, class)? {
return Ok(Some(unsafe {
$crate::refs::Cast::new_unchecked(throwable)
}));
} else {
return Ok(None);
}
}
}
};
}
macro_rules! bind_basic_exception {
($rust_type:ident => $java_type:literal $($rest:tt)*) => {
bind_exception! {
$rust_type => $java_type,
constructors {
fn new_null(),
fn new(msg: JString),
}
$($rest)*
}
};
}
bind_exception! {
JArrayIndexOutOfBoundsException => "java.lang.ArrayIndexOutOfBoundsException",
constructors {
fn new_null(),
fn new(msg: JString),
fn new_for_index(index: jint),
}
}
bind_basic_exception! { JArrayStoreException => "java.lang.ArrayStoreException" }
bind_basic_exception! { JClassCircularityError => "java.lang.ClassCircularityError" }
bind_basic_exception! { JClassFormatError => "java.lang.ClassFormatError" }
bind_exception! {
JExceptionInInitializerError => "java.lang.ExceptionInInitializerError",
constructors {
fn new_null(),
fn new(msg: JString),
fn new_with_exception(cause: JThrowable)
},
methods {
fn get_cause() -> JThrowable,
fn get_exception() -> JThrowable
}
}
bind_basic_exception! {
JClassNotFoundException => "java.lang.ClassNotFoundException",
constructors {
fn new_null(),
fn new(msg: JString),
fn new_with_cause(msg: JString, cause: JThrowable),
},
methods {
fn get_cause() -> JThrowable,
}
}
bind_exception! {
JIllegalArgumentException => "java.lang.IllegalArgumentException",
constructors {
fn new_null(),
fn new(msg: JString),
fn new_with_cause(msg: JString, cause: JThrowable),
fn new_with_only_cause(cause: JThrowable)
}
}
bind_basic_exception! { JIllegalMonitorStateException => "java.lang.IllegalMonitorStateException" }
bind_basic_exception! { JInstantiationException => "java.lang.InstantiationException" }
bind_exception! {
JLinkageError => "java.lang.LinkageError",
constructors {
fn new_null(),
fn new(msg: JString),
fn new_with_cause(msg: JString, cause: JThrowable),
},
}
bind_basic_exception! { JNoClassDefFoundError => "java.lang.NoClassDefFoundError" }
bind_basic_exception! { JNoSuchFieldError => "java.lang.NoSuchFieldError" }
bind_basic_exception! { JNoSuchMethodError => "java.lang.NoSuchMethodError" }
bind_basic_exception! { JNumberFormatException => "java.lang.NumberFormatException" }
bind_basic_exception! { JOutOfMemoryError => "java.lang.OutOfMemoryError" }
bind_exception! {
JRuntimeException => "java.lang.RuntimeException",
constructors {
fn new_null(),
fn new(msg: JString),
fn new_with_cause(msg: JString, cause: JThrowable)
}
}
bind_exception! {
JSecurityException => "java.lang.SecurityException",
constructors {
fn new_null(),
fn new(msg: JString),
fn new_with_cause(msg: JString, cause: JThrowable),
fn new_with_only_cause(cause: JThrowable)
}
}
bind_exception! {
JStringIndexOutOfBoundsException => "java.lang.StringIndexOutOfBoundsException",
constructors {
fn new_null(),
fn new(msg: JString),
fn new_for_index(index: jint),
}
}