Struct NoException

Source
pub struct NoException<'env> { /* private fields */ }
Expand description

A token that represents that there is no pending Java exception in the current thread.

§Pending exceptions

When a JNI function is called, it can throw an exception. Then the current thread is said to have a pending exception. Most JNI functions must not be called when there is a pending exception. Read more about exception handling in JNI documentation.

§Exception tokens

rust-jni tries to push as many programming errors as possible from run-time to compile-time. To not allow a caller to call JNI methods when there is a pending exception, these methods will require the caller to provide a NoException token. The caller can obtain the token after attaching the thread to the Java VM:

use rust_jni::{AttachArguments, InitArguments, JavaVM, JniVersion};

let init_arguments = InitArguments::get_default(JniVersion::V8).unwrap();
let vm = JavaVM::create(&init_arguments).unwrap();
let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap();
let token = env.token();

Once obtained, the token can be used to call JNI methods:

let token = env.token();
let string = java::lang::String::empty(&env, &token).unwrap();

rust-jni follows Java semantics, where a method either returns a result or throws an exception. All Java methods return a JavaResult value, which is either an actual result or a Throwable value representing the exception thrown by this method call. Java methods never leave a pending exception, so they never consume the NoException token, but they always require it to be presented:

let token = env.token();
let string = java::lang::Class::find(&env, "java/lang/String", &token).unwrap();
let exception = java::lang::Class::find(&env, "invalid", &token).unwrap_err();

A token can not be obtained twice from a JniEnv value:

let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap();
let token = env.token();
let token = env.token(); // panics!

There is no possible way to obtain a token when there is a pending exception. The token is bound to the JniEnv object, so it can’t outlive it:


let token = {
    let env = vm.attach(&AttachArguments::new(&init_arguments)).unwrap();
    let token = env.token();
    token
}; // doesn't compile!

Some JNI methods can throw exceptions themselves. In this case the token will be consumed:

let token = env.token();
let exception = java::lang::String::empty(&env, &token).unwrap_err();
exception.throw(token);
java::lang::String::empty(&env, &token); // doesn't compile! Can't use the token any more.

Methods that consume the token will always return an Exception token. The Exception token can be unwrap-ped into a new NoException token and a Throwable value with the pending exception. Unwrapping the Exception token will clear the pending exception, so it is again safe to call JNI methods:

let token = env.token();
let exception = java::lang::Class::find(&env, "invalid", &token).unwrap_err();
let exception_token = exception.throw(token); // there is a pending exception now.
let (exception, new_token) = exception_token.unwrap();
java::lang::String::empty(&env, &new_token); // can call Java methods again.

Trait Implementations§

Source§

impl<'env> Debug for NoException<'env>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'env> Freeze for NoException<'env>

§

impl<'env> !RefUnwindSafe for NoException<'env>

§

impl<'env> !Send for NoException<'env>

§

impl<'env> !Sync for NoException<'env>

§

impl<'env> Unpin for NoException<'env>

§

impl<'env> UnwindSafe for NoException<'env>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.