Skip to main content

JString

Struct JString 

Source
pub struct JString<'local>(/* private fields */);
Expand description

A java.lang.String reference, tied to a JNI local reference frame.

See the JObject documentation for more information about object references, how to cast them, and local reference frame lifetimes.

Implementations§

Source§

impl<'local> JString<'local>

Source

pub unsafe fn from_raw<'env_inner>( env: &Env<'env_inner>, raw: jstring, ) -> JString<'env_inner>

Creates a JString that wraps the given raw jobject

§Safety
  • raw must be a valid raw JNI local reference (or null).
  • raw must be an instance of the correct Java class.
  • There must not be any other owning Reference wrapper for the same reference.
  • The local reference must belong to the current thread and not outlive the JNI stack frame associated with the Env 'local lifetime.
Source

pub const fn null() -> JString<'static>

Creates a new null reference.

Null references are always valid and do not belong to a local reference frame. Therefore, the returned JString always has the 'static lifetime.

Source

pub fn into_raw(self) -> jstring

Unwrap to the raw jni type.

Source

pub fn cast_local<'any_local>( env: &mut Env<'_>, obj: impl Reference + Into<JObject<'any_local>> + AsRef<JObject<'any_local>>, ) -> Result<JString<'any_local>>

Cast a local reference to a JString

This will do a runtime (IsInstanceOf) check that the object is an instance of the correct class.

Also see these other options for casting local or global references to a JString:

§Errors

Returns Error::WrongObjectType if the IsInstanceOf check fails.

Source§

impl<'local> JString<'local>

Source

pub fn as_char_sequence(&self) -> Cast<'local, '_, JCharSequence<'local>>

Casts this JString to a ::jni::objects::JCharSequence

This does not require a runtime type check since any JString is also a ::jni::objects::JCharSequence

Source§

impl<'local> JString<'local>

Source

pub fn intern<'env_local>( &self, env: &mut Env<'env_local>, ) -> Result<JString<'env_local>>

Returns a canonical, interned version of this string.

Source§

impl JString<'_>

Source

pub fn new<'env_local>( env: &mut Env<'env_local>, from: impl AsRef<str>, ) -> Result<JString<'env_local>>

Encodes a Rust &str to MUTF-8 and creates a JString (java.lang.String object).

This is a convenience that’s equivalent to calling Self::from_str

This API catches exceptions internally and is not expected to return Error::JavaException (unless called while there is a pending exception).

§Performance

The input string is re-encoded to modified UTF-8, so this involves a copy of your input to encode, before calling into JNI to create the JString.

To avoid the overhead of encoding and copying, use Self::from_jni_str and the crate::jni_str! macro to encode strings to MUTF-8 at compile time.

Source

pub fn from_str<'env_local>( env: &mut Env<'env_local>, from: impl AsRef<str>, ) -> Result<JString<'env_local>>

Encodes a Rust &str to MUTF-8 and creates a JString (java.lang.String object).

This API catches exceptions internally and is not expected to return Error::JavaException (unless called while there is a pending exception).

§Performance

The input string is re-encoded to modified UTF-8, so this involves a copy of your input to encode, before calling into JNI to create the JString.

To avoid the overhead of encoding and copying, use Self::from_jni_str and the crate::jni_str! macro to encode strings to MUTF-8 at compile time.

Source

pub fn from_jni_str<'env_local>( env: &mut Env<'env_local>, from: impl AsRef<JNIStr>, ) -> Result<JString<'env_local>>

Creates a JString (java.lang.String object) from a JNIStr (modified UTF-8).

For simple string literals, consider using the crate::jni_str! macro to create / encode JNIStr literals at compile time.

This API catches exceptions internally and is not expected to return Error::JavaException (unless called while there is a pending exception).

Source

