How to invoke assignment operator in a native method?

H

HappyHippy

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!
 
G

Gordon Beaton

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
 
H

HappyHippy

Gordon said:
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
 
G

Gordon Beaton

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
 
O

Oliver Wong

HappyHippy said:
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
 
C

Chris Smith

HappyHippy said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top