S
stixwix
Most of the examples I've seen for JNI seem to allow access to 'top
level' instance variables like this:
public class HelloNative{
private String str;
private native void setObjLong();
public static void main(String[] args) {
HelloNative test = new HelloNative();
test.setObjLong();
System.out.println("The java s is: "+ test.str);
}
}
The string is created in the c code:
JNIEXPORT void JNICALL
Java_HelloNative_setObjLong (JNIEnv* env, jobject obj) {
/* get the class object */
jclass cls = (*env)->GetObjectClass(env, obj);
/* get the field id */
jfieldID fid = (*env)->GetFieldID(env, cls, "str",
"Ljava/lang/String;");
/* make new string */
jstring myStr = (*env)->NewStringUTF(env,"1234");
/* set the value */
(*env)->SetObjectField(env, obj, fid, myStr);
return 0;
}
This all works fine, the question is how do you get to user-defined
object's instanace variables?
So if I change the java bit thus:
public class HelloNative {
private MyClass myObj = new MyClass();
private native void setObjLong();
class MyClass{
private long address;
// getters and setters omitted
}
public static void main(String[] args) {
HelloNative test = new HelloNative();
test.setObjLong();
System.out.println("The java long is: "+
test.myObj.getAddress());
}
}
How can I set the address field of myObj?
If I try:
jfieldID fid = (*env)->GetFieldID(env, cls, "myObj",
"Ljava/lang/Object;");
I get:
java.lang.NoSuchFieldError: HelloNative.myObj
....and anyway this would only get me to the object and not to the
address instance which I wish to set.
Thanks,
Andy
level' instance variables like this:
public class HelloNative{
private String str;
private native void setObjLong();
public static void main(String[] args) {
HelloNative test = new HelloNative();
test.setObjLong();
System.out.println("The java s is: "+ test.str);
}
}
The string is created in the c code:
JNIEXPORT void JNICALL
Java_HelloNative_setObjLong (JNIEnv* env, jobject obj) {
/* get the class object */
jclass cls = (*env)->GetObjectClass(env, obj);
/* get the field id */
jfieldID fid = (*env)->GetFieldID(env, cls, "str",
"Ljava/lang/String;");
/* make new string */
jstring myStr = (*env)->NewStringUTF(env,"1234");
/* set the value */
(*env)->SetObjectField(env, obj, fid, myStr);
return 0;
}
This all works fine, the question is how do you get to user-defined
object's instanace variables?
So if I change the java bit thus:
public class HelloNative {
private MyClass myObj = new MyClass();
private native void setObjLong();
class MyClass{
private long address;
// getters and setters omitted
}
public static void main(String[] args) {
HelloNative test = new HelloNative();
test.setObjLong();
System.out.println("The java long is: "+
test.myObj.getAddress());
}
}
How can I set the address field of myObj?
If I try:
jfieldID fid = (*env)->GetFieldID(env, cls, "myObj",
"Ljava/lang/Object;");
I get:
java.lang.NoSuchFieldError: HelloNative.myObj
....and anyway this would only get me to the object and not to the
address instance which I wish to set.
Thanks,
Andy