Consolidate Java and C + + with Jace

Source: Internet
Author: User

Summary

Jace is a free open-source tool that allows us to easily develop JNI (Java Native Interface) code. This article gives a detailed analysis of the JNI API and how to use Jace to solve these problems.

Without a deeper understanding, we would have thought that Sun was designing JNI to keep Java programmers from using it. After all, type safety is a fake, and without the error checking mechanism, a simple Java method call requires 4 or more JNI calls, which are obvious deficiencies of JNI. In addition, we must also manage jnienv pointers, which cannot be used in multiple threads, and must be selected for each possible operation in 9 function calls, and access to exception information is also very difficult. This is only part of the problem with JNI, and we can find many other problems.

Many of these restrictions are related to the binding of JNI and C, and the C language itself is very poorly supported for type safety and exception handling mechanisms. Although most programmers now have the ability to write code in C + +, Sun has not given up C programmers, which is why JNI is currently in this situation. Unfortunately, this difficult API poses a lot of difficulties for developers.

Jace is a free open source toolkit designed to make JNI programming easier. It supports the automatic generation of C + + proxy classes by Java class files and the integration of C + + and Java exceptions, arrays, packages, and objects, and manages the thread bindings and lifecycles of Java references. More importantly, it enables us to develop modules that are smaller, easier to understand, and type safe at compile time.

Type System of JNI

The most basic feature of Jace is that it uses C + + proxy classes to express Java types. To really understand the merits of the proxy class, we first need to look at the type system of JNI. Sun uses 24 types of C in JNI to represent all possible Java types. JNI contains 9 simple types:

Jboolean

Jbyte

Jchar

Jshort

Jint

Jlong

jdouble

jfloat

void

JNI has 14 types of references, as shown in the following illustration:

(Figure: PICTURE01)

In addition, JNI has a composite type jvalue that can express all the simple and reference types.

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.