Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return Nullable instead of Optional<T> #395

Open
Kofituo opened this issue Sep 20, 2021 · 6 comments
Open

Return Nullable instead of Optional<T> #395

Kofituo opened this issue Sep 20, 2021 · 6 comments
Labels
bug Java/JNI Case specific only for Java/JNI interface generation

Comments

@Kofituo
Copy link
Contributor

Kofituo commented Sep 20, 2021

Is there way to return a nullable value instead of a value wrapped in Optional<T>?
The difficulty is Optional<T> works with Andriod Api Version 24+ (but my projects starts from Api 21) and since the main language for andriod programming is Kotlin, it would be better to use Kotlin's way of handling nullable values i.e. using ? operator among others

A possible solution would be to rewrite the Optional class in Java (since Kotlin's companion objects are different from Java's static keyword), preserving the parts I'd use, but I feel using the Kotlin syntax integrates more with my project

@Dushistov
Copy link
Owner

There are drop-in-replacement for standard optional class, it can be activated like this:

 JavaConfig::new(output_dir, "com.company".into())
.use_optional_package("com.hadisatrio.optional".into())

and in app/build.gradle I have

dependencies {
  ...
    implementation 'com.github.dushistov:Optional:9016463'

In theory you can patch jni-include.rs like this:

foreign_typemap!(
    ($p:r_type) <T: SwigForeignClass> Option<T> => jlong {
        $out = match $p {
            Some(x) => {
                let ptr = <swig_subst_type!(T)>::box_object(x);
                debug_assert_ne!(0, ptr);
                ptr
            }
            None => 0,
        };
    };
    ($p:f_type) => "@Nullable swig_f_type!(T, NoNullAnnotations)" r#"
        $out;
        if ($p != 0) {
            $out = new swig_f_type!(T, NoNullAnnotations)(InternalPointerMarker.RAW_PTR, $p);
        } else {
            $out = null;
        }
"#;
);

@Dushistov Dushistov added bug Java/JNI Case specific only for Java/JNI interface generation labels Sep 20, 2021
@Kofituo
Copy link
Contributor Author

Kofituo commented Sep 21, 2021

Okay thanks I'll try it out

@Kofituo Kofituo closed this as completed Sep 21, 2021
@Kofituo Kofituo reopened this Sep 21, 2021
@Kofituo
Copy link
Contributor Author

Kofituo commented Sep 21, 2021

Can I just put the path in the java_glue.rs.in file?or it has to be the jni_include.rs file?

@Kofituo Kofituo closed this as completed Oct 6, 2021
@Dushistov
Copy link
Owner

In theory you should be able to rewrite any standard rule that defined in jni-include.rs in your file.rs.in . But looks like rewriting generic rule contains some bug,
so I leave this issue open until I fixed the rewriting issue.

@Dushistov Dushistov reopened this Oct 6, 2021
@Kofituo
Copy link
Contributor Author

Kofituo commented Oct 6, 2021

Okay sure

@Jonathas-Conceicao
Copy link

I've recently ran into the same problem, the drop-in-replacement solved the issue but I had to use

dependencies {
    implementation 'com.github.dushistov:Optional:develop-SNAPSHOT'
    ...
}

Due to the empty method that is only available on the develop branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Java/JNI Case specific only for Java/JNI interface generation
Projects
None yet
Development

No branches or pull requests

3 participants