source: trunk/src/gcc/libjava/doc/cni.sgml@ 869

Last change on this file since 869 was 2, checked in by bird, 23 years ago

Initial revision

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