Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to invoke assignment operator in a native method?

Reply
Thread Tools

How to invoke assignment operator in a native method?

 
 
HappyHippy
Guest
Posts: n/a
 
      08-30-2006
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/StringLjava/lang/StringBuffer;");
....

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

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

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


Thank you!
 
Reply With Quote
 
 
 
 
Gordon Beaton
Guest
Posts: n/a
 
      08-30-2006
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/IntegerLjava/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
 
Reply With Quote
 
 
 
 
HappyHippy
Guest
Posts: n/a
 
      08-30-2006
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/IntegerLjava/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
 
Reply With Quote
 
Gordon Beaton
Guest
Posts: n/a
 
      08-30-2006
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
 
Reply With Quote
 
Oliver Wong
Guest
Posts: n/a
 
      08-30-2006

"HappyHippy" <(E-Mail Removed)> wrote in message
news:ed4lb4$9a3$(E-Mail Removed)-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/IntegerLjava/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

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      08-31-2006
HappyHippy <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
POD and assignment operator and test operator Hicham Mouline C++ 2 09-01-2009 06:00 PM
conditions for automatic generation of default ctor, copy ctor,and default assignment operator (operator) puzzlecracker C++ 8 04-15-2008 09:56 PM
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
comma operator and assignment operator G Patel C Programming 4 02-08-2005 02:53 AM



Advertisments