Re: How to invoke assignment operator in a native method?



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
.