JNI的“jobject this”是什么?它的用途是什么?

时间:2022-07-01 16:03:36

I am having a hard time finding an answer to this. But, what is "jobject this" used for in JNI function calls? For example:

我很难找到答案。但是,JNI函数调用中使用的“jobject this”是什么?例如:

jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject this ) {

I use env to allocate objects often, but I've never used thiz and I'm not sure what it is for. Just for knowledge purposes.

我经常使用env来分配对象,但我从未使用过thiz,我也不确定它的用途。只是对知识的目的。

3 个解决方案

#1


13  

The following is a JNI wrapper function which has two parameters, and returns a primitive array of objects:

下面是一个JNI包装函数,它有两个参数,返回一个原始对象数组:

jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject thiz );

From the function name you have given I don't think it is complete, that is, you haven't respected the obligatory function name convention which is:

从你给出的函数名来看,我不认为它是完整的,也就是说,你没有遵守义务函数名约定即:

  1. Start the function with Java_

    使用Java_启动函数

  2. Append the package name separated by _ (undescores) i.e. com_company_awesomeapp. So far the function name is composed of: Java_com_company_awesomeapp

    附加以_ (undescore)分隔的包名,即com_company_awesomeapp。到目前为止,函数名由:Java_com_company_awesomeapp组成

  3. Append the Java class name where the native method has been defined,followed by the actual function name. So at this point we should have the following function name: Java_com_company_awesomeapp_MainActivity_Test

    附加Java类名,其中定义了本机方法,然后是实际的函数名。现在我们应该有以下函数名:Java_com_company_awesomeapp_MainActivity_Test

The first parameter is a pointer to a structure storing all JNI function pointers, i.e. all the predefined functions you have available after you #include <jni.h>.

第一个参数是一个指向存储所有JNI函数指针的结构的指针,即您在#include < JNI .h>之后拥有的所有预定义函数。

The second parameter is a reference to the Java object inside which this native method has been declared in. You can use it to call the other methods of the Java object from the current JNI function, i.e. Call Java instance methods from JNI code written in C or C++.

第二个参数是对Java对象的引用,其中声明了这个本机方法。您可以使用它从当前的JNI函数调用Java对象的其他方法,即从用C或c++编写的JNI代码调用Java实例方法。

If for example you have the following Java class inside the MainActivity.java file:

例如,如果主活动中有以下Java类。java文件:

public class MainActivity extends Activity{    static    {        try        {            System.loadLibrary("mynativelib");        }        catch (UnsatisfiedLinkError ule)        {            Log.e(TAG, "WARNING: Could not load native library: " + ule.getMessage());        }    }    public static native Object[] Test();}

Then, the jobject thiz parameter of the JNI function would be a reference to an object of type MainActivity.

然后,JNI函数的jobject thiz参数将是对类型MainActivity对象的引用。

#2


1  

I found this link that should help clarify the question.

我发现这个链接应该有助于澄清这个问题。

https://library.vuforia.com/articles/Solution/How-To-Communicate-Between-Java-and-C-using-the-JNI

https://library.vuforia.com/articles/Solution/How-To-Communicate-Between-Java-and-C-using-the-JNI

Here is an example in it that uses the "jobject".

这里有一个使用“jobject”的例子。

JNIEXPORT void JNICALLJava_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_initApplicationNative(                            JNIEnv* env, jobject obj, jint width, jint height){    ...    jclass activityClass = env->GetObjectClass(obj);    jmethodID getTextureCountMethodID = env->GetMethodID(activityClass,                                                    "getTextureCount", "()I");    if (getTextureCountMethodID == 0)    {        LOG("Function getTextureCount() not found.");        return;    }    textureCount = env->CallIntMethod(obj, getTextureCountMethodID);    ...}

#3


0  

jobject thiz means the this in java class.

jobject thiz在java类中表示this。

Sometimes if you create a static native method like this.

有时,如果您创建一个像这样的静态本机方法。

void Java_MyClass_method1 (JNIEnv *, jclass);

jclass means the class itself.

jclass表示类本身。

#1


13  

The following is a JNI wrapper function which has two parameters, and returns a primitive array of objects:

下面是一个JNI包装函数,它有两个参数,返回一个原始对象数组:

jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject thiz );

From the function name you have given I don't think it is complete, that is, you haven't respected the obligatory function name convention which is:

从你给出的函数名来看,我不认为它是完整的,也就是说,你没有遵守义务函数名约定即:

  1. Start the function with Java_

    使用Java_启动函数

  2. Append the package name separated by _ (undescores) i.e. com_company_awesomeapp. So far the function name is composed of: Java_com_company_awesomeapp

    附加以_ (undescore)分隔的包名,即com_company_awesomeapp。到目前为止,函数名由:Java_com_company_awesomeapp组成

  3. Append the Java class name where the native method has been defined,followed by the actual function name. So at this point we should have the following function name: Java_com_company_awesomeapp_MainActivity_Test

    附加Java类名,其中定义了本机方法,然后是实际的函数名。现在我们应该有以下函数名:Java_com_company_awesomeapp_MainActivity_Test

The first parameter is a pointer to a structure storing all JNI function pointers, i.e. all the predefined functions you have available after you #include <jni.h>.

第一个参数是一个指向存储所有JNI函数指针的结构的指针,即您在#include < JNI .h>之后拥有的所有预定义函数。

The second parameter is a reference to the Java object inside which this native method has been declared in. You can use it to call the other methods of the Java object from the current JNI function, i.e. Call Java instance methods from JNI code written in C or C++.

第二个参数是对Java对象的引用,其中声明了这个本机方法。您可以使用它从当前的JNI函数调用Java对象的其他方法,即从用C或c++编写的JNI代码调用Java实例方法。

If for example you have the following Java class inside the MainActivity.java file:

例如,如果主活动中有以下Java类。java文件:

public class MainActivity extends Activity{    static    {        try        {            System.loadLibrary("mynativelib");        }        catch (UnsatisfiedLinkError ule)        {            Log.e(TAG, "WARNING: Could not load native library: " + ule.getMessage());        }    }    public static native Object[] Test();}

Then, the jobject thiz parameter of the JNI function would be a reference to an object of type MainActivity.

然后,JNI函数的jobject thiz参数将是对类型MainActivity对象的引用。

#2


1  

I found this link that should help clarify the question.

我发现这个链接应该有助于澄清这个问题。

https://library.vuforia.com/articles/Solution/How-To-Communicate-Between-Java-and-C-using-the-JNI

https://library.vuforia.com/articles/Solution/How-To-Communicate-Between-Java-and-C-using-the-JNI

Here is an example in it that uses the "jobject".

这里有一个使用“jobject”的例子。

JNIEXPORT void JNICALLJava_com_qualcomm_QCARSamples_ImageTargets_ImageTargets_initApplicationNative(                            JNIEnv* env, jobject obj, jint width, jint height){    ...    jclass activityClass = env->GetObjectClass(obj);    jmethodID getTextureCountMethodID = env->GetMethodID(activityClass,                                                    "getTextureCount", "()I");    if (getTextureCountMethodID == 0)    {        LOG("Function getTextureCount() not found.");        return;    }    textureCount = env->CallIntMethod(obj, getTextureCountMethodID);    ...}

#3


0  

jobject thiz means the this in java class.

jobject thiz在java类中表示this。

Sometimes if you create a static native method like this.

有时,如果您创建一个像这样的静态本机方法。

void Java_MyClass_method1 (JNIEnv *, jclass);

jclass means the class itself.

jclass表示类本身。