Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Virtual Machine / June 2004

Tip: Looking for answers? Try searching our database.

JNI newbie: storing instance data?

Thread view: 
Matthias Bethke - 21 May 2004 15:58 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.
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.