Android learning notes ---- c control in JNI java, android ---- jni

Source: Internet
Author: User

Android learning notes ---- c control in JNI java, android ---- jni

Underlying Implementation of object-oriented

Java, as an advanced object-oriented language, can be used to model the real world. What is different from process-oriented is that the compilation of object-oriented software is not the accumulation of processes, but the multi-view decomposition and classification of business logic. The process is roughly as follows:
1) break down knowledge into small concepts of different granularities.
2) classify concepts to form classes, modules, and systems
3) implement it as an object (including operations and data) in computer languages)

However, most programmers seldom think about how the object-oriented system is implemented when writing code. It is not necessary because it belongs to the underlying layer. However, those who have learned the Java reflection technology and thought about it will realize that the object-oriented syntax itself is also an interesting system. For example, attributes are the objects of the Field Class, methods are the objects of the Method Class, and even classes are Class objects.

 

JNI learning Summary


JNI is the interaction protocol between java and c in android. It specifies mutual calls between java code and c code, variable access, and new object operations. Java is an object-oriented language, while c is a process-oriented language. The two are different in terms of syntax and the way of thinking to solve the problem. How can we solve the conflict?
The answer is to go deep into the bottom layer of the object-oriented system and operate on attributes, methods, and classes. Java classes run in the VM. Every object, every method, every attribute, and every class are managed by the virtual machine, and then mapped to the underlying binary data stored in the memory. Therefore, if a VM interface is exposed to a programmer, you can imagine what will happen. We can create an object without the need of new, without calling a function or assigning a value to the attribute. We can directly perform operations on attributes, methods, and classes by requesting the VM, which is similar to the reflection mechanism of Java. In JNI, c calls java using c syntax instead of java syntax. c directly performs various VM-level operations by associating VM pointers.
All in all, the overall idea of c calling java is to implement the decomposition of object-oriented actions through the management of classes, methods, attributes, and objects by VM.

Obtain classes from the current object thiz in c:
Jclass clazz = (* env)-> GetObjectClass (env, thiz );

Obtain a function ID from the class:
JmethodID m_mid = (* env)-> GetMethodID (env, clazz, "setV", "(I) V ");

Use the object and function ID to call the object functions in the java object (class functions use class ID ):
(* Env)-> CallVoidMethod (env, thiz, m_mid, data );

Get the property ID from the class:
JFiledID m_fid = (* env)-> GetFiledID (env, clazz, "data", "I ");

Use the object and attribute ID to get the values in the java object:
Int n = (int) (* env)-> GetObjectFiled (env, thiz, m_fid );

Obtain a class:
Jclass rvclass = (* env)-> FindClass (env, "com/misoo/counter/ResultValue ");

Class to obtain the constructor ID:
JmethodID constr = (* env)-> GetMethodID (env, rvclass, "<init>", "() V ");

New object:
Jobject ref = (* env)-> NewObject (env, rvclass, constr );

 

* The above env is a pointer variable pointing to an object that can process VM requests.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.