JNI return jobjectArray

P

Philipp Kraus

Hello,

I hope this question is not OT.

I create a JNI call for this Java method

class myclass {

void native mymethod( Double[] x )
}

so the parameter x should be a call-by-reference, I would set x in the
native JNI function:

void mymethod( JNIEnv *jenv, jclass jcls, jobjectArray& jarg )
{
// do something
jobjectArray t = .....

jarg = t;
}

How can I set the jobjectArray& back, so the data is return in the parameter x?
IMHO I need jenv->NewGlobalRef, os this the correct use jarg =
(jobjectArray)jenv->NewGlobalRef( (jobject*) &t ) ?

Thanks

Phil
 
M

markspace

Hello,

I hope this question is not OT.

I create a JNI call for this Java method

class myclass {

void native mymethod( Double[] x )
}

so the parameter x should be a call-by-reference, I would set x in the
native JNI function:

void mymethod( JNIEnv *jenv, jclass jcls, jobjectArray& jarg )
{
// do something
jobjectArray t = .....

jarg = t;
}

How can I set the jobjectArray& back, so the data is return in the
parameter x?


You can't. Java doesn't have a pass-by-reference call scheme, at all.
Java uses only pass by value. You could:

1. Return the new array as a return value.
2. Create a reference to a reference so you can replace the 2nd one:
class myclass {

void native mymethod( Double[][] x )
}

will give you a pointer to an array which you can then modify.

A Google search for "Java pass by reference" will give you lots of
details. Maybe add "jni" to get some clues more specific results for
your particular situation.

<https://www.google.com/search?q=java+pass+by+reference>
 
P

Philipp Kraus

Hello,

I hope this question is not OT.

I create a JNI call for this Java method

class myclass {

void native mymethod( Double[] x )
}

so the parameter x should be a call-by-reference, I would set x in the
native JNI function:

void mymethod( JNIEnv *jenv, jclass jcls, jobjectArray& jarg )
{
// do something
jobjectArray t = .....

jarg = t;
}

How can I set the jobjectArray& back, so the data is return in the
parameter x?


You can't. Java doesn't have a pass-by-reference call scheme, at all.
Java uses only pass by value. You could:

1. Return the new array as a return value.
2. Create a reference to a reference so you can replace the 2nd one:
class myclass {

void native mymethod( Double[][] x )
}

will give you a pointer to an array which you can then modify.

A Google search for "Java pass by reference" will give you lots of
details. Maybe add "jni" to get some clues more specific results for
your particular situation.

You are not right, because the problem is not the Java side, it is the
JNI (C/C++) side, so under C/C++ exists call-by-reference.

In detail I have got a native (C) methode, that shows in Java:

class myclass {
native void mymethod( Double[] x, Double[][] y )
}

The JNI glue code generates a

void mymethod (JNIEnv* jnv, jclass jcls, jobjectArray arg1, jobjectArray arg2 )

So the reference to the underlaying heap object is passed in the JNI
code parameters arg1 & arg2,
so I would modify this two heap adresses.

If I call in Java this code:

Double[] x = null;
Double[][] y = null;

myclass.mymethod(x, y);
// x & y are now filled with data


in x & y should be create a new array object, so I need a
GlobalReference on the heap of the VM
and push back the reference. I can create the global object on the VM
heap, but how can I push back
the reference
 
M

markspace

You are not right,


I'm not right? And yet...

heap, but how can I push back
the reference


....the problem still exists.

In detail I have got a native (C) methode, that shows in Java:


Truthfully, there is not enough detail here for me to guess what the
problem really is. You're showing method signatures but no code.

If I call in Java this code:

Double[] x = null;
Double[][] y = null;

myclass.mymethod(x, y);


No this will not work. I guess I was not specific enough: *you* have to
create a reference to the array reference you want to modify. That
doesn't happen if the parameter is null.

Double x = { {1.2} };

