pub struct Jvm { /* private fields */ }Expand description
Holds the assets for the JVM
Implementations§
Source§impl Jvm
impl Jvm
Sourcepub fn new(
jvm_options: &[String],
lib_name_to_load: Option<String>,
) -> Result<Jvm>
pub fn new( jvm_options: &[String], lib_name_to_load: Option<String>, ) -> Result<Jvm>
Creates a new Jvm.
Sourcepub fn attach_thread() -> Result<Jvm>
pub fn attach_thread() -> Result<Jvm>
Attaches the current thread to an active JavaVM
Sourcepub fn attach_thread_with_no_detach_on_drop() -> Result<Jvm>
pub fn attach_thread_with_no_detach_on_drop() -> Result<Jvm>
Attaches the current thread to an active JavaVM and instructs that the Jvm will detach the Java JVM from the thread when the rust Jvm is dropped.
This is useful when creating a Jvm while on a Thread that is created in the Java world. When this Jvm is dropped, we don’t want to detach the thread from the Java VM.
Sourcepub fn detach_thread_on_drop(&mut self, detach: bool)
pub fn detach_thread_on_drop(&mut self, detach: bool)
If false, the thread will not be detached when the Jvm is being dropped. This is useful when creating a Jvm while on a Thread that is created in the Java world. When this Jvm is dropped, we don’t want to detach the thread from the Java VM.
It prevents errors like: attempting to detach while still running code
pub unsafe fn try_from(jni_environment: *mut JNIEnv) -> Result<Jvm>
Sourcepub fn create_instance(
&self,
class_name: &str,
inv_args: &[impl Borrow<InvocationArg>],
) -> Result<Instance>
pub fn create_instance( &self, class_name: &str, inv_args: &[impl Borrow<InvocationArg>], ) -> Result<Instance>
Creates an Instance of the class class_name, passing an array of InvocationArgs to construct the instance.
Sourcepub fn static_class(&self, class_name: &str) -> Result<Instance>
pub fn static_class(&self, class_name: &str) -> Result<Instance>
Retrieves the static class class_name.
Sourcepub fn create_java_array(
&self,
class_name: &str,
inv_args: &[impl Borrow<InvocationArg>],
) -> Result<Instance>
pub fn create_java_array( &self, class_name: &str, inv_args: &[impl Borrow<InvocationArg>], ) -> Result<Instance>
Creates a new Java Array with elements of the class class_name.
The array will have the InvocationArgs populated.
The InvocationArgs must be of type class_name.
Sourcepub fn create_java_list(
&self,
class_name: &str,
inv_args: &[InvocationArg],
) -> Result<Instance>
👎Deprecated since 0.15.0: Please use java_list instead
pub fn create_java_list( &self, class_name: &str, inv_args: &[InvocationArg], ) -> Result<Instance>
java_list insteadCreates a new Java List with elements of the class class_name.
The array will have the InvocationArgs populated.
The InvocationArgs must be of type class_name.
Sourcepub fn java_list<'a>(
&self,
inner_class_name: impl Into<&'a str>,
inv_args: Vec<impl TryInto<InvocationArg, Error = J4RsError>>,
) -> Result<Instance>
pub fn java_list<'a>( &self, inner_class_name: impl Into<&'a str>, inv_args: Vec<impl TryInto<InvocationArg, Error = J4RsError>>, ) -> Result<Instance>
Creates a new Java List with elements of the class inner_class_name.
Sourcepub fn java_map<'a>(
&self,
key_class_name: impl Into<&'a str>,
value_class_name: impl Into<&'a str>,
inv_args: HashMap<impl TryInto<InvocationArg, Error = J4RsError>, impl TryInto<InvocationArg, Error = J4RsError>>,
) -> Result<Instance>
pub fn java_map<'a>( &self, key_class_name: impl Into<&'a str>, value_class_name: impl Into<&'a str>, inv_args: HashMap<impl TryInto<InvocationArg, Error = J4RsError>, impl TryInto<InvocationArg, Error = J4RsError>>, ) -> Result<Instance>
Creates a new Java Map with keys of class key_class_name and values of class value_class_name.
Sourcepub fn invoke(
&self,
instance: &Instance,
method_name: &str,
inv_args: &[impl Borrow<InvocationArg>],
) -> Result<Instance>
pub fn invoke( &self, instance: &Instance, method_name: &str, inv_args: &[impl Borrow<InvocationArg>], ) -> Result<Instance>
Invokes the method method_name of a created Instance, passing an array of InvocationArgs. It returns an Instance as the result of the invocation.
Sourcepub fn field(&self, instance: &Instance, field_name: &str) -> Result<Instance>
pub fn field(&self, instance: &Instance, field_name: &str) -> Result<Instance>
Retrieves the field field_name of a created Instance.
Sourcepub fn static_class_field(
&self,
class_name: &str,
field_name: &str,
) -> Result<Instance>
pub fn static_class_field( &self, class_name: &str, field_name: &str, ) -> Result<Instance>
Retrieves the field field_name of a static class.
Sourcepub fn invoke_to_channel(
&self,
instance: &Instance,
method_name: &str,
inv_args: &[impl Borrow<InvocationArg>],
) -> Result<InstanceReceiver>
pub fn invoke_to_channel( &self, instance: &Instance, method_name: &str, inv_args: &[impl Borrow<InvocationArg>], ) -> Result<InstanceReceiver>
Invokes the method method_name of a created Instance, passing an array of InvocationArgs.
It returns a Result of InstanceReceiver that may be used to get an underlying Receiver<Instance>. The result of the invocation will come via this Receiver.
Sourcepub fn init_callback_channel(
&self,
instance: &Instance,
) -> Result<InstanceReceiver>
pub fn init_callback_channel( &self, instance: &Instance, ) -> Result<InstanceReceiver>
Initializes a callback channel via a Java Instance that is a NativeCallbackToRustChannelSupport.
It returns a Result of InstanceReceiver that may be used to get an underlying Receiver<Instance>.
The NativeCallbackToRustChannelSupport Instance which is passed as argument, will be sending Instances via this Receiver.
Sourcepub fn invoke_static(
&self,
class_name: &str,
method_name: &str,
inv_args: &[impl Borrow<InvocationArg>],
) -> Result<Instance>
pub fn invoke_static( &self, class_name: &str, method_name: &str, inv_args: &[impl Borrow<InvocationArg>], ) -> Result<Instance>
Invokes the static method method_name of the class class_name, passing an array of InvocationArgs. It returns an Instance as the result of the invocation.
Sourcepub fn clone_instance(&self, instance: &Instance) -> Result<Instance>
pub fn clone_instance(&self, instance: &Instance) -> Result<Instance>
Creates a clone of the provided Instance
Sourcepub fn cast(&self, from_instance: &Instance, to_class: &str) -> Result<Instance>
pub fn cast(&self, from_instance: &Instance, to_class: &str) -> Result<Instance>
Invokes the static method method_name of the class class_name, passing an array of InvocationArgs. It returns an Instance as the result of the invocation.
Sourcepub fn check_equals(
&self,
instance: impl Borrow<Instance>,
inv_arg: impl Borrow<InvocationArg>,
) -> Result<bool>
pub fn check_equals( &self, instance: impl Borrow<Instance>, inv_arg: impl Borrow<InvocationArg>, ) -> Result<bool>
Checks whether an Instance a is equal to some InvocationArg.
The check is actually against the Java Object.equals, taking into consideration the possibility of null.
NullPointerException will not be thrown, even if one of the inputs is null.
Sourcepub fn instance_into_raw_object(&self, instance: Instance) -> Result<jobject>
pub fn instance_into_raw_object(&self, instance: Instance) -> Result<jobject>
Consumes an Instance and returns its jobject. The returned jobject is a JNI local reference.
Sourcepub fn to_rust_boxed<T>(&self, instance: Instance) -> Result<Box<T>>where
T: DeserializeOwned + Any,
pub fn to_rust_boxed<T>(&self, instance: Instance) -> Result<Box<T>>where
T: DeserializeOwned + Any,
Returns the Rust representation of the provided instance, boxed
Sourcepub fn to_rust<T>(&self, instance: Instance) -> Result<T>where
T: DeserializeOwned + Any,
pub fn to_rust<T>(&self, instance: Instance) -> Result<T>where
T: DeserializeOwned + Any,
Returns the Rust representation of the provided instance
pub fn to_rust_deserialized<T>(&self, instance: Instance) -> Result<T>where
T: DeserializeOwned + Any,
Sourcepub fn deploy_artifact<T: Any + JavaArtifact>(&self, artifact: &T) -> Result<()>
pub fn deploy_artifact<T: Any + JavaArtifact>(&self, artifact: &T) -> Result<()>
Deploys an artifact in the default j4rs jars location.
This is useful for build scripts that need jars for the runtime that can be downloaded from e.g. Maven.
The function deploys only the specified artifact, not its transitive dependencies.
Sourcepub fn copy_j4rs_libs_under(path: &str) -> Result<()>
pub fn copy_j4rs_libs_under(path: &str) -> Result<()>
Copies the jassets default directory and the j4rs dynamic library under the specified
location.
This is useful for cases when with_base_path method is used when building a Jvm with
the JvmBuilder.
Build scripts should use this method.
Sourcepub fn chain(&self, instance: &Instance) -> Result<ChainableInstance<'_>>
pub fn chain(&self, instance: &Instance) -> Result<ChainableInstance<'_>>
Initiates a chain of operations on Instances.
Sourcepub fn into_chain(&self, instance: Instance) -> ChainableInstance<'_>
pub fn into_chain(&self, instance: Instance) -> ChainableInstance<'_>
Initiates a chain of operations on Instances.
Sourcepub fn throw_invocation_exception(&self, message: &str) -> Result<()>
pub fn throw_invocation_exception(&self, message: &str) -> Result<()>
Throws an exception in the Java World
Sourcepub fn select(
instance_receivers: &[&InstanceReceiver],
) -> Result<(usize, Instance)>
pub fn select( instance_receivers: &[&InstanceReceiver], ) -> Result<(usize, Instance)>
Returns the first Instance that is available from the passed InstanceReceivers,
along with the index of the receiver that was selected and actually returned the instance.
This is a mostly naive implementation of select, because of absence for selecting among mpsc channels.
Sourcepub fn select_timeout(
instance_receivers: &[&InstanceReceiver],
timeout: &Duration,
) -> Result<(usize, Instance)>
pub fn select_timeout( instance_receivers: &[&InstanceReceiver], timeout: &Duration, ) -> Result<(usize, Instance)>
Returns the first Instance that is available from the passed InstanceReceivers,
along with the index of the receiver that was selected and actually returned the instance.
If there are no instances returned for the duration defined in timeout argument, an error is returned.
This is a mostly naive implementation of select, because of absence for selecting among mpsc channels.
Source§impl Jvm
impl Jvm
Sourcepub async fn invoke_async(
&self,
instance: &Instance,
method_name: &str,
inv_args: &[InvocationArg],
) -> Result<Instance>
pub async fn invoke_async( &self, instance: &Instance, method_name: &str, inv_args: &[InvocationArg], ) -> Result<Instance>
Invokes the method method_name of a created Instance asynchronously, passing an array of InvocationArgs.
It returns an Instance as the result of the invocation.
Sourcepub async fn invoke_into_sendable_async(
instance: Instance,
method_name: String,
inv_args: Vec<InvocationArg>,
) -> Result<Instance>
pub async fn invoke_into_sendable_async( instance: Instance, method_name: String, inv_args: Vec<InvocationArg>, ) -> Result<Instance>
Invokes the method method_name of a created Instance asynchronously, passing an array of InvocationArgs.
It returns an Instance as the result of the invocation.
Instances are Send and can be safely sent to other threads. However, because of Send Approximation, the Future returned by invoke_async is not Send, even if it just contains an Instance. This is because the Jvm is being captured by the async call as well and the Jvm is not Send.
In order to have a Future<Instance> that is Send, the Jvm::invoke_into_sendable_async can be used. This function does not get a Jvm as argument; it creates one internally when needed and applies some scoping workarounds in order to achieve returning a Future<Instance> which is also Send.
Trait Implementations§
Source§impl JavaFxSupport for Jvm
impl JavaFxSupport for Jvm
Source§fn start_javafx_app(&self) -> Result<InstanceReceiver>
fn start_javafx_app(&self) -> Result<InstanceReceiver>
Triggers the start of a JavaFX application.
When the JavaFX application starts, the InstanceReceiver channel will receive an Instance of javafx.stage.Stage.
The UI may start being built using the provided Stage
Source§fn get_javafx_event_receiver(
&self,
instance: &Instance,
fx_event_type: FxEventType,
) -> Result<InstanceReceiver>
fn get_javafx_event_receiver( &self, instance: &Instance, fx_event_type: FxEventType, ) -> Result<InstanceReceiver>
Creates an instance receiver that will be receiving Instances of events.
The fx_event_type argument is the type of the event that we want to handle and receive Instances for.
For example, to create an InstanceReceiver for a ‘javafx.scene.control.Button’,
you need to call the method by using the button as the instance argument
FxEventType::ActionEvent_Action as the fx_event_type argument
Source§fn on_close_event_receiver(&self, stage: &Instance) -> Result<InstanceReceiver>
fn on_close_event_receiver(&self, stage: &Instance) -> Result<InstanceReceiver>
Creates an instance receiver that will be receiving Instances of events for onclose requests of a Stage.
The instance passed as argument needs to be of class javafx.stage.Stage.
Source§fn deploy_javafx_dependencies(&self) -> Result<()>
fn deploy_javafx_dependencies(&self) -> Result<()>
Deploys the required dependencies to run a JavaFX application in order to be able to be used by j4rs.