Skip to content

Latest commit

 

History

History
205 lines (153 loc) · 5.72 KB

2.6.md

File metadata and controls

205 lines (153 loc) · 5.72 KB
public class JNIAccessField extends BaseOperation {
    private static final String TAG = "JNIAccessField";
    final Animal animal = new Animal("animal");
    static int num = 1;

    @Override
    public void invoke() {

        // 访问实例的字段
        accessInstanceFiled(animal);
        Log.d(TAG, "after access instance field,the name is " + animal.getName());

        // 访问静态类的字段
        accessStaticField(animal);
        Log.d(TAG, "after access static field,the num is " + Animal.num);

        staticAccessInstanceField();
        Log.d(TAG, "--staticAccessInstanceField--,the num is " + JNIAccessField.num);

        // 访问实例的方法
        callInstanceMethod(animal);
        // 访问静态类的方法
        callStaticMethod(animal);
    }

    private native int accessInstanceFiled(Animal animal);

    private native int accessStaticField(Animal animal);

    private native void callInstanceMethod(Animal animal);

    private native void callStaticMethod(Animal animal);

    public static native void staticAccessInstanceField();

}
extern "C"
JNIEXPORT jint JNICALL
Java_com_jdqm_ndktutorials_jni_JNIAccessField_accessInstanceFiled(JNIEnv *env, jobject thiz,
                                                                  jobject animal) {

    //找到这个类
    jclass cls = env->GetObjectClass(animal);
    jfieldID fid = env->GetFieldID(cls, "name", "Ljava/lang/String;");//找到实例字段
    jstring str = env->NewStringUTF("this is new name");
    //重新设值
    env->SetObjectField(animal, fid, str);
    return JNI_OK;
}


extern "C"
JNIEXPORT jint JNICALL
Java_com_jdqm_ndktutorials_jni_JNIAccessField_accessStaticField(JNIEnv *env, jobject thiz,
                                                                jobject animal) {
    jclass cls = env->GetObjectClass(animal);//找到类
    jfieldID fid = env->GetStaticFieldID(cls, "num", "I");//找到静态字段
    int num = env->GetStaticIntField(cls, fid);
    //重新设值
    env->SetStaticIntField(cls, fid, num + 1);
    return JNI_OK;
}




extern "C"
JNIEXPORT void JNICALL
Java_com_jdqm_ndktutorials_jni_JNIAccessField_staticAccessInstanceField(JNIEnv *env,
                                                                        jclass clazz) {

    jfieldID fid = env->GetStaticFieldID(clazz, "num", "I");
    int num = env->GetStaticIntField(clazz, fid);
    //重新设值
    env->SetStaticIntField(clazz, fid, num + 1);
}


// Native 访问 Java 的类实例方法
extern "C"
JNIEXPORT void JNICALL
Java_com_jdqm_ndktutorials_jni_JNIAccessField_callInstanceMethod(JNIEnv *env, jobject thiz,
                                                                 jobject animal) {

    jclass cls = env->GetObjectClass(animal);
    jmethodID mid = env->GetMethodID(cls, "callInstanceMethod", "(I)V");
    if (mid != nullptr) {
        env->CallVoidMethod(animal, mid, 2);
    }
}




// Native 访问 Java 的静态方法
extern "C"
JNIEXPORT void JNICALL
Java_com_jdqm_ndktutorials_jni_JNIAccessField_callStaticMethod(JNIEnv *env, jobject thiz,
                                                               jobject animal) {

    jclass cls = env->GetObjectClass(animal);
    jmethodID argsmid = nullptr;

    argsmid = env->GetStaticMethodID(cls, "callStaticMethod",
                                               "(Ljava/lang/String;)Ljava/lang/String;");
    if (argsmid == nullptr) {
        return;
    }
    jstring jstr = env->NewStringUTF("jstring");
    env->CallStaticObjectMethod(cls, argsmid, jstr);



    argsmid = env->GetStaticMethodID(cls, "callStaticMethod",
                                     "([Ljava/lang/String;I)Ljava/lang/String;");
    if (argsmid == nullptr) {
        return;
    }

    jobjectArray objArray;

    jclass objclass = env->FindClass("java/lang/String");

    if (objclass == nullptr) {
        return;
    }

    int size = 100;
    objArray = env->NewObjectArray(size, objclass, nullptr);

    if (objArray == nullptr) {
        return;
    }
    jstring strElement;
    for (int i = 0; i < size; ++i) {
        strElement = env->NewStringUTF("str in c");
        env->SetObjectArrayElement(objArray, i, strElement);
    }

    env->CallStaticObjectMethod(cls, argsmid, objArray, size);

}
public class Animal {
    private static final String TAG = "Animal";


    protected String name;

    public static int num = 0;

    public Animal(String name) {
        this.name = name;
    }


    public String getName() {
        Log.d(TAG,"call getName method");
        return this.name;
    }

    public int getNum() {
        return num;
    }

    // C++ 调用 Java 的实例方法
    public void callInstanceMethod(int num) {
        Log.d(TAG, "call instance method and num is " + num);
    }

    // C++ 调用 Java 的类方法
    public static String callStaticMethod(String str) {

        if (str != null) {
            Log.d(TAG, "call static method with " + str);
        } else {
            Log.d(TAG, "call static method str is null");
        }
        return "";
    }

    public static String callStaticMethod(String[] strs, int num) {
        Log.d(TAG, "call static method with string array " + num);
        if (strs != null) {
//            for (String str : strs) {
//                Log.d(TAG, "str in array is " + str+"");
//            }
            for (int i = 0; i <strs.length ; i++) {
                Log.d(TAG, "str in array is " + strs[i]+ i);
            }
        }
        return "";
    }

    public static void callStaticVoidMethod() {
        Log.d(TAG, "call static void method");
    }
}