| 1 | <!DOCTYPE article PUBLIC "-//Davenport//DTD DocBook V3.0//EN">
|
|---|
| 2 | <article>
|
|---|
| 3 | <artheader>
|
|---|
| 4 | <title>The Cygnus Native Interface for C++/Java Integration</title>
|
|---|
| 5 | <subtitle>Writing native Java methods in natural C++</subtitle>
|
|---|
| 6 | <authorgroup>
|
|---|
| 7 | <corpauthor>Cygnus Solutions</corpauthor>
|
|---|
| 8 | </authorgroup>
|
|---|
| 9 | <date>March, 2000</date>
|
|---|
| 10 | </artheader>
|
|---|
| 11 |
|
|---|
| 12 | <abstract><para>
|
|---|
| 13 | This documents CNI, the Cygnus Native Interface,
|
|---|
| 14 | which is is a convenient way to write Java native methods using C++.
|
|---|
| 15 | This is a more efficient, more convenient, but less portable
|
|---|
| 16 | alternative to the standard JNI (Java Native Interface).</para>
|
|---|
| 17 | </abstract>
|
|---|
| 18 |
|
|---|
| 19 | <sect1><title>Basic Concepts</title>
|
|---|
| 20 | <para>
|
|---|
| 21 | In terms of languages features, Java is mostly a subset
|
|---|
| 22 | of C++. Java has a few important extensions, plus a powerful standard
|
|---|
| 23 | class library, but on the whole that does not change the basic similarity.
|
|---|
| 24 | Java is a hybrid object-oriented language, with a few native types,
|
|---|
| 25 | in addition to class types. It is class-based, where a class may have
|
|---|
| 26 | static as well as per-object fields, and static as well as instance methods.
|
|---|
| 27 | Non-static methods may be virtual, and may be overloaded. Overloading is
|
|---|
| 28 | resolved at compile time by matching the actual argument types against
|
|---|
| 29 | the parameter types. Virtual methods are implemented using indirect calls
|
|---|
| 30 | through a dispatch table (virtual function table). Objects are
|
|---|
| 31 | allocated on the heap, and initialized using a constructor method.
|
|---|
| 32 | Classes are organized in a package hierarchy.
|
|---|
| 33 | </para>
|
|---|
| 34 | <para>
|
|---|
| 35 | All of the listed attributes are also true of C++, though C++ has
|
|---|
| 36 | extra features (for example in C++ objects may be allocated not just
|
|---|
| 37 | on the heap, but also statically or in a local stack frame). Because
|
|---|
| 38 | <acronym>gcj</acronym> uses the same compiler technology as
|
|---|
| 39 | <acronym>g++</acronym> (the GNU C++ compiler), it is possible
|
|---|
| 40 | to make the intersection of the two languages use the same
|
|---|
| 41 | <acronym>ABI</acronym> (object representation and calling conventions).
|
|---|
| 42 | The key idea in <acronym>CNI</acronym> is that Java objects are C++ objects,
|
|---|
| 43 | and all Java classes are C++ classes (but not the other way around).
|
|---|
| 44 | So the most important task in integrating Java and C++ is to
|
|---|
| 45 | remove gratuitous incompatibilities.
|
|---|
| 46 | </para>
|
|---|
| 47 | <para>
|
|---|
| 48 | You write CNI code as a regular C++ source file. (You do have to use
|
|---|
| 49 | a Java/CNI-aware C++ compiler, specifically a recent version of G++.)</para>
|
|---|
| 50 | <para>
|
|---|
| 51 | You start with:
|
|---|
| 52 | <programlisting>
|
|---|
| 53 | #include <gcj/cni.h>
|
|---|
| 54 | </programlisting></para>
|
|---|
| 55 |
|
|---|
| 56 | <para>
|
|---|
| 57 | You then include header files for the various Java classes you need
|
|---|
| 58 | to use:
|
|---|
| 59 | <programlisting>
|
|---|
| 60 | #include <java/lang/Character.h>
|
|---|
| 61 | #include <java/util/Date.h>
|
|---|
| 62 | #include <java/lang/IndexOutOfBoundsException.h>
|
|---|
| 63 | </programlisting></para>
|
|---|
| 64 |
|
|---|
| 65 | <para>
|
|---|
| 66 | In general, <acronym>CNI</acronym> functions and macros start with the
|
|---|
| 67 | `<literal>Jv</literal>' prefix, for example the function
|
|---|
| 68 | `<literal>JvNewObjectArray</literal>'. This convention is used to
|
|---|
| 69 | avoid conflicts with other libraries.
|
|---|
| 70 | Internal functions in <acronym>CNI</acronym> start with the prefix
|
|---|
| 71 | `<literal>_Jv_</literal>'. You should not call these;
|
|---|
| 72 | if you find a need to, let us know and we will try to come up with an
|
|---|
| 73 | alternate solution. (This manual lists <literal>_Jv_AllocBytes</literal>
|
|---|
| 74 | as an example; <acronym>CNI</acronym> should instead provide
|
|---|
| 75 | a <literal>JvAllocBytes</literal> function.)</para>
|
|---|
| 76 | <para>
|
|---|
| 77 | These header files are automatically generated by <command>gcjh</command>.
|
|---|
| 78 | </para>
|
|---|
| 79 | </sect1>
|
|---|
| 80 |
|
|---|
| 81 | <sect1><title>Packages</title>
|
|---|
| 82 | <para>
|
|---|
| 83 | The only global names in Java are class names, and packages.
|
|---|
| 84 | A <firstterm>package</firstterm> can contain zero or more classes, and
|
|---|
| 85 | also zero or more sub-packages.
|
|---|
| 86 | Every class belongs to either an unnamed package or a package that
|
|---|
| 87 | has a hierarchical and globally unique name.
|
|---|
| 88 | </para>
|
|---|
| 89 | <para>
|
|---|
| 90 | A Java package is mapped to a C++ <firstterm>namespace</firstterm>.
|
|---|
| 91 | The Java class <literal>java.lang.String</literal>
|
|---|
| 92 | is in the package <literal>java.lang</literal>, which is a sub-package
|
|---|
| 93 | of <literal>java</literal>. The C++ equivalent is the
|
|---|
| 94 | class <literal>java::lang::String</literal>,
|
|---|
| 95 | which is in the namespace <literal>java::lang</literal>,
|
|---|
| 96 | which is in the namespace <literal>java</literal>.
|
|---|
| 97 | </para>
|
|---|
| 98 | <para>
|
|---|
| 99 | Here is how you could express this:
|
|---|
| 100 | <programlisting>
|
|---|
| 101 | // Declare the class(es), possibly in a header file:
|
|---|
| 102 | namespace java {
|
|---|
| 103 | namespace lang {
|
|---|
| 104 | class Object;
|
|---|
| 105 | class String;
|
|---|
| 106 | ...
|
|---|
| 107 | }
|
|---|
| 108 | }
|
|---|
|
|---|