[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Recovery of values of fields from his ID in JNI

If I have an ID of a field in JNI, how Can I know its value
(if it is a primitive) or reference (if it is an object) and modify it?

UJM
0
ujuarez71
Asked:
ujuarez71
  • 6
  • 5
  • 4
3 Solutions
 
CEHJCommented:
Can you elaborate on

>>If I have an ID of a field in JNI

?
0
 
ujuarez71Author Commented:
I have an ID field, so I want to know its value.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
objectsCommented:
Use the releveant GetXXXField() method depending on the type of the field.
0
 
objectsCommented:
heres an example:

#include <stdio.h>
#include <jni.h>
#include "FieldAccess.h"

JNIEXPORT void JNICALL
Java_FieldAccess_accessFields(JNIEnv *env, jobject obj)
{
  jclass cls = (*env)->GetObjectClass(env, obj);
  jfieldID fid;
  jstring jstr;
  const char *str;
  jint si;

  printf("In C:\n");

  fid = (*env)->GetStaticFieldID(env, cls, "si", "I");
  if (fid == 0) {
    return;
  }
  si = (*env)->GetStaticIntField(env, cls, fid);
  printf("  FieldAccess.si = %d\n", si);
  (*env)->SetStaticIntField(env, cls, fid, 200);
 
  fid = (*env)->GetFieldID(env, cls, "s", "Ljava/lang/String;");
  if (fid == 0) {
    return;
  }
  jstr = (*env)->GetObjectField(env, obj, fid);
  str = (*env)->GetStringUTFChars(env, jstr, 0);
  printf("  c.s = \"%s\"\n", str);
  (*env)->ReleaseStringUTFChars(env, jstr, str);

  jstr = (*env)->NewStringUTF(env, "123");
  (*env)->SetObjectField(env, obj, fid, jstr);
}


0
 
ujuarez71Author Commented:
This apply for arguments too? That Is, given a method identifier, how I can obtain the values of its arguments?
0
 
objectsCommented:
Arguments are passed when a method is called. A method alone does not have argument values.
0
 
ujuarez71Author Commented:
Then, the only thing that I can do is to define native methods in my source code and to pass the arguments that I need. That's right?
0
 
objectsCommented:
yes
0
 
CEHJCommented:
>>Then, the only thing that I can do ...

What is it that you're trying to do in fact?
0
 
CEHJCommented:
ujuarez71, can you say

a. Why you ignored the correct comment I gave that answered your question?
b. Why you accepted an answer to a different question?
0
 
ujuarez71Author Commented:
I apologize widely by the incident. My doubt extended and was
clarified quickly. My error was not to raise another new question or to distribute the points.
0
 
CEHJCommented:
>>or to distribute the points

It's still possible to do that by posting a 0-point question to CS ;-)

http://www.experts-exchange.com/Community_Support/
0
 
objectsCommented:
My additional clarifications should deserve a larger share of the split :)
0
 
CEHJCommented:
Thanks ;-)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now