关于GetFieldID的澄清 [英] Clarification about GetFieldID

查看:53
本文介绍了关于GetFieldID的澄清的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建本机方法,尽管它似乎运行良好,但是我仍在努力理解函数中的字段[* sig]-

I was trying to build a native method and although it seems to run fine, but I am struggling to understand a field [*sig] in a function -

jfieldID GetFieldID(JNIEnv *env, jclass clazz,
    const char *name, const char *sig);

https://docs.oracle. com/javase/8/docs/technotes/guides/jni/spec/functions.html

例如,我有一个小的Java代码,它使用一个名为"i"的实例变量,而所有此本机方法(名为test)所做的就是将此乘以2.

For example I have a small Java code, which uses an instance variable called "i" and all this native method(named test) does is multiply this by 2.

现在我指的是Java完整参考书-这里的代码就像-

Now I am referring to Java Complete Reference book - here the code goes like -

我的问题是-这是什么我"- fid =(* env)-> GetFieldId(env,cls,"i","I");

My question is - what is "I" in this - fid = (*env)->GetFieldId(env,cls,"i", "I");

当我阅读Oracle文档时,它说- https://docs.oracle.com/javase /8/docs/technotes/guides/jni/spec/functions.html

When I read Oracle documenttion it says - https://docs.oracle.com/javase/8/docs/technotes/guides/jni/spec/functions.html

访问对象的字段
GetFieldID

Accessing Fields of Objects
GetFieldID

    jfieldID GetFieldID(JNIEnv *env, jclass clazz,
const char *name, const char *sig);

返回类的实例(非静态)字段的字段ID.该字段由其名称和签名指定.访问器函数的GetField和SetField家族使用字段ID来检索对象字段.
GetFieldID()导致未初始化的类被初始化.
GetFieldID()无法用于获取数组的长度字段.改为使用GetArrayLength().
链接:
JNIEnv接口功能表中的索引94.
参数:
env:JNI接口指针.
clazz:一个Java类对象.
name:以0终止的修改后的UTF-8字符串中的字段名称.
sig:字段签名,以0终止的已修改UTF-8字符串.

Returns the field ID for an instance (nonstatic) field of a class. The field is specified by its name and signature. The GetField and SetField families of accessor functions use field IDs to retrieve object fields.
GetFieldID() causes an uninitialized class to be initialized.
GetFieldID() cannot be used to obtain the length field of an array. Use GetArrayLength() instead.
LINKAGE:
Index 94 in the JNIEnv interface function table.
PARAMETERS:
env: the JNI interface pointer.
clazz: a Java class object.
name: the field name in a 0-terminated modified UTF-8 string.
sig: the field signature in a 0-terminated modified UTF-8 string.

能否请您详细说明此字段"sig"?我无法将我"与任何事物联系起来.

Could you please may be elaborate what is this field "sig"? I couldn't relate "I" with anything.

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

JNIEXPORT void JNICALL Java_NativeDemo_test(JNIEnv *env , jobject obj)
{
    jclass cls;
    jfieldID fid;
    jint i;

    printf("Starting the native method\n");

    cls = (*env)->GetObjectClass(env,obj);
    fid = (*env)->GetFieldId(env,cls,"i", "I");

推荐答案

请参阅以下Core Java v.2 ch中的员工"示例. 12,由Horstmann和Cornell在 http://www.horstmann.com/corejava.html "id"是字段的名称,在这里 私人双薪; 在Employee类内部;还必须指定其签名("sig")或类型"D"(双精度).在您的代码中,"I"是整数. 请参见 http://上的类型字段" docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html 一旦获得了jFieldId,它就可以用来修改 字段.

please see the "employee" example below in Core Java, v. 2 ch. 12, by Horstmann and Cornell at http://www.horstmann.com/corejava.html the "id" is the name of the field, here private double salary; inside the Employee class; its signature ("sig"), or type, "D" (double) also must be specified. in your code, "I" is integer. see the "Type Fields" at http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html once the jFieldId is obtained, it can be used to modify the value of the field.

/**
  @version 1.10 1999-11-13
  @author Cay Horstmann
*/

#include "Employee.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_Employee_raiseSalary(JNIEnv* env, jobject     this_obj, jdouble byPercent)
{  
   /* get the class */
   jclass class_Employee = (*env)->GetObjectClass(env, this_obj);

   /* get the field ID */
   jfieldID id_salary = (*env)->GetFieldID(env, class_Employee, "salary", "D");

   /* get the field value */
   jdouble salary = (*env)->GetDoubleField(env, this_obj, id_salary);

   salary *= 1 + byPercent / 100;

   /* set the field value */
   (*env)->SetDoubleField(env, this_obj, id_salary, salary);
}

这篇关于关于GetFieldID的澄清的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