Struct jni::JNIEnv [] [src]

pub struct JNIEnv<'a> {
    pub internal: *mut JNIEnv,
    // some fields omitted
}

FFI-compatible JNIEnv struct. You can safely use this as the JNIEnv argument to exported methods that will be called by java. This is where most of the magic happens. All methods on this object are wrappers around JNI functions, so the documentation on their behavior is still pretty applicable.

Since we're calling into the JVM with this, many methods also have the potential to cause an exception to get thrown. If this is the case, an Err result will be returned with the error kind JavaException. Note that this will not clear the exception - it's up to the caller to decide whether to do so or to let it continue being thrown.

Because null pointers are a thing in Java, this also converts them to an Err result with the kind NullPtr. This may occur when either a null argument is passed to a method or when a null would be returned. Where applicable, the null error is changed to a more applicable error type, such as MethodNotFound.

Fields

Methods

impl<'a> JNIEnv<'a>
[src]

Get the java version that we're being executed from. This is encoded and will need to be checked against constants from the sys module.

TODO: convert this to something more usable.

Define a new java class. See the JNI docs for more details - I've never had occasion to use this and haven't researched it fully.

Look up a class by name. The argument to this will be something like java/lang/String. This can also take a concrete JClass, in which case it simply returns it after doing a null check. This is so that it can be generic over both concrete classes and class descriptor strings. Methods with class arguments should therefore take IntoClassDesc instead, and use ths when an actual class is needed. That way, optimizations such as reusing a class object to look up multiple methods can be done.

Example

let class: JClass<'a> = env.find_class("java/lang/String");

Get the superclass for a particular class. As with find_class, takes a descriptor.

Tests whether class1 is assignable from class2.

Raise an exception from an existing object. This will continue being thrown in java unless exception_clear is called.

Create and throw a new exception from a class descriptor and an error message.

Check whether or not an exception is currently in the process of being thrown. An exception is in this state from the time it gets thrown and not caught in a java function until exception_clear is called.

Print exception information to the console.

Clear an exception in the process of being thrown. If this is never called, the exception will continue being thrown when control is returned to java.

Abort the JVM with an error message.

Check to see if an exception is being thrown. This only differs from exception_occurred in that it doesn't return the actual thrown exception.

Turns an object into a global ref. This has the benefit of removing the lifetime bounds since it's guaranteed to not get GC'd by java. It releases the GC pin upon being dropped.

Allocates a new object from a class descriptor without running a constructor.

Look up a method by class descriptor (or concrete class), name, and signature. Like find_class, this is generic over descriptors and concrete JMethodID objects and can take both. If given a concrete object, it simply returns it.

Example

let method_id: JMethodID = env.get_method_id(
    ("java/lang/String", "getString", "()Ljava/lang/String;"),
);

Get the class for an object.

Call a static method in an unsafe manner. This does nothing to check whether the method is valid to call on the class, whether the return type is correct, or whether the number of args is valid for the method.

Under the hood, this simply calls the CallStatic<Type>MethodA method with the provided arguments.

Call an object method in an unsafe manner. This does nothing to check whether the method is valid to call on the object, whether the return type is correct, or whether the number of args is valid for the method.

Under the hood, this simply calls the Call<Type>MethodA method with the provided arguments.

Calls an object method safely. This comes with a number of lookups/checks. It

  • Parses the type signature to find the number of arguments and return type
  • Looks up the JClass for the given object.
  • Looks up the JMethodID for the class/name/signature combination
  • Ensures that the number of args matches the signature
  • Calls call_method_unsafe with the verified safe arguments.

Note: this may cause a java exception if the arguments are the wrong type, in addition to if the method itself throws.

Calls a static method safely. This comes with a number of lookups/checks. It

  • Parses the type signature to find the number of arguments and return type
  • Looks up the JMethodID for the class/name/signature combination
  • Ensures that the number of args matches the signature
  • Calls call_method_unsafe with the verified safe arguments.

Note: this may cause a java exception if the arguments are the wrong type, in addition to if the method itself throws.

Create a new object using a constructor. This is done safely using checks similar to those in call_static_method.

Cast a JObject to a JMap. This won't throw exceptions or return errors in the event that the object isn't actually a map, but the methods on the resulting map object will.

Get a JavaStr from a JString. This allows conversions from java string objects to rust strings.

This entails a call to GetStringUTFChars and only decodes java's modified UTF-8 format on conversion to a rust-compatible string.

Create a new java string object from a rust string. This requires a re-encoding of rusts real UTF-8 strings to java's modified UTF-8 format.

Trait Implementations

impl<'a> From<*mut JNIEnv> for JNIEnv<'a>
[src]

Performs the conversion.