将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
(需要错误检查.)