Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "jobject thiz" in JNI and what is it used for?

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

jobjectArray Java_com_gnychis_awmon_Test( JNIEnv* env, jobject thiz ) {

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.

like image 417
gnychis Avatar asked Jan 20 '13 20:01

gnychis


2 Answers

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

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_

  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

  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

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>.

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++.

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

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.

like image 67
Alex Bitek Avatar answered Oct 05 '22 02:10

Alex Bitek


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

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

JNIEXPORT void JNICALL
Java_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);
    ...
}
like image 24
mdang Avatar answered Oct 05 '22 02:10

mdang