pub fn mutf8_chars(&self, env: &Env<'_>) -> Result<MUTF8Chars<'_, &JString<'_>>>

Gets the contents of this string, in modified UTF-8 encoding (via GetStringUTFChars).

The returned MUTF8Chars guard can be used to access the modified UTF-8 bytes, or to convert to a Rust string (UTF-8).

For example:

let my_jstring = JString::from_str(env, "Hello, world!")?;
let mutf8_chars = my_jstring.mutf8_chars(env)?;
let jni_str: &JNIStr = &mutf8_chars;
let rust_str = jni_str.to_str();

When the MUTF8Chars guard is dropped, the reference to the contents gets released.

The MUTF8Chars guard dereferences to a JNIStr.

Also note that MUTF8Chars (and also JString itself) implements Display and ToString so it’s also possible to use .to_string() to get a Rust String from a JString

§Errors

Returns an Error::NullPtr if this JString is null.

Source

pub fn try_to_string(&self, env: &Env<'_>) -> Result<String>

Gets the contents of this string as a Rust String.

For example:

let jstring = JString::from_str(env, "Hello, world!")?;
let rust_string = jstring.try_to_string(&env)?;
assert_eq!(rust_string, "Hello, world!");

This is equivalent to calling Self::mutf8_chars and then converting that to a String, like:

let jstring = JString::from_str(env, "Hello, world!")?;
let mutf8_chars = jstring.mutf8_chars(&env)?;
let rust_string = mutf8_chars.to_string();
§Errors

Returns an Error::NullPtr if this JString is null.

Methods from Deref<Target = JObject<'local>>§

Source

pub fn as_raw(&self) -> jobject

Returns the raw JNI pointer.

Trait Implementations§

Source§

impl<'local> AsRef<JCharSequence<'local>> for JString<'local>

Source§

fn as_ref(&self) -> &JCharSequence<'local>

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

impl<'local> AsRef<JObject<'local>> for JString<'local>

Source§

fn as_ref(&self) -> &JObject<'local>

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

impl<'local> AsRef<JString<'local>> for JString<'local>

Source§

fn as_ref(&self) -> &JString<'local>

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

impl<'local> Debug for JString<'local>

Source§

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

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

impl<'local> Default for JString<'local>

Source§

fn default() -> JString<'local>

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

impl<'local> Deref for JString<'local>

Source§

type Target = JObject<'local>

The resulting type after dereferencing.
Source§

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

Dereferences the value.
Source§

impl<'local> Display for JString<'local>

Display the contents of a JString

This implementation relies on JNI (GetStringUTFChars) to retrieve the string contents for display.

If you try and format a null reference this will output “

In case you attempt to format a JString before JavaVM::singleton has been initialized then this will simply output “” and log an error.

In case of any other unexpected JNI error, this will output “” and log the error details.

Source§

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

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

impl<'local> From<JString<'local>> for JCharSequence<'local>

Source§

fn from(value: JString<'local>) -> JCharSequence<'local>

Converts to this type from the input type.
Source§

impl<'local> From<JString<'local>> for JObject<'local>

Source§

fn from(other: JString<'local>) -> JObject<'local>

Converts to this type from the input type.
Source§

impl<'local> Reference for JString<'local>

Source§

type Kind<'env> = JString<'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 = JString<'static>

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

Auto Trait Implementations§

§

impl<'local> Freeze for JString<'local>

§

impl<'local> RefUnwindSafe for JString<'local>

§

impl<'local> Send for JString<'local>
where 'local: 'static,

§

impl<'local> Sync for JString<'local>
where 'local: 'static,

§

impl<'local> Unpin for JString<'local>

§

impl<'local> UnsafeUnpin for JString<'local>

§

impl<'local> UnwindSafe for JString<'local>

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<'local, T> IntoAuto<'local> for T
where T: Into<JObject<'local>>,

Source§

fn auto(self) -> Auto<'local, Self>

Wraps the local reference type into an auto-delete Auto that will automatically delete the local reference when it is dropped
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.