Skip to main content

Global

Struct Global 

Source
pub struct Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static,
{ /* private fields */ }
Expand description

A global reference to a Java object.

Global references are slower to create and delete than ordinary local references, but have several properties that distinguish them:

  • Global references are not bound to the lifetime of a Env.

  • Global references are not bound to any particular thread; they have the Send and Sync traits.

  • Until a global reference is dropped, it will prevent the referenced Java object from being garbage collected.

  • It takes more time to create or delete a global reference than to create or delete a local reference.

These properties make global references useful in a few specific situations:

  • When you need to keep a reference to the same Java object across multiple invocations of a native method, especially if you need a guarantee that it’s the exact same object every time, one way to do it is by storing a global reference to it in a Rust static variable.

  • When you need to send a Java object reference to a different thread, or use a Java object reference from several different threads at the same time, a global reference can be used to do so.

  • When you need a Java object to not be garbage collected too soon, because some side effect will happen (via java.lang.Object::finalize, java.lang.ref.Cleaner, or the like) when it is garbage collected, a global reference can be used to prevent it from being garbage collected. (This hold is released when the global reference is dropped.)

See also Weak, a global reference that does not prevent the underlying Java object from being garbage collected.

§Creating and Deleting

To create a global reference, use the Env::new_global_ref method or wrap an existing global reference (that you own) with Env::global_from_raw. To delete it, simply drop the Global.

If you have a raw jobject global reference that you do not own, you can use Env::as_cast_raw to create a temporary wrapper that will not delete the reference when dropped.

Note that, because global references take more time to create or delete than local references do, they should only be used when their benefits outweigh this drawback. Also note that this performance penalty does not apply to using a global reference (such as calling methods on the underlying Java object), only to creation and deletion of the reference.

§Warning: Drop On an Attached Thread If Possible

When a Global is dropped, a JNI call is made to delete the global reference. If this frequently happens on a thread that is not already attached to the JVM, the thread will be temporarily attached using JavaVM::attach_current_thread_for_scope, causing a severe performance penalty.

To avoid this performance penalty, ensure that Globals are only dropped on a thread that is already attached (or never dropped at all).

In the event that a global reference is dropped on an unattached thread, a message is logged at log::Level::Warn.

Implementations§

Source§

impl<T> Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static,

Source

pub fn null() -> Self

Creates a Global wrapper for a null reference

This is equivalent Global::default()

Source

pub fn into_raw(self) -> jobject

Unwrap to the auto-delete global reference into a raw global jobject.

This prevents the global reference from being automatically deleted when this guard is dropped.

§Leaking References

When unwrapping a Global you should consider how else you will ensure that the reference will get deleted.

The global reference may end up leaking unless a new Global wrapper is create later, or you find some way to call the JNI DeleteGlobalRef API on the raw reference.

Note: You should not find yourself unwrapping a raw global jobject that you do not logically own. If you have a raw global jobject that you do not own, you should instead use Env::as_cast_raw to create a temporary wrapper.

Source

pub fn as_obj(&self) -> &JObject<'static>

Borrows a JObject referring to the same Java object as this Global.

This method is zero-cost and does not create a new local reference.

Global also implements AsRef<JObject>. That trait’s as_ref method does the same thing as this method.

Trait Implementations§

Source§

impl<T, U> AsRef<U> for Global<T>
where T: AsRef<U> + Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync,

Source§

fn as_ref(&self) -> &U

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<T> Debug for Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static + Debug,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T> Default for Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T> Deref for Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'local, 'obj_ref, T> Desc<'local, T> for &'obj_ref Global<T>
where T: Reference + AsRef<T> + AsRef<JObject<'static>> + Into<JObject<'static>> + Default + Send + Sync + 'static,

Source§

type Output = &'obj_ref T

The type that this Desc returns.
Source§

fn lookup(self, _: &mut Env<'local>) -> Result<Self::Output>

Look up the concrete type from the JVM. Read more
Source§

impl<T> Drop for Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<T> Reference for Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync,

Source§

type Kind<'env> = <T as Reference>::Kind<'env>

The generic associated Self::Kind type corresponds to the underlying class type (such as JObject or JString), parameterized by the lifetime that indicates whether the type holds a global reference ('static) or a local reference that’s tied to a JNI stack frame. Read more
Source§

type GlobalKind = <T as Reference>::GlobalKind

The associated GlobalKind type should be equivalent to Kind<'static>, with the additional bound that ensures the type is Send + Sync Read more
Source§

fn as_raw(&self) -> jobject

Returns the underlying, raw crate::sys::jobject reference.
Source§

fn class_name() -> Cow<'static, JNIStr>

The fully qualified class name of the Java class represented by this reference. Read more
Source§

fn lookup_class<'caller>( env: &Env<'_>, loader_context: &LoaderContext<'_, '_>, ) -> Result<impl Deref<Target = Global<JClass<'static>>> + 'caller>

Looks up a global reference to the JClass associated with this reference. Read more
Source§

unsafe fn kind_from_raw<'env>(local_ref: jobject) -> Self::Kind<'env>

Returns a new reference type based on Self::Kind for the given reference that is tied to the specified lifetime. Read more
Source§

unsafe fn global_kind_from_raw(global_ref: jobject) -> Self::GlobalKind

Returns a ('static) reference type based on Self::GlobalKind for the given global_ref. Read more
Source§

fn is_null(&self) -> bool

Returns true if this is a null object reference
Source§

fn null<'any>() -> Self::Kind<'any>

Returns null reference based on Self::Kind
Source§

impl<T> Send for Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static,

Source§

impl<T> Sync for Global<T>
where T: Into<JObject<'static>> + AsRef<JObject<'static>> + Default + Reference + Send + Sync + 'static,

Auto Trait Implementations§

§

impl<T> Freeze for Global<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Global<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for Global<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Global<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Global<T>
where T: UnwindSafe,

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<'local, T> Desc<'local, T> for T
where T: AsRef<T>,

Source§

type Output = T

The type that this Desc returns.
Source§

fn lookup(self, _: &mut Env<'local>) -> Result<T, Error>

Look up the concrete type from the JVM. 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.