Android Learning Note----C control Java in JNI

Source: Internet
Author: User

Object-oriented implementation of the underlying

Java, as an object-oriented high-level language, can model the real world. And the process is different is the object-oriented software writing is not the accumulation of processes, but the business logic of multi-view decomposition and classification. The process is roughly:
1), the knowledge decomposition into different granularity of the small concept.
2), the concept of classification, the formation of classes, modules, systems
3), using computer language to implement it as an object (including operations and data)

However, most programmers seldom think about how the object-oriented architecture is implemented when writing code. Because it belongs to the bottom, it is not necessary. However, people who have studied Java reflection technology and think about it will realize that the various object-oriented grammars themselves are also interesting systems. For example, the property itself is an object of the field class, which is the object of the method class, and even the class is the class object.

JNI Learning Summary


JNI is an interactive protocol between Java and C in Android, which specifies the interaction between Java code and C code, variable access, new objects, and so on. Java is an object-oriented language, and C is a process-oriented language. There is a difference between the grammar and the problem-solving way of thinking, so how to solve the conflict?
The answer is to go deep into the bottom of the object-oriented system and manipulate it in attributes, methods, and classes. Java classes run in VMS, each object, every method, every property, every class is managed by a virtual machine, and then mapped to the underlying binary data stored in memory. So, if there is a VM interface exposed to the programmer, you can imagine what will happen. We can create a new object without new, without having to call the function, assigning a value to the property without assigning a value. We can operate directly on properties, methods, and classes by requesting VMS, where there is a similar reflection mechanism to Java. In a different way, the C call to Java in JNI is in C syntax, not Java syntax, and C is done in a variety of VM-level operations directly through pointers to VMS.
All in all, the whole idea of C calling Java is to implement object-oriented action decomposition through VM management of classes, methods, properties, and objects.

C to get the class from the current object thiz:
Jclass clazz = (*env)->getobjectclass (env,thiz);

Get a function ID from the class:
Jmethodid M_mid = (*env)->getmethodid (Env,clazz, "Setv", "(I) V");

Use object and function IDs to invoke object functions in Java objects (class functions use class IDs):
(*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 property ID to take the values in the Java object:
int n = (int) (*ENV)->getobjectfiled (ENV,THIZ,M_FID);

Get a class:
Jclass Rvclass = (*env)->findclass (env, "com/misoo/counter/resultvalue");

class to get the constructor ID:
Jmethodid constr = (*env)->getmethodid (Env,rvclass, "<init>", "() V");

New object:
Jobject ref = (*env)->newobject (ENV,RVCLASS,CONSTR);

* Above env is a pointer variable pointing to an object that can handle a VM request.

Android Learning Note----C control Java in JNI

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.