Android Studio 3.0 Develops JNI Process--The Representation of Java Polymorphism in JNI

Keywords: Attribute Java Android

In the development of DNK, you will inexplicably encounter the problems of Java polymorphism, which subclasses and parent classes. Today, we will write a simple Demo for you to learn.

Java code is simple

  public static native String tt();

    public static String t(){
        List<String> l = new LinkedList();
        l.add("123");   //Simulating false data is not to get these data, in fact, the data is implemented in jni.
        l.add("456");
        return l.get(1);
    }

Among them, the local t() method in the class needs to be implemented in jni, that is, the t t() local method to implement the t() method.

jni Code:

jstring tt(JNIEnv *env, jclass type){

    LOGE(".....test java Polymorphism in jni Forms of expression.....");

    jobject l;   //Define ajobject

    jclass linkedList_clazz = findClass(env, "java/util/LinkedList");

    jobject linkedList_obj = newObject(env, linkedList_clazz);

    l=linkedList_obj;

    jclass list_clazz = findClass(env, "java/util/List");

    //Ljava/util/List;->add(Ljava/lang/Object;)Z

    jmethodID add_methodID = getMethodID(env, list_clazz, "add", "(Ljava/lang/Object;)Z");

    jmethodID get_methodID = getMethodID(env, list_clazz, "get", "(I)Ljava/lang/Object;");

    //
    env->CallBooleanMethod(l,add_methodID,env->NewStringUTF("Data 1"));

    env->CallBooleanMethod(l,add_methodID,env->NewStringUTF("Data 2"));

    //Ljava/util/List;->get(I)Ljava/lang/Object;

    jobject data = env->CallObjectMethod(l, get_methodID,1);  //Get the second data, and the collection starts at corner 0.

    return (jstring)data;
}

Code description:

java code

List l = new LinkedList();
List It's the parent class, and LinkedList yes List Subclass, create(new)The object process is actually Java The manifestation of polymorphism.

jni code

jobject l defines objects in the form of List l
All reference type superclasses (parent classes) in jni are job objects (except String types).
The above code findClass, getMethodID, newObject are all tool classes written by myself. In fact, they are just looking for classes, methods and creating objects, just like env - > findClass principle.

Make up some smali code <It's easy to understand if you understand Android decompilation>:

.method public static t()Ljava/lang/String;
    .registers 2

    .prologue
    .line 37
    new-instance v0, Ljava/util/LinkedList;

    invoke-direct {v0}, Ljava/util/LinkedList;-><init>()V

    .line 38
    .local v0, "l":Ljava/util/List;, "Ljava/util/List<Ljava/lang/String;>;"
    const-string v1, "123"

    invoke-interface {v0, v1}, Ljava/util/List;->add(Ljava/lang/Object;)Z

    .line 39
    const-string v1, "456"

    invoke-interface {v0, v1}, Ljava/util/List;->add(Ljava/lang/Object;)Z

    .line 40
    const/4 v1, 0x1

    invoke-interface {v0, v1}, Ljava/util/List;->get(I)Ljava/lang/Object;

    move-result-object v1

    check-cast v1, Ljava/lang/String;

    return-object v1
.end method

.method public static native tt()Ljava/lang/String;
.end method

Operation results:

12-25 20:34:13.450 8025-8025/com.tencent.mm.wvs E/=======capture=======:... test the manifestation of java polymorphism in jni...
12-25 20:34:13.451 8025-8025/com.tencent.mm.wvs E/fj: Get the second data of the list: Data 2

Posted by alimadzi on Sun, 10 Feb 2019 03:36:17 -0800