JNI newbie: storing instance data? - Java

This is a discussion on JNI newbie: storing instance data? - Java ; 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. ...

+ Reply to Thread
Results 1 to 4 of 4

JNI newbie: storing instance data?

  1. Default JNI newbie: storing instance data?

    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

  2. Default Re: JNI newbie: storing instance data?

    On 21 May 2004 14:58:24 GMT, Matthias Bethke wrote:
    > 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?


    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

    --
    [ 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

  3. Default Re: JNI newbie: storing instance data?

    Matthias Bethke wrote:

    > 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?)


    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.


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


  4. Default Re: JNI newbie: storing instance data?

    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

    --
    end

+ Reply to Thread

Similar Threads

  1. newbie - creating a new instance
    By Application Development in forum CSharp
    Replies: 4
    Last Post: 10-18-2007, 03:18 PM
  2. storing data not in xml nor database
    By Application Development in forum CSharp
    Replies: 6
    Last Post: 09-15-2007, 03:56 AM
  3. Thoughts on storing address data with XML data type?
    By Application Development in forum Framework and Interface Programming
    Replies: 340
    Last Post: 01-19-2007, 05:35 PM
  4. Storing data methods?
    By Application Development in forum basic.visual
    Replies: 4
    Last Post: 10-25-2006, 10:31 AM
  5. storing xml data into a sybase db
    By Application Development in forum XML SOAP
    Replies: 2
    Last Post: 08-06-2006, 07:13 AM