Now you have something to modify. Java does NOT have pass by reference,
you must do it yourself. I did a Google search, and I didn't see the
solution, so here I guess is some lost knowledge. This is Java, you'll
have to translate to C++ on your own:

class Example {

// manual "pass by reference"
void makeNewDoubleArray( Double [][] x ) {
x[0] = new Double[] { 1.1, 2.2, 3.3 };
}

public static void main( String... args ) {
Double[] y = {0.0};
Double[][] wrapper = { {} };
wrapper[0] = y; // pack
makeNewDoubleArray( wrapper );
y = wrapper[0] // unpack
System.out.println( java.util.Arrays.deepToString( y ) );
}
}

Code is untested; watch out for silly errors.
 
L

Lew

markspace said:
I'm not right? And yet...


...the problem still exists.


Truthfully, there is not enough detail here for me to guess what the
problem really is. You're showing method signatures but no code.

What he did show was a parameter passed from Java as a C++ reference.

Does that even work? Java doesn't have anything like C++ references to pass.

That being the advice the OP blew off immediately.
If I call in Java this code:

Double[] x = null;

'x' is equivalent to a pointer, not a reference.

Pardon my ignorance, but what if you used a pointer in the native code
instead of a reference?
Double[][] y = null;

myclass.mymethod(x, y);

Java convention calls for type names to start with an upper-case letter
and use camel case.
No this will not work. I guess I was not specific enough: *you* have to
create a reference to the array reference you want to modify. That

Well, in C++ terms, wouldn't that be a pointer?
doesn't happen if the parameter is null.

Double x = { {1.2} };

Now you have something to modify. Java does NOT have pass by reference,
you must do it yourself. I did a Google search, and I didn't see the
solution, so here I guess is some lost knowledge. This is Java, you'll
have to translate to C++ on your own:

class Example {

// manual "pass by reference"

void makeNewDoubleArray( Double [][] x ) {
x[0] = new Double[] { 1.1, 2.2, 3.3 };
}

public static void main( String... args ) {
Double[] y = {0.0};
Double[][] wrapper = { {} };
wrapper[0] = y; // pack
makeNewDoubleArray( wrapper );
y = wrapper[0] // unpack
System.out.println( java.util.Arrays.deepToString( y ) );
}
}

Code is untested; watch out for silly errors.

I am ignorant of the ways of JNI, so my question might be extraordinarily
off base.
 
M

markspace

I am ignorant of the ways of JNI, so my question might be extraordinarily
off base.


I'm pretty ignorant of JNI also, but I'm making the assumption that
Chris Upal is correct (he posted after you and I; I was just making an
educated guess). It *must* work the same was as native Java, since
there's no other way for Java to handle it.

The OP is just confused about reference vs value, and hoping Java has an
"out." It doesn't, and he'll have to adjust his thinking.
 
S

Steven Simpson

class myclass {

void native mymethod( Double[] x )
}

I realise this snippet could be just for illustration, but 'native' must
come before the return type in real code. Missing semicolon too.
so the parameter x should be a call-by-reference, I would set x in the
native JNI function:

void mymethod( JNIEnv *jenv, jclass jcls, jobjectArray& jarg )

When I tried the class (after the fixes) with javah, I got this:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class myclass */

#ifndef _Included_myclass
#define _Included_myclass
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: myclass
* Method: mymethod
* Signature: ([Ljava/lang/Double;)V
*/
JNIEXPORT void JNICALL Java_myclass_mymethod
(JNIEnv *, jobject, jobjectArray);

#ifdef __cplusplus
}
#endif
#endif

The jobjectArray parameter has no '&'. If it had one, it would not be
possible to link it with a C implementation. If an '&' is inserted
manually, and the function is implemented in C++, the machine code would
likely be identical to an equivalent function using a pointer, but the
JVM would not pass a jobjectArray* to it, and you'd get undefined behaviour.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top