将jobject与值相关联 [英] associating jobject with a value

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

问题描述

jclass object = (*env)->FindClass(env,"java/lang/Integer") // C Code

有什么方法可以将 integer 值与object关联?我希望object包含/指向一个整数.

Is there any way i can associate an integer value with object ? I want object to contain/point to an integer number.

推荐答案

请确保您已阅读理解

Make sure you read an understand Confusing jclass with jobject in the Pitfalls section of the JNI guide.

FindClass(env, "Foo")返回类型为java.lang.Class的对象(的句柄).从概念上讲,它等效于 Class.forName(String) 静态方法:它不会返回您作为参数(Foo)提供的类的实例.它返回表示该类的Class类型的对象.

FindClass(env, "Foo") returns (a handle to) an object of type java.lang.Class. It is conceptually equivalent to the Class.forName(String) static method: it does not return an instance of the class you give it as a parameter (Foo). It returns an object of type Class which represents that class.

使用jclass(或Class)可以执行的操作是找到所需的构造函数,然后调用该构造函数以创建Foo类型的对象.

What you can do with a jclass (or a Class) is find the constructor you want , and invoke that constructor to create an object of type Foo.

JNI指南在为类String调用构造函数.对于类Integer的操作类似,除了方法签名之外.

The JNI guide has an example of how you do this in the Invoking constructors for class String. Doing it for class Integer is similar, except for the method signature.

您将执行以下操作:

jclass clazz = (*env)->FindClass(env, "java/lang/Integer");
jmethodID mid = (*env)->GetMethodID(env, clazz, "<init>", "(I)V");
jobject mint = (*env)->NewObject(env, clazz, mid, 42); // your desired value here

(需要错误检查.)

这篇关于将jobject与值相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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