I'm new to JNI programming and have just read Sun's tutorial by Beth
Stearns, so please excuse if this is a stupid question.
My application is to get a kind of Trie structure in C wrapped in a Java
class. The existing code has quite some global variables because it
wasn't designed to be thread safe -- as it is it wouldn't even allow
multiple objects. So I suppose I have to pack all of them into a
structure and store a pointer to it in the Object's instance data.
Would it be OK to use an Object attribute on the Java side and do
something like this in C (adding error checks of course)?
struct myData *data = malloc(sizeof *data);
// fill the structure
jclass cls = (*env)->GetObjectClass(env, obj);
jfieldID fid = (*env)->GetFieldID(env, cls, "cdata", "Ljava/lang/Object;");
(*env)->SetObjectField(env, obj, fid, myData);
Apart from the expensive signature lookup (I do have to do this on every
invocation on my function if I want to access member variables, don't
I?) this would look fine, but may I misuse an Object reference in this
way without the GC or anything assuming things about my structure that
aren't there?
TIA!
Matthias
Gordon Beaton - 23 May 2004 10:59 GMT
> So I suppose I have to pack all of them into a structure and store a
> pointer to it in the Object's instance data. Would it be OK to use
[quoted text clipped - 12 lines]
> Object reference in this way without the GC or anything assuming
> things about my structure that aren't there?
Hold the pointer in a long field in java.
I think you'll find that it's generally a lot easier to use return
values and method arguments to pass values back and forth, than it is
to use reflective mechanisms for setting and getting field values from
the native code.
You probably don't want to expose the extra argument to users of the
class, in which case it's easy enough to define the native methods as
private helpers to the corresponding public methods.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Matthias Bethke - 07 Jun 2004 10:49 GMT
begin followup to Gordon Beaton:
> Hold the pointer in a long field in java.
>
> I think you'll find that it's generally a lot easier to use return
> values and method arguments to pass values back and forth, than it is
> to use reflective mechanisms for setting and getting field values from
> the native code.
OK, so my first impression that this is quite a hassle wasn't that wrong
:) I thought I had to do quite some callback and instance-data
manipulation stuff anyway because that was how the original code wanted
it, but now I found I can do much of this in Java without calling JNI
and back to Java from the C function.
Thanks
Matthias

Signature
end
Chris Gray - 24 May 2004 21:44 GMT
> I'm new to JNI programming and have just read Sun's tutorial by Beth
> Stearns, so please excuse if this is a stupid question.
[quoted text clipped - 15 lines]
> invocation on my function if I want to access member variables, don't
> I?)
No. Do he lookup once and store the resulting jclass, jfieldID somewhere,
anywhere.
> this would look fine, but may I misuse an Object reference in this
> way without the GC or anything assuming things about my structure that
> aren't there?
You will almost certainly crash GC if you do this. Better to store a
"handle" to your structure (could even be a direct pointer) in an int or
long field.

Signature
Chris Gray chris@kiffer.eunet.be
/k/ Embedded Java Solutions