All notable changes to this project will be documented in this file.
The format is based on Keep a Changelog and this project adheres to Semantic Versioning.
jni-sys
dependency dumped to0.4
(#478JNIEnv::get_version
has been renamed toJNIEnv::version
(#478JNIEnv::from_raw
now explicitly checks that the JNI version is>= 1.4
since the crate needs to be able to assume>= 1.2
so it can check for exceptions and assume>= 1.4
to avoid runtime checks for direct byte buffers (#478- The following functions are now infallible (#478:
JNIEnv::version
JNIEnv::exception_check
JNIEnv::exception_clear
JNIEnv::exception_describe
JNIEnv::exception_occurred
(#517JNIEnv::is_same_object
JNIEnv::delete_local_ref
WeakRef::is_same_object
WeakRef::is_weak_ref_to_same_object
WeakRef::is_garbage_collected
JNIEnv::fatal_error
is now guaranteed not to panic or allocate, but requires the error message to be encoded ahead of time. (#480)JNIEnv::get_native_interface
has been removed since it's redundant andJNIEnv::get_raw
is more consistent with other APIs.JavaVM::get_java_vm_pointer
has been renamedJavaVM::get_raw
for consistency.
- New functions for converting Rust
char
to and from Javachar
andint
(#427 / #434) JNIEnv::call_nonvirtual_method
andJNIEnv::call_nonvirtual_method_unchecked
to call non-virtual method. (#454)JavaStr
,JNIStr
, andJNIString
have several new methods and traits, most notably ato_str
method that converts to a regular Rust string. (#510 / #512)
JValueGen
has been removed.JValue
andJValueOwned
are now separate, unrelated, non-generic types. (#429)- Make most
from_raw()
,get_raw()
andinto_raw()
methodsconst fn
. (#453) get_object_class
borrows theJNIEnv
mutably because it creates a new local reference. (#456)get/set_field_unchecked
have been marked as unsafe since they can lead to undefined behaviour if the given types don't match the field type (#457)get/set/take_rust_field
no longer require a mutableJNIEnv
reference since they don't return any new local references to the caller (#455)JavaStr::from_env
has been removed because it was unsound (it could cause undefined behavior and was not markedunsafe
). UseJNIEnv::get_string
orJNIEnv::get_string_unchecked
instead. (#510 / #512)JavaStr::get_raw
has been renamed toas_ptr
. (#510 / #512)JavaStr
,JNIStr
, andJNIString
no longer coerce toCStr
, because usingCStr::to_str
will often have incorrect results. You can still get aCStr
, but must use the newas_cstr
method to do so. (#510 / #512)
0.21.1 — 2023-03-08
- Compilation is fixed for architectures with a C ABI that has unsigned
char
types. (#419)
0.21.0 — 2023-02-13
This release makes extensive breaking changes in order to improve safety. Most projects that use this library will need to be changed. Please see the migration guide.
JavaStr::into_raw()
which drops theJavaStr
and releases ownership of the raw string pointer (#374)JavaStr::from_raw()
which takes ownership of a raw string pointer to create aJavaStr
(#374)JNIEnv::get_string_unchecked
is a cheaper,unsafe
alternative toget_string
that doesn't check the given object is ajava.lang.String
instance. (#328)WeakRef
andJNIEnv#new_weak_ref
. (#304)define_class_bytearray
method that takes anAutoElements<jbyte>
rather than a&[u8]
(#244)JObject
now has anas_raw
method that borrows theJObject
instead of taking ownership likeinto_raw
. Needed becauseJObject
no longer has theCopy
trait. (#392)JavaVM::destroy()
(unsafe) as a way to try and unload aJavaVM
on supported platforms (#391)JavaVM::detach_current_thread()
(unsafe) as a way to explicitly detach a thread (normally this is automatic on thread exit). Needed to detach daemon threads manually if usingJavaVM::destroy()
(#391)JPrimitiveArray<T: TypeArray>
and type-specific aliases likeJByteArray
,JIntArray
etc now provide safe, reference wrappers for thesys
typesjarray
andjbyteArray
etc with a lifetime likeJObject
(#400)JObjectArray
provides a reference wrapper for ajobjectArray
with a lifetime likeJObject
. (#400)AutoElements
andAutoElementsCritical
(previouslyAutoArray
/AutoPrimitiveArray
) implementDeref<Target=[T]>
andDerefMut
so array elements can be accessed via slices without needing additionalunsafe
code. (#400)AsJArrayRaw
trait which enablesJNIEnv::get_array_length()
to work withJPrimitiveArray
orJObjectArray
types (#400)InitArgsBuilder
now hastry_option
andoption_encoded
methods. (#414)
JNIEnv::get_string
checks that the given object is ajava.lang.String
instance to avoid undefined behaviour from the JNI implementation potentially aborting the program. (#328)JNIEnv::call_*method_unchecked
was markedunsafe
, as passing improper argument types, or a bad number of arguments, can cause a JVM crash. (#385)- The
JNIEnv::new_object_unchecked
function now takes arguments as&[jni::sys::jvalue]
to avoid allocating, putting it inline with changes toJniEnv::call_*_unchecked
from 0.20.0 (#382) - The
get_superclass
function now returns an Option instead of a null pointer if the class has no superclass (#151) - The
invocation
feature now locates the JVM implementation dynamically at runtime (via thejava-locator
crate by default) instead of linking with the JVM at build time (#293) - Most
JNIEnv
methods now require&mut self
. This improves safety by preventingJObject
s from getting an invalid lifetime. Most native method implementations (that is,#[no_mangle] extern "system" fn
s) must now make theJNIEnv
parametermut
. See the example on the crate documentation. (#392) JByteBuffer
,JClass
,JNIEnv
,JObject
,JString
, andJThrowable
no longer have theClone
orCopy
traits. This improves safety by preventing object references from being used after the JVM deletes them. Most functions that take one of these types as a parameter (exceptextern fn
s that are directly called by the JVM) should now borrow it instead, e.g.&JObject
instead ofJObject
. (#392)AutoLocal
is now generic in the type of object reference (JString
, etc). (#392)- The closure passed to
JNIEnv::with_local_frame
must now take a&mut JNIEnv
parameter, which has a different lifetime. This improves safety by preventing local references from escaping the closure, which would cause a use-after-free bug.Executor::with_attached
andExecutor::with_attached_capacity
have been similarly changed. (#392) - The closure passed to
JNIEnv::with_local_frame
can now return a genericResult<T, E>
so long as the error implementsFrom<jni::errors::Error>
(#399) JNIEnv::with_local_frame
now returns the same type that the given closure returns (#399)JNIEnv::with_local_frame
no longer supports returning a local reference directly to the calling scope (seewith_local_frame_returning_local
) (#399)Executor::with_attached
andExecutor::with_attached_capacity
have been changed in the same way asJNIEnv::with_local_frame
(they are thin wrappers) (#399)Desc
,JNIEnv::pop_local_frame
, andTypeArray
are nowunsafe
. (#392)- The
Desc
trait now has an associated typeOutput
. Many implementations now returnAutoLocal
, so if you callDesc::lookup
yourself and then callas_raw
on the returned object, make sure theAutoLocal
isn't dropped too soon (see theDesc::lookup
documentation for examples). (#392) - The
Desc<JClass>
trait is no longer implemented forJObject
or&JObject
. The previous implementation that called.get_object_class()
was surprising and a simpler cast would make it easy to mistakenly pass instances where a class is required. (#118) - Named lifetimes in the documentation have more descriptive names (like
'local
instead of'a
). The new naming convention is explained in theJNIEnv
documentation. (#392) - Object reference types (
JObject
,JClass
,AutoLocal
,GlobalRef
, etc) now implementAsRef<JObject>
andDeref<Target = JObject>
. Typed wrappers likeJClass
also implementInto<JObject>
, butGlobalRef
does not. (#392) - Most
JList
andJMap
methods now require a&mut JNIEnv
parameter.JListIter
andJMapIter
no longer implementIterator
, and instead have anext
method that requires a&mut JNIEnv
parameter (usewhile let
loops instead offor
). (#392) JValue
has been changed in several ways: (#392)- It is now a generic type named
JValueGen
.JValue
is now a type alias forJValueGen<&JObject>
, that is, it borrows an object reference.JValueOwned
is a type alias forJValueGen<JObject>
, that is, it owns an object reference. JValueOwned
does not have theCopy
trait.- The
to_jni
method is now namedas_jni
, and it borrows theJValueGen
instead of taking ownership. JObject
can no longer be converted directly toJValue
, which was commonly done when calling Java methods or constructors. Instead ofobj.into()
, use(&obj).into()
.
- It is now a generic type named
- All
JNIEnv
array APIs now work in terms ofJPrimitiveArray
andJObjectArray
(reference wrappers with a lifetime) instead ofsys
types likejarray
andjbyteArray
(#400) AutoArray
andAutoPrimitiveArray
have been renamedAutoElements
andAutoElementsCritical
to show their connection and differentiate from newJPrimitiveArray
API (#400)get_primitive_array_critical
is nowunsafe
and has been renamed toget_array_elements_critical
(consistent with the rename ofAutoPrimitiveArray
) with more detailed safety documentation (#400)get_array_elements
is now alsounsafe
(for many of the same reasons asget_array_elements_critical
) and has detailed safety documentation (#400)AutoArray/AutoArrayCritical::size()
has been replaced with.len()
which can't fail and returns ausize
(#400)- The
TypeArray
trait is now a private / sealed trait, that is considered to be an implementation detail for theAutoArray
API. JvmError
has several more variants and is nownon_exhaustive
. (#414)InitArgsBuilder::option
raises an error on Windows if the string is too long. The limit is currently 1048576 bytes. (#414)
- Trying to use an object reference after it has been deleted now causes a compile error instead of undefined behavior. As a result, it is now safe to use
AutoLocal
,JNIEnv::delete_local_ref
, andJNIEnv::with_local_frame
. (Most of the limitations added in #392, listed above, were needed to make this work.) (#381, #392) - Class lookups via the
Desc
trait now returnAutoLocal
s, which prevents them from leaking. (#109, #392) InitArgsBuilder::option
properly encodes non-ASCII characters on Windows. (#414)
get_string_utf_chars
andrelease_string_utf_chars
fromJNIEnv
(SeeJavaStr::into_raw()
andJavaStr::from_raw()
instead) (#372)- All
JNIEnv::get_<type>_array_elements()
methods have been removed as redundant since they would all be equivalent toget_array_elements()
with the introduction ofJPrimitiveArray
(#400)
0.20.0 — 2022-10-17
Default
trait implemented forJObject
,JString
,JClass
, andJByteBuffer
(#199)Debug
trait implemented forJavaVM
,GlobalRef
,GlobalRefGuard
,JStaticMethodID
andReleaseMode
(#345)ReturnType
for specifying object return types without a String allocation. (#329)
- The
release_string_utf_chars
function has been marked as unsafe. (#334) - Mark
JNIEnv::new_direct_byte_buffer
asunsafe
(#320) JNIEnv::new_direct_byte_buffer
now takes a raw pointer and size instead of a slice (#351 and #364)JNIEnv::direct_buffer_address
returns a raw pointer instead of a slice (#364)- The lifetime of
AutoArray
is no longer tied to the lifetime of a particularJNIEnv
reference. (#302) - Relaxed lifetime restrictions on
JNIEnv::new_local_ref
. Now it can be used to create a local reference from a global reference. (#301 / #319) JMethodID
andJStaticMethodID
implementSend
+Sync
and no longer has a lifetime parameter, making method IDs cacheable (with a documented 'Safety' note about ensuring they remain valid). (#346)JFieldID
andJStaticFieldID
implementSend
+Sync
and no longer has a lifetime parameter, making field IDs cacheable (with a documented 'Safety' note about ensuring they remain valid). (#346)- The
call_*_method_unchecked
functions now takejni:sys::jvalue
arguments to avoid allocating aVec
on each call to map + collectJValue
s assys:jvalue
s (#329) - The
From
trait implementations convertingjni_sys
types likejobject
toJObject
have been replaced withunsafe
::from_raw
functions and corresponding::into_raw
methods. Existing::into_inner
APIs were renamed::into_raw
for symmetry. (#197) - The APIs
JNIEnv::set_rust_field
,JNIEnv::get_rust_field
andJNIEnv::take_rust_field
have been marked asunsafe
(#219)
0.19.0 — 2021-01-24
AutoArray
and genericget_array_elements()
, along withget_<type>_array_elements
helpers. (#287)size()
method toAutoArray
andAutoPrimitiveArray
. (#278 / #287)discard()
method toAutoArray
andAutoPrimitiveArray
. (#275 / #287)
- Removed AutoPrimitiveArray::commit(). (#290)
AutoByte/PrimitiveArray.commit()
now returnsResult
. (#275)- Removed methods get/release/commit_byte/primitive_array_{elements|critical}. (#281)
- Renamed methods get_auto_byte/long/primitive_array_{elements|critical} to get_byte/long/primitive_array_{elements|critical}. (#281)
0.18.0 — 2020-09-23
JNIEnv#define_unnamed_class
function that allows loading a class without specifying its name. The name is inferred from the class data. (#246)SetStatic<type>Field
. (#248)TryFrom<JValue>
for types inside JValue variants (#264).- Implemented Copy for JNIEnv (#255).
repr(transparent)
attribute to JavaVM struct (#259)
- Switch from
error-chain
tothiserror
, making all errorsSend
. Also, support all JNI errors in thejni_error_code_to_result
function and add more information to theInvalidArgList
error. (#242)
0.17.0 — 2020-06-30
- Get/ReleaseByteArrayElements, and Get/ReleasePrimitiveArrayCritical. (#237)
0.16.0 — 2020-02-28
- Java VM instantiation with some MacOS configurations. (#220, #229, #230).
0.15.0 — 2020-02-28
- Ability to pass object wrappers that are convertible to
JObject
as arguments to the majority of JNIEnv methods without explicit conversion. (#213) JNIEnv#is_same_object
implementation. (#213)JNIEnv#register_native_methods
. (#214)- Conversion from
Into<JObject>
toJValue::Object
.
- Passing
null
as class loader todefine_class
method now allowed according to the JNI specification. (#225)
0.14.0 — 2019-10-31
- Relaxed some lifetime restrictions in JNIEnv to support the case when method, field ids; and global references to classes have a different (larger) lifetime than JNIEnv. (#209)
0.13.1 — 2019-08-22
- Various documentation improvements.
0.13.0 — 2019-07-05
0.13 brings major improvements in thread management, allowing to attach the native threads
permanently and safely; Executor
for extra convenience and safety; and other
improvements and fixes.
JavaVM::attach_current_thread_permanently
method, which attaches the current thread and detaches it when the thread finishes. Daemon threads attached withJavaVM::attach_current_thread_as_daemon
also automatically detach themselves when finished. The number of currently attached threads may be acquired usingJavaVM::threads_attached
method. (#179, #180)Executor
— a simple thread attachment manager which helps to safely execute a closure in attached thread context and to automatically free created local references at closure exit. (#186)
- The default JNI API version in
InitArgsBuilder
from V1 to V8. (#178) - Extended the lifetimes of
AutoLocal
to make it more flexible. (#190) - Default exception type from checked
java.lang.Exception
to uncheckedjava.lang.RuntimeException
. It is used implicitly whenJNIEnv#throw
is invoked with exception message:env.throw("Exception message")
; however, for efficiency reasons, it is recommended to specify the exception type explicitly and usethrow_new
:env.throw_new(exception_type, "Exception message")
. (#194)
- Native threads attached with
JavaVM::attach_current_thread_as_daemon
now automatically detach themselves on exit, preventing Java Thread leaks. (#179) - Local reference leaks in
JList
,JMap
andJMapIter
. (#190, #191)
From<jboolean>
implementation forJValue
(#173)Debug
trait for InitArgsBuilder. (#175)InitArgsBuilder#options
returning the collected JVM options. (#177)
- Updated documentation of GetXArrayRegion methods (#169)
- Improved ABI compatibility on various platforms (#170)
This release does not bring code changes.
- Updated project documentation.
JString
,JMap
andJavaStr
and their respective iterators now require an extra lifetime so that they can now work with&'b JNIEnv<'a>
, where'a: 'b
.
This release brings various improvements and fixes, outlined below. The most notable changes are:
null
is no longer represented as anErr
with error kindNullPtr
if it is a value of some nullable Java reference (not an indication of an error). Related issues: #136, #148, #163.unsafe
methods, providing a low-level API similar to JNI, has been marked safe and renamed to have_unchecked
suffix. Such methods can be used to implement caching of class references and method IDs to improve performance in loops and frequently called Java callbacks. If you have such, check out the docs and one of early usages of this feature.
- Invocation API support on Windows and AppVeyor CI (#149)
-
push_local_frame
,delete_global_ref
andrelease_string_utf_chars
no longer check for exceptions as they are safe to call if there is a pending exception (#124):push_local_frame
will now work in case of pending exceptions — as the spec requires; and fail in case of allocation errorsdelete_global_ref
andrelease_string_utf_chars
won't print incorrect log messages
-
Rename some macros to better express their intent (see #123):
- Rename
jni_call
tojni_non_null_call
as it checks the return value to be non-null. - Rename
jni_non_null_call
(which may return nulls) tojni_non_void_call
.
- Rename
-
A lot of public methods of
JNIEnv
have been marked as safe, all unsafe code has been isolated inside internal macros. Methods with_unsafe
suffixes have been renamed and now have_unchecked
suffixes (#140) -
from_str
method of theJavaType
has been replaced by theFromStr
implementation -
Implemented Sync for GlobalRef (#102).
-
Improvements in macro usage for JNI methods calls (#136):
call_static_method_unchecked
andget_static_field_unchecked
methods are allowed to return NULL object- Added checking for pending exception to the
call_static_method_unchecked
method (eliminated WARNING messages in log)
-
Further improvements in macro usage for JNI method calls (#150):
- The new_global_ref() and new_local_ref() functions are allowed to work with NULL objects according to specification.
- Fixed the family of functions new_direct_byte_buffer(), get_direct_buffer_address() and get_direct_buffer_capacity() by adding checking for null and error codes.
- Increased tests coverage for JNIEnv functions.
-
Implemented Clone for JNIEnv (#147).
-
The get_superclass(), get_field_unchecked() and get_object_array_element() are allowed to return NULL object according to the specification (#163).
- The issue with early detaching of a thread by nested AttachGuard. (#139)
JavaVM#get_java_vm_pointer
to retrieve a JavaVM pointer (#98)- This changelog and other project documents (#106)
- The project is moved to an organization (#104)
- Updated versions of dependencies (#105)
- Improved project documents (#107)
- Crate type of a shared library with native methods
must be
cdylib
(#100)
- No changes has been made to the Changelog until this release.