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>
impl<'local> JString<'local>
Sourcepub unsafe fn from_raw<'env_inner>(
env: &Env<'env_inner>,
raw: jstring,
) -> JString<'env_inner>
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
rawmust be a valid raw JNI local reference (ornull).rawmust 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
'locallifetime.
Sourcepub const fn null() -> JString<'static>
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.
Sourcepub fn cast_local<'any_local>(
env: &mut Env<'_>,
obj: impl Reference + Into<JObject<'any_local>> + AsRef<JObject<'any_local>>,
) -> Result<JString<'any_local>>
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>
impl<'local> JString<'local>
Sourcepub fn as_char_sequence(&self) -> Cast<'local, '_, JCharSequence<'local>>
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 JString<'_>
impl JString<'_>
Sourcepub fn new<'env_local>(
env: &mut Env<'env_local>,
from: impl AsRef<str>,
) -> Result<JString<'env_local>>
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.
Sourcepub fn from_str<'env_local>(
env: &mut Env<'env_local>,
from: impl AsRef<str>,
) -> Result<JString<'env_local>>
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.
Sourcepub fn from_jni_str<'env_local>(
env: &mut Env<'env_local>,
from: impl AsRef<JNIStr>,
) -> Result<JString<'env_local>>
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).
Sourcepub fn mutf8_chars(&self, env: &Env<'_>) -> Result<MUTF8Chars<'_, &JString<'_>>>
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.
Sourcepub fn try_to_string(&self, env: &Env<'_>) -> Result<String>
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.
Trait Implementations§
Source§impl<'local> AsRef<JCharSequence<'local>> for JString<'local>
impl<'local> AsRef<JCharSequence<'local>> for JString<'local>
Source§fn as_ref(&self) -> &JCharSequence<'local>
fn as_ref(&self) -> &JCharSequence<'local>
Source§impl<'local> Display for JString<'local>
Display the contents of a JString
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 “
In case of any other unexpected JNI error, this will output “
Source§impl<'local> From<JString<'local>> for JCharSequence<'local>
impl<'local> From<JString<'local>> for JCharSequence<'local>
Source§fn from(value: JString<'local>) -> JCharSequence<'local>
fn from(value: JString<'local>) -> JCharSequence<'local>
Source§impl<'local> Reference for JString<'local>
impl<'local> Reference for JString<'local>
Source§type Kind<'env> = JString<'env>
type Kind<'env> = JString<'env>
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 moreSource§type GlobalKind = JString<'static>
type GlobalKind = JString<'static>
GlobalKind type should be equivalent to
Kind<'static>, with the additional bound that ensures the type is
Send + Sync Read moreSource§fn as_raw(&self) -> jobject
fn as_raw(&self) -> jobject
crate::sys::jobject reference.Source§fn class_name() -> Cow<'static, JNIStr>
fn class_name() -> Cow<'static, JNIStr>
Source§fn lookup_class<'caller>(
env: &Env<'_>,
loader_context: &LoaderContext<'_, '_>,
) -> Result<impl Deref<Target = Global<JClass<'static>>> + 'caller>
fn lookup_class<'caller>( env: &Env<'_>, loader_context: &LoaderContext<'_, '_>, ) -> Result<impl Deref<Target = Global<JClass<'static>>> + 'caller>
Source§unsafe fn kind_from_raw<'env>(local_ref: jobject) -> Self::Kind<'env>
unsafe fn kind_from_raw<'env>(local_ref: jobject) -> Self::Kind<'env>
Self::Kind for the given reference that is tied
to the specified lifetime. Read moreSource§unsafe fn global_kind_from_raw(global_ref: jobject) -> Self::GlobalKind
unsafe fn global_kind_from_raw(global_ref: jobject) -> Self::GlobalKind
Source§fn null<'any>() -> Self::Kind<'any>
fn null<'any>() -> Self::Kind<'any>
null reference based on Self::Kind