Solved

Recovery of values of fields from his ID in JNI

Posted on 2004-08-18
16
180 Views
Last Modified: 2013-11-23
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
Comment
Question by:ujuarez71
  • 6
  • 5
  • 4
16 Comments
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Can you elaborate on

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

?
0
 

Author Comment

by:ujuarez71
Comment Utility
I have an ID field, so I want to know its value.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
Comment Utility
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Use the releveant GetXXXField() method depending on the type of the field.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 75 total points
Comment Utility
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
 

Author Comment

by:ujuarez71
Comment Utility
This apply for arguments too? That Is, given a method identifier, how I can obtain the values of its arguments?
0
 
LVL 92

Accepted Solution

by:
objects earned 75 total points
Comment Utility
Arguments are passed when a method is called. A method alone does not have argument values.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:ujuarez71
Comment Utility
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
 
LVL 92

Expert Comment

by:objects
Comment Utility
yes
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>Then, the only thing that I can do ...

What is it that you're trying to do in fact?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
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
 

Author Comment

by:ujuarez71
Comment Utility
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
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>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
 
LVL 92

Expert Comment

by:objects
Comment Utility
My additional clarifications should deserve a larger share of the split :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
Thanks ;-)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now