How to invoke assignment operator in a native method?

Discussion in 'Java' started by HappyHippy, Aug 30, 2006.

  1. HappyHippy

    HappyHippy Guest

    Hi,

    Could you please help me to figure out how to invoke assignment operator
    in a native method (C/C++ side)?
    For StringBuffer "append" method I am doing the following:
    ....
    jmethodID methodId = env->GetMethodID(objClass, "append",
    "(Ljava/lang/String;)Ljava/lang/StringBuffer;");
    ....

    Can I do something like this for Integer's assignment operator?
    Something like:

    jmethodID methodId = env->GetMethodID(objClass, "=",
    "(Ljava/lang/Integer;)Ljava/lang/Integer;");

    And then just invoke it via: env->CallObjectMethod(..., methodId,....)


    Thank you!
     
    HappyHippy, Aug 30, 2006
    #1
    1. Advertising

  2. On Wed, 30 Aug 2006 13:44:41 -0400, HappyHippy wrote:
    > Can I do something like this for Integer's assignment operator?
    > Something like:
    >
    > jmethodID methodId = env->GetMethodID(objClass, "=",
    > "(Ljava/lang/Integer;)Ljava/lang/Integer;");


    Please explain exactly what it is you're trying to achieve, not how
    you hope to achieve it.

    To see what methods the Integer class has, use javap -s. The only way
    to assign a value to an Integer is to specify the value when you
    invoke the constructor.

    If your class has an Integer field and you want to assign an
    (existing) Integer *object* to it, use SetObjectField().

    Please don't multipost. I've *crossposted* this reply and set
    followups to c.l.j.programmer.

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Aug 30, 2006
    #2
    1. Advertising

  3. HappyHippy

    HappyHippy Guest

    Gordon Beaton wrote:
    > On Wed, 30 Aug 2006 13:44:41 -0400, HappyHippy wrote:
    >
    >> Can I do something like this for Integer's assignment operator?
    >> Something like:
    >>
    >> jmethodID methodId = env->GetMethodID(objClass, "=",
    >> "(Ljava/lang/Integer;)Ljava/lang/Integer;");
    >>

    >
    > Please explain exactly what it is you're trying to achieve, not how
    > you hope to achieve it.
    >
    > To see what methods the Integer class has, use javap -s. The only way
    > to assign a value to an Integer is to specify the value when you
    > invoke the constructor.
    >
    > If your class has an Integer field and you want to assign an
    > (existing) Integer *object* to it, use SetObjectField().
    >
    > Please don't multipost. I've *crossposted* this reply and set
    > followups to c.l.j.programmer.
    >
    > /gordon
    >
    >

    I want to achieve the effect of the following assignment (which can be
    done in Java)

    SomeNumber = 5;

    where SomeNumber is of type java.lang.Integer


    But in a native method I do not have an Integer object, I have jobject
    instead.
    So, my question is: is it possible to invoke assignment operator "=" on
    the object referenced by jobject?
    As far as I know (I don't know much about Java...) assignment operator
    is kind of special and it is "hidden" in Java.

    Thank you
     
    HappyHippy, Aug 30, 2006
    #3
  4. On Wed, 30 Aug 2006 14:27:34 -0400, HappyHippy wrote:
    > I want to achieve the effect of the following assignment (which can
    > be done in Java)
    >
    > SomeNumber = 5;
    >
    > where SomeNumber is of type java.lang.Integer


    This is only possible due to autoboxing, and is shorthand for

    SomeNumber = new Integer(5);

    In native code, you do it by invoking the Integer constructor (an
    instance method with the name "<init>") then taking the resulting
    Integer and assigning it to the field in your object:

    /* create an Integer */
    jclass iclass = (*env)->FindClass(env,"java/lang/Integer");
    jmethodID mid = (*env)->GetMethodID(env,iclass,"<init>","(I)V");
    jobject i = (*env)->NewObject(env,iclass,mid,5);

    /* assign the field */
    jclass oclass = (*env)->GetObjectClass(env,myobj);
    jfieldID fid = (*env)->GetFieldID(env,oclass,
    "SomeNumber",
    "Ljava/lang/Integer;");
    (*env)->SetObjectField(env,myobj,fid,i);

    /gordon

    --
    [ don't email me support questions or followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
     
    Gordon Beaton, Aug 30, 2006
    #4
  5. HappyHippy

    Oliver Wong Guest

    "HappyHippy" <> wrote in message
    news:ed4lb4$9a3$-state.edu...
    > Gordon Beaton wrote:
    >> On Wed, 30 Aug 2006 13:44:41 -0400, HappyHippy wrote:
    >>
    >>> Can I do something like this for Integer's assignment operator?
    >>> Something like:
    >>>
    >>> jmethodID methodId = env->GetMethodID(objClass, "=",
    >>> "(Ljava/lang/Integer;)Ljava/lang/Integer;");
    >>>

    >>
    >> Please explain exactly what it is you're trying to achieve, not how
    >> you hope to achieve it.
    >>
    >> To see what methods the Integer class has, use javap -s. The only way
    >> to assign a value to an Integer is to specify the value when you
    >> invoke the constructor.
    >>
    >> If your class has an Integer field and you want to assign an
    >> (existing) Integer *object* to it, use SetObjectField().
    >>
    >> Please don't multipost. I've *crossposted* this reply and set
    >> followups to c.l.j.programmer.
    >>
    >> /gordon
    >>
    >>

    > I want to achieve the effect of the following assignment (which can be
    > done in Java)
    >
    > SomeNumber = 5;
    >
    > where SomeNumber is of type java.lang.Integer
    >
    >
    > But in a native method I do not have an Integer object, I have jobject
    > instead.
    > So, my question is: is it possible to invoke assignment operator "=" on
    > the object referenced by jobject?
    > As far as I know (I don't know much about Java...) assignment operator is
    > kind of special and it is "hidden" in Java.


    This is basically what the compile does under the covers:

    Integer someNumber = Integer.valueOf(5);

    If you know how to express that in your native code, then you can
    perform the assignment.

    - Oliver
     
    Oliver Wong, Aug 30, 2006
    #5
  6. HappyHippy

    Chris Smith Guest

    HappyHippy <> wrote:
    > I want to achieve the effect of the following assignment (which can be
    > done in Java)
    >
    > SomeNumber = 5;
    >
    > where SomeNumber is of type java.lang.Integer
    >
    > But in a native method I do not have an Integer object, I have jobject
    > instead.


    In addition to Gordon and Oliver's replies, let me be explicit about
    something. When you write:

    SomeNumber = 5;

    which is equivalent to:

    SomeNumber = Integer.valueOf(5);

    you are not doing *anything* the object that SomeNumber currently points
    to. You are reassigning SomeNumber to point to a completely new object.
    You say you've got a jobject, but you don't say which one it is. The
    jobject that SomeNumber points *to* doesn't help you at all.
    Presumably, SomeNumber is a field (you can't access local variables of
    Java methods from JNI at all), and you need the jobject that *contains*
    the SomeNumber field. Once you've got that, then you can call
    SetObjectField as Gordon described.

    Incidentally, naming conventions are there for a reason, and are
    absolutely universal among professional Java programmers. Call the
    field someNumber, not SomeNumber.

    --
    Chris Smith
     
    Chris Smith, Aug 31, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jerry051
    Replies:
    2
    Views:
    756
    Oliver Sturm
    Aug 2, 2005
  2. Storm
    Replies:
    1
    Views:
    457
    Chris Uppal
    May 1, 2004
  3. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    492
    Barry Schwarz
    Feb 8, 2005
  4. Chris
    Replies:
    34
    Views:
    1,530
  5. Hicham Mouline
    Replies:
    2
    Views:
    699
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page