Skip to content

Invoking a method that modifies an input parameter #83

Open

Description

@sorokod

I am invoking a Java method that accepts an array as a parameter and returns an int, something like int method(byte[] out). method is modifying the out parameter which I would like to pick Julia side.

On Julia side I have:

jout = Array{UInt8}(undef, 64)
jcall(object, "method", jint, (Array{jbyte,1}), jout)

But what I see is that jout is not affected by the invocation. How do I implemented this invocation pattern with JavaCall?

Activity

  1. dfdx commented on Aug 18, 2018

    @dfdx
    Collaborator

    JavaCall automatically converts Julia arrays to Java arrays and vice versa, but they don't share memory. What you pass to the Java method is actually a copy, that's why the argument isn't modified.

    If you can add custom class to JVM, then the easiest way to overcome it would be to change signature to take ArrayList or return modified array (although this will lead to double conversion - between Julia and Java arrays).

    Another approach would be to wrap Java arrays into a special type (say, JavaArrray) that you can pass and extract explicitly. But it's quite a big piece of work since Java treats all the primitive and object arrays differently (e.g. you construct byte[] using NewByteArray, int[] using NewIntArray, Object[] using NewObjectArray, etc.; access to elements is also specific for each array type).

  2. sorokod commented on Aug 19, 2018

    @sorokod
    Author

    Yes, an adapter can work, but an extra copy is a concern.

  3. dfdx commented on Aug 19, 2018

    @dfdx
    Collaborator

    If you have a lot of code already working with byte[], you can create a no-copy wrapper for it instead of ArrayList. Something like:

    class ArrayWrapper {
        private byte[];
       // constructor and accessors
    }
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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