Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Confusing documentation in is_instance_of section of macro bind_java_type #821

@wuwbobo2021

Description

@wuwbobo2021

Description

This is a note found in bind_java_type documentation (https://github.com/jni-rs/jni-rs/blob/master/crates/jni/docs/macros/bind_java_type_1_properties.md#is_instance_of):

The generated as_*() methods perform a runtime IsInstanceOf check.

However, in generated macros, some note like this is automatically generated:

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

Side note

I am adding a get_parcelable_extra method for jni_min_helper::Intent, and I have to avoid #764; the workaround that I tried is to have private IntentApi32 and IntentApi33 bindings that also maps to android.content.Intent, and have this in the original Intent binding in order to avoid runtime class type checking:

    is_instance_of = {
        api_32 = IntentApi32,
        api_33 = IntentApi33,
    }

Soon I found this is problematic because it exposes as_api_32 and as_api_33 to the public API, it's just because Intent has been made public. Honestly, I don't know if this should be fixed, because the fix of not exposing generated methods with signature that contains private type seems to be complicated.

Why do I need these as_*() methods? Probably because I can't find a safe Env method for doing similar things (casting &JavaType<'_> to &SuperType<'_>) without runtime check. (I understand jni-rs doesn't allow implicit upcast of borrowed references, because methods from the base class and the derived class may have the same name, and that would be confusing.)

So I did this for now (https://docs.rs/jni-min-helper/0.4.1/src/jni_min_helper/receiver.rs.html#141-170):

// Safety: `Intent` and `IntentApi33` both map to `android.content.Intent`.
let intent: Cast<'_, '_, IntentApi33> = unsafe { env.as_cast_unchecked(self) };

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions