Opps. Wrong file the first time.
[official-gcc.git] / libjava / doc / cni.sgml
blobfac01db7512f32cb74528431c804f33770432e7f
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>February, 1999</date>
10 </artheader>
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>
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 &lt;cni.h&gt;
54 </programlisting></para>
56 <para>
57 You then include header files for the various Java classes you need
58 to 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>
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>
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;
110 class java::lang::String : public java::lang::Object
114 </programlisting>
115 </para>
116 <para>
117 The <literal>gcjh</literal> tool automatically generates the
118 nessary namespace declarations.</para>
120 <sect2><title>Nested classes as a substitute for namespaces</title>
121 <para>
122 <!-- FIXME the next line reads poorly jsm -->
123 It is not that long since g++ got complete namespace support,
124 and it was very recent (end of February 1999) that <literal>libgcj</literal>
125 was changed to uses namespaces. Releases before then used
126 nested classes, which are the C++ equivalent of Java inner classes.
127 They provide similar (though less convenient) functionality.
128 The old syntax is:
129 <programlisting>
130 class java {
131 class lang {
132 class Object;
133 class String;
136 </programlisting>
137 The obvious difference is the use of <literal>class</literal> instead
138 of <literal>namespace</literal>. The more important difference is
139 that all the members of a nested class have to be declared inside
140 the parent class definition, while namespaces can be defined in
141 multiple places in the source. This is more convenient, since it
142 corresponds more closely to how Java packages are defined.
143 The main difference is in the declarations; the syntax for
144 using a nested class is the same as with namespaces:
145 <programlisting>
146 class java::lang::String : public java::lang::Object
147 { ... }
148 </programlisting>
149 Note that the generated code (including name mangling)
150 using nested classes is the same as that using namespaces.</para>
151 </sect2>
153 <sect2><title>Leaving out package names</title>
154 <para>
155 <!-- FIXME next line reads poorly jsm -->
156 Having to always type the fully-qualified class name is verbose.
157 It also makes it more difficult to change the package containing a class.
158 The Java <literal>package</literal> declaration specifies that the
159 following class declarations are in the named package, without having
160 to explicitly name the full package qualifiers.
161 The <literal>package</literal> declaration can be followed by zero or
162 more <literal>import</literal> declarations, which allows either
163 a single class or all the classes in a package to be named by a simple
164 identifier. C++ provides something similar
165 with the <literal>using</literal> declaration and directive.
166 </para>
167 <para>
168 A Java simple-type-import declaration:
169 <programlisting>
170 import <replaceable>PackageName</replaceable>.<replaceable>TypeName</replaceable>;
171 </programlisting>
172 allows using <replaceable>TypeName</replaceable> as a shorthand for
173 <literal><replaceable>PackageName</replaceable>.<replaceable>TypeName</replaceable></literal>.
174 The C++ (more-or-less) equivalent is a <literal>using</literal>-declaration:
175 <programlisting>
176 using <replaceable>PackageName</replaceable>::<replaceable>TypeName</replaceable>;
177 </programlisting>
178 </para>
179 <para>
180 A Java import-on-demand declaration:
181 <programlisting>
182 import <replaceable>PackageName</replaceable>.*;
183 </programlisting>
184 allows using <replaceable>TypeName</replaceable> as a shorthand for
185 <literal><replaceable>PackageName</replaceable>.<replaceable>TypeName</replaceable></literal>
186 The C++ (more-or-less) equivalent is a <literal>using</literal>-directive:
187 <programlisting>
188 using namespace <replaceable>PackageName</replaceable>;
189 </programlisting>
190 </para>
191 </sect2>
192 </sect1>
194 <sect1><title>Primitive types</title>
195 <para>
196 Java provides 8 <quote>primitives</quote> types:
197 <literal>byte</literal>, <literal>short</literal>, <literal>int</literal>,
198 <literal>long</literal>, <literal>float</literal>, <literal>double</literal>,
199 <literal>char</literal>, and <literal>boolean</literal>.
200 These are the same as the following C++ <literal>typedef</literal>s
201 (which are defined by <literal>cni.h</literal>):
202 <literal>jbyte</literal>, <literal>jshort</literal>, <literal>jint</literal>,
203 <literal>jlong</literal>, <literal>jfloat</literal>,
204 <literal>jdouble</literal>,
205 <literal>jchar</literal>, and <literal>jboolean</literal>.
206 You should use the C++ typenames
207 (<ForeignPhrase><Abbrev>e.g.</Abbrev></ForeignPhrase> <literal>jint</literal>),
208 and not the Java types names
209 (<ForeignPhrase><Abbrev>e.g.</Abbrev></ForeignPhrase> <literal>int</literal>),
210 even if they are <quote>the same</quote>.
211 This is because there is no guarantee that the C++ type
212 <literal>int</literal> is a 32-bit type, but <literal>jint</literal>
213 <emphasis>is</emphasis> guaranteed to be a 32-bit type.
215 <informaltable frame="all" colsep="1" rowsep="0">
216 <tgroup cols="3">
217 <thead>
218 <row>
219 <entry>Java type</entry>
220 <entry>C/C++ typename</entry>
221 <entry>Description</entry>
222 </thead>
223 <tbody>
224 <row>
225 <entry>byte</entry>
226 <entry>jbyte</entry>
227 <entry>8-bit signed integer</entry>
228 </row>
229 <row>
230 <entry>short</entry>
231 <entry>jshort</entry>
232 <entry>16-bit signed integer</entry>
233 </row>
234 <row>
235 <entry>int</entry>
236 <entry>jint</entry>
237 <entry>32-bit signed integer</entry>
238 </row>
239 <row>
240 <entry>long</entry>
241 <entry>jlong</entry>
242 <entry>64-bit signed integer</entry>
243 </row>
244 <row>
245 <entry>float</entry>
246 <entry>jfloat</entry>
247 <entry>32-bit IEEE floating-point number</entry>
248 </row>
249 <row>
250 <entry>double</entry>
251 <entry>jdouble</entry>
252 <entry>64-bit IEEE floating-point number</entry>
253 </row>
254 <row>
255 <entry>char</entry>
256 <entry>jchar</entry>
257 <entry>16-bit Unicode character</entry>
258 </row>
259 <row>
260 <entry>boolean</entry>
261 <entry>jboolean</entry>
262 <entry>logical (Boolean) values</entry>
263 </row>
264 <row>
265 <entry>void</entry>
266 <entry>void</entry>
267 <entry>no value</entry>
268 </row>
269 </tbody></tgroup>
270 </informaltable>
271 </para>
273 <para>
274 <funcsynopsis>
275 <funcdef><function>JvPrimClass</function></funcdef>
276 <paramdef><parameter>primtype</parameter></paramdef>
277 </funcsynopsis>
278 This is a macro whose argument should be the name of a primitive
279 type, <ForeignPhrase><Abbrev>e.g.</Abbrev></ForeignPhrase>
280 <literal>byte</literal>.
281 The macro expands to a pointer to the <literal>Class</literal> object
282 corresponding to the primitive type.
283 <ForeignPhrase><Abbrev>E.g.</Abbrev></ForeignPhrase>,
284 <literal>JvPrimClass(void)</literal>
285 has the same value as the Java expression
286 <literal>Void.TYPE</literal> (or <literal>void.class</literal>).
287 </para>
289 </sect1>
291 <sect1><title>Objects and Classes</title>
292 <sect2><title>Classes</title>
293 <para>
294 All Java classes are derived from <literal>java.lang.Object</literal>.
295 C++ does not have a unique <quote>root</quote>class, but we use
296 a C++ <literal>java::lang::Object</literal> as the C++ version
297 of the <literal>java.lang.Object</literal> Java class. All
298 other Java classes are mapped into corresponding C++ classes
299 derived from <literal>java::lang::Object</literal>.</para>
300 <para>
301 Interface inheritance (the <quote><literal>implements</literal></quote>
302 keyword) is currently not reflected in the C++ mapping.</para>
303 </sect2>
304 <sect2><title>Object references</title>
305 <para>
306 We implement a Java object reference as a pointer to the start
307 of the referenced object. It maps to a C++ pointer.
308 (We cannot use C++ references for Java references, since
309 once a C++ reference has been initialized, you cannot change it to
310 point to another object.)
311 The <literal>null</literal> Java reference maps to the <literal>NULL</literal>
312 C++ pointer.
313 </para>
314 <para>
315 Note that in some Java implementations an object reference is implemented as
316 a pointer to a two-word <quote>handle</quote>. One word of the handle
317 points to the fields of the object, while the other points
318 to a method table. Gcj does not use this extra indirection.
319 </para>
320 </sect2>
321 <sect2><title>Object fields</title>
322 <para>
323 Each object contains an object header, followed by the instance
324 fields of the class, in order. The object header consists of
325 a single pointer to a dispatch or virtual function table.
326 (There may be extra fields <quote>in front of</quote> the object,
327 for example for
328 memory management, but this is invisible to the application, and
329 the reference to the object points to the dispatch table pointer.)
330 </para>
331 <para>
332 The fields are laid out in the same order, alignment, and size
333 as in C++. Specifically, 8-bite and 16-bit native types
334 (<literal>byte</literal>, <literal>short</literal>, <literal>char</literal>,
335 and <literal>boolean</literal>) are <emphasis>not</emphasis>
336 widened to 32 bits.
337 Note that the Java VM does extend 8-bit and 16-bit types to 32 bits
338 when on the VM stack or temporary registers.</para>
339 <para>
340 If you include the <literal>gcjh</literal>-generated header for a
341 class, you can access fields of Java classes in the <quote>natural</quote>
342 way. Given the following Java class:
343 <programlisting>
344 public class Int
346 public int i;
347 public Integer (int i) { this.i = i; }
348 public static zero = new Integer(0);
350 </programlisting>
351 you can write:
352 <programlisting>
353 #include &lt;cni.h&gt;
354 #include &lt;Int.h&gt;
355 Int*
356 mult (Int *p, jint k)
358 if (k == 0)
359 return Int::zero; // static member access.
360 return new Int(p->i * k);
362 </programlisting>
363 </para>
364 <para>
365 <acronym>CNI</acronym> does not strictly enforce the Java access
366 specifiers, because Java permissions cannot be directly mapped
367 into C++ permission. Private Java fields and methods are mapped
368 to private C++ fields and methods, but other fields and methods
369 are mapped to public fields and methods.
370 </para>
371 </sect2>
372 <sect2><title>Non-Java fields</title>
373 <para>
374 When you write a Java wrapper around an existing library, that library
375 will often allocate and manage its own data structures. These are
376 <quote>objects</quote> that are not Java <literal>Object</literal>s;
377 instead they are usually C <literal>struct</literal> instances.
378 Typically, you will write a Java class, and use native CNI methods
379 which call functions in the C library. The problem is how to get
380 from the Java wrapper object to the C <literal>struct</literal> instances.
381 The obvious solution is to add a field to the Java object that
382 points to the C structure. The problem is that there is no Java
383 type that we can give to this field.</para>
384 <para>The GCJ solution is to define a special dummy class
385 <literal>gnu.gcj.RawData</literal>. This can be used as the type for fields,
386 parameters, array elements, or local variables in Java code.
387 It means that the field or variable is a pointer to a non-Java object.
388 Nothing else is known about it, so it corresponds to a
389 <literal>(void*)</literal> declaration is C or C++ code.</para>
390 <para>
391 The garbage collector will ignore a field that has type
392 <literal>gnu.gcj.RawData</literal>. You are responsible for
393 freeing the C data structure when you are done with it, and
394 performing any necessary cleanups. In most cases, you should
395 use a <literal>finalize</literal> method, and have it call
396 the library's cleanup routine. Also, the C data structure
397 should not contain a pointer back to the Java object, since
398 the garbage collector will not know about the pointer.
399 If you need to save a pointer to a Java object inside some
400 non-Java data structure, you first need to <quote>pin</quote>
401 or <quote>globalize</quote> the pointer; there is no CNI function
402 to do this yet.
403 (From the point of view of the
404 implementation, a <literal>gnu.gcj.RawData</literal> value is
405 the same as an integer that has the same size as a pointer.)</para>
406 <para>
407 Here is an example where we create a Java wrapper around C stdio:
408 <programlisting>
409 import gnu.gcj.RawData;
411 public class StdioFile
413 private RawData file;
414 public StdioFile (RawData file) { this.file = file; }
415 public StdioFile (String name, String mode)
416 throws FileNotFoundException
417 { init(name, mode); }
418 private native void init (String name, String mode)
419 throws FileNotFoundException;
420 public native int getc();
421 public native int close();
422 protected native void finalize();
424 </programlisting>
425 This is the CNI implementation:
426 <programlisting>
427 jint
428 StdioFile::getc()
430 return getc((FILE*) file);
433 jint
434 StdioFile::close()
436 return fclose((FILE*) file);
439 void
440 StdioFile::init(jstring name, jstring mode)
442 int cname_len = JvGetStringUTFLength (name);
443 int cmode_len = JvGetStringUTFLength (mode);
444 char cname[cname_len + 1];
445 char cmode[cmode_len + 1];
446 JvGetStringUTFRegion (name, 0, name->length(), cname);
447 JvGetStringUTFRegion (mode, 0, mode->length(), cmode);
448 cname[cname_len] = '\0';
449 cmode[cmode_len] = '\0';
450 file = (gnu::gcj::RawData*) fopen(cname, cmode);
451 if (file == NULL)
452 JvThrow(new java::lang::FileNotFoundException(name));
455 void
456 StdioFile::finalize()
458 fclose((FILE*) file);
460 </programlisting>
462 </sect2>
464 </sect1>
466 <sect1><title>Arrays</title>
467 <para>
468 While in many ways Java is similar to C and C++,
469 it is quite different in its treatment of arrays.
470 C arrays are based on the idea of pointer arithmetic,
471 which would be incompatible with Java's security requirements.
472 Java arrays are true objects (array types inherit from
473 <literal>java.lang.Object</literal>). An array-valued variable
474 is one that contains a reference (pointer) to an array object.
475 </para>
476 <para>
477 Referencing a Java array in C++ code is done using the
478 <literal>JArray</literal> template, which as defined as follows:
479 <programlisting>
480 class __JArray : public java::lang::Object
482 public:
483 int length;
486 template&lt;class T&gt;
487 class JArray : public __JArray
489 T data[0];
490 public:
491 T&amp; operator[](jint i) { return data[i]; }
493 </programlisting></para>
494 <para>
495 <funcsynopsis>
496 <funcdef>template&lt;class T&gt; T *<function>elements</function></funcdef>
497 <paramdef>JArray&lt;T&gt; &amp;<parameter>array</parameter></paramdef>
498 </funcsynopsis>
499 This template function can be used to get a pointer to the
500 elements of the <parameter>array</parameter>.
501 For instance, you can fetch a pointer
502 to the integers that make up an <literal>int[]</literal> like so:
503 <programlisting>
504 extern jintArray foo;
505 jint *intp = elements (foo);
506 </programlisting>
507 The name of this function may change in the future.</para>
508 <para>
509 There are a number of typedefs which correspond to typedefs from JNI.
510 Each is the type of an array holding objects of the appropriate type:
511 <programlisting>
512 typedef __JArray *jarray;
513 typedef JArray&lt;jobject&gt; *jobjectArray;
514 typedef JArray&lt;jboolean&gt; *jbooleanArray;
515 typedef JArray&lt;jbyte&gt; *jbyteArray;
516 typedef JArray&lt;jchar&gt; *jcharArray;
517 typedef JArray&lt;jshort&gt; *jshortArray;
518 typedef JArray&lt;jint&gt; *jintArray;
519 typedef JArray&lt;jlong&gt; *jlongArray;
520 typedef JArray&lt;jfloat&gt; *jfloatArray;
521 typedef JArray&lt;jdouble&gt; *jdoubleArray;
522 </programlisting>
523 </para>
524 <para>
525 You can create an array of objects using this function:
526 <funcsynopsis>
527 <funcdef>jobjectArray <function>JvNewObjectArray</function></funcdef>
528 <paramdef>jint <parameter>length</parameter></paramdef>
529 <paramdef>jclass <parameter>klass</parameter></paramdef>
530 <paramdef>jobject <parameter>init</parameter></paramdef>
531 </funcsynopsis>
532 Here <parameter>klass</parameter> is the type of elements of the array;
533 <parameter>init</parameter> is the initial
534 value to be put into every slot in the array.
535 </para>
536 <para>
537 For each primitive type there is a function which can be used
538 to create a new array holding that type. The name of the function
539 is of the form
540 `<literal>JvNew&lt;<replaceable>Type</replaceable>&gt;Array</literal>',
541 where `&lt;<replaceable>Type</replaceable>&gt;' is the name of
542 the primitive type, with its initial letter in upper-case. For
543 instance, `<literal>JvNewBooleanArray</literal>' can be used to create
544 a new array of booleans.
545 Each such function follows this example:
546 <funcsynopsis>
547 <funcdef>jbooleanArray <function>JvNewBooleanArray</function></funcdef>
548 <paramdef>jint <parameter>length</parameter></paramdef>
549 </funcsynopsis>
550 </para>
551 <para>
552 <funcsynopsis>
553 <funcdef>jsize <function>JvGetArrayLength</function></funcdef>
554 <paramdef>jarray <parameter>array</parameter></paramdef>
555 </funcsynopsis>
556 Returns the length of <parameter>array</parameter>.</para>
557 </sect1>
559 <sect1><title>Methods</title>
561 <para>
562 Java methods are mapped directly into C++ methods.
563 The header files generated by <literal>gcjh</literal>
564 include the appropriate method definitions.
565 Basically, the generated methods have the same names and
566 <quote>corresponding</quote> types as the Java methods,
567 and are called in the natural manner.</para>
569 <sect2><title>Overloading</title>
570 <para>
571 Both Java and C++ provide method overloading, where multiple
572 methods in a class have the same name, and the correct one is chosen
573 (at compile time) depending on the argument types.
574 The rules for choosing the correct method are (as expected) more complicated
575 in C++ than in Java, but given a set of overloaded methods
576 generated by <literal>gcjh</literal> the C++ compiler will choose
577 the expected one.</para>
578 <para>
579 Common assemblers and linkers are not aware of C++ overloading,
580 so the standard implementation strategy is to encode the
581 parameter types of a method into its assembly-level name.
582 This encoding is called <firstterm>mangling</firstterm>,
583 and the encoded name is the <firstterm>mangled name</firstterm>.
584 The same mechanism is used to implement Java overloading.
585 For C++/Java interoperability, it is important that both the Java
586 and C++ compilers use the <emphasis>same</emphasis> encoding scheme.
587 </para>
588 </sect2>
590 <sect2><title>Static methods</title>
591 <para>
592 Static Java methods are invoked in <acronym>CNI</acronym> using the standard
593 C++ syntax, using the `<literal>::</literal>' operator rather
594 than the `<literal>.</literal>' operator. For example:
595 </para>
596 <programlisting>
597 jint i = java::lang::Math::round((jfloat) 2.3);
598 </programlisting>
599 <para>
600 <!-- FIXME this next sentence seems ungammatical jsm -->
601 Defining a static native method uses standard C++ method
602 definition syntax. For example:
603 <programlisting>
604 #include &lt;java/lang/Integer.h&gt;
605 java::lang::Integer*
606 java::lang::Integer::getInteger(jstring str)
610 </programlisting>
611 </sect2>
613 <sect2><title>Object Constructors</title>
614 <para>
615 Constructors are called implicitly as part of object allocation
616 using the <literal>new</literal> operator. For example:
617 <programlisting>
618 java::lang::Int x = new java::lang::Int(234);
619 </programlisting>
620 </para>
621 <para>
622 <!-- FIXME rewrite needed here, mine may not be good jsm -->
623 Java does not allow a constructor to be a native method.
624 Instead, you could define a private method which
625 you can have the constructor call.
626 </para>
627 </sect2>
629 <sect2><title>Instance methods</title>
630 <para>
631 <!-- FIXME next para week, I would remove a few words from some sentences jsm -->
632 Virtual method dispatch is handled essentially the same way
633 in C++ and Java -- <abbrev>i.e.</abbrev> by doing an
634 indirect call through a function pointer stored in a per-class virtual
635 function table. C++ is more complicated because it has to support
636 multiple inheritance, but this does not effect Java classes.
637 However, G++ has historically used a different calling convention
638 that is not compatible with the one used by <acronym>gcj</acronym>.
639 During 1999, G++ will switch to a new ABI that is compatible with
640 <acronym>gcj</acronym>. Some platforms (including Linux) have already
641 changed. On other platforms, you will have to pass
642 the <literal>-fvtable-thunks</literal> flag to g++ when
643 compiling <acronym>CNI</acronym> code.
644 </para>
645 <para>
646 Calling a Java instance method in <acronym>CNI</acronym> is done
647 using the standard C++ syntax. For example:
648 <programlisting>
649 java::lang::Number *x;
650 if (x-&gt;doubleValue() &gt; 0.0) ...
651 </programlisting>
652 </para>
653 <para>
654 Defining a Java native instance method is also done the natural way:
655 <programlisting>
656 #include &lt;java/lang/Integer.h&gt;
657 jdouble
658 java::lang:Integer::doubleValue()
660 return (jdouble) value;
662 </programlisting>
663 </para>
664 </sect2>
666 <sect2><title>Interface method calls</title>
667 <para>
668 In Java you can call a method using an interface reference.
669 This is not yet supported in <acronym>CNI</acronym>.</para>
670 </sect2>
671 </sect1>
673 <sect1><title>Object allocation</title>
675 <para>
676 New Java objects are allocated using a
677 <firstterm>class-instance-creation-expression</firstterm>:
678 <programlisting>
679 new <replaceable>Type</replaceable> ( <replaceable>arguments</replaceable> )
680 </programlisting>
681 The same syntax is used in C++. The main difference is that
682 C++ objects have to be explicitly deleted; in Java they are
683 automatically deleted by the garbage collector.
684 Using <acronym>CNI</acronym>, you can allocate a new object
685 using standard C++ syntax. The C++ compiler is smart enough to
686 realize the class is a Java class, and hence it needs to allocate
687 memory from the garbage collector. If you have overloaded
688 constructors, the compiler will choose the correct one
689 using standard C++ overload resolution rules. For example:
690 <programlisting>
691 java::util::Hashtable *ht = new java::util::Hashtable(120);
692 </programlisting>
693 </para>
694 <para>
695 <funcsynopsis>
696 <funcdef>void *<function>_Jv_AllocBytes</function></funcdef>
697 <paramdef>jsize <parameter>size</parameter></paramdef>
698 </funcsynopsis>
699 Allocate <parameter>size</parameter> bytes. This memory is not
700 scanned by the garbage collector. However, it will be freed by
701 the GC if no references to it are discovered.
702 </para>
703 </sect1>
705 <sect1><title>Interfaces</title>
706 <para>
707 A Java class can <firstterm>implement</firstterm> zero or more
708 <firstterm>interfaces</firstterm>, in addition to inheriting from
709 a single base class.
710 An interface is a collection of constants and method specifications;
711 it is similar to the <firstterm>signatures</firstterm> available
712 as a G++ extension. An interface provides a subset of the
713 functionality of C++ abstract virtual base classes, but they
714 are currently implemented differently.
715 CNI does not currently provide any support for interfaces,
716 or calling methods from an interface pointer.
717 This is partly because we are planning to re-do how
718 interfaces are implemented in <acronym>gcj</acronym>.
719 </para>
720 </sect1>
722 <sect1><title>Strings</title>
723 <para>
724 <acronym>CNI</acronym> provides a number of utility functions for
725 working with Java <literal>String</literal> objects.
726 The names and interfaces are analogous to those of <acronym>JNI</acronym>.
727 </para>
729 <para>
730 <funcsynopsis>
731 <funcdef>jstring <function>JvNewString</function></funcdef>
732 <paramdef>const jchar *<parameter>chars</parameter></paramdef>
733 <paramdef>jsize <parameter>len</parameter></paramdef>
734 </funcsynopsis>
735 Creates a new Java String object, where
736 <parameter>chars</parameter> are the contents, and
737 <parameter>len</parameter> is the number of characters.
738 </para>
740 <para>
741 <funcsynopsis>
742 <funcdef>jstring <function>JvNewStringLatin1</function></funcdef>
743 <paramdef>const char *<parameter>bytes</parameter></paramdef>
744 <paramdef>jsize <parameter>len</parameter></paramdef>
745 </funcsynopsis>
746 Creates a new Java String object, where <parameter>bytes</parameter>
747 are the Latin-1 encoded
748 characters, and <parameter>len</parameter> is the length of
749 <parameter>bytes</parameter>, in bytes.
750 </para>
752 <para>
753 <funcsynopsis>
754 <funcdef>jstring <function>JvNewStringLatin1</function></funcdef>
755 <paramdef>const char *<parameter>bytes</parameter></paramdef>
756 </funcsynopsis>
757 Like the first JvNewStringLatin1, but computes <parameter>len</parameter>
758 using <literal>strlen</literal>.
759 </para>
761 <para>
762 <funcsynopsis>
763 <funcdef>jstring <function>JvNewStringUTF</function></funcdef>
764 <paramdef>const char *<parameter>bytes</parameter></paramdef>
765 </funcsynopsis>
766 Creates a new Java String object, where <parameter>bytes</parameter> are
767 the UTF-8 encoded characters of the string, terminated by a null byte.
768 </para>
770 <para>
771 <funcsynopsis>
772 <funcdef>jchar *<function>JvGetStringChars</function></funcdef>
773 <paramdef>jstring <parameter>str</parameter></paramdef>
774 </funcsynopsis>
775 Returns a pointer to the array of characters which make up a string.
776 </para>
778 <para>
779 <funcsynopsis>
780 <funcdef> int <function>JvGetStringUTFLength</function></funcdef>
781 <paramdef>jstring <parameter>str</parameter></paramdef>
782 </funcsynopsis>
783 Returns number of bytes required to encode contents
784 of <parameter>str</parameter> as UTF-8.
785 </para>
787 <para>
788 <funcsynopsis>
789 <funcdef> jsize <function>JvGetStringUTFRegion</function></funcdef>
790 <paramdef>jstring <parameter>str</parameter></paramdef>
791 <paramdef>jsize <parameter>start</parameter></paramdef>
792 <paramdef>jsize <parameter>len</parameter></paramdef>
793 <paramdef>char *<parameter>buf</parameter></paramdef>
794 </funcsynopsis>
795 This puts the UTF-8 encoding of a region of the
796 string <parameter>str</parameter> into
797 the buffer <parameter>buf</parameter>.
798 The region of the string to fetch is specifued by
799 <parameter>start</parameter> and <parameter>len</parameter>.
800 It is assumed that <parameter>buf</parameter> is big enough
801 to hold the result. Note
802 that <parameter>buf</parameter> is <emphasis>not</emphasis> null-terminated.
803 </para>
804 </sect1>
806 <sect1><title>Class Initialization</title>
807 <para>
808 Java requires that each class be automatically initialized at the time
809 of the first active use. Initializing a class involves
810 initializing the static fields, running code in class initializer
811 methods, and initializing base classes. There may also be
812 some implementation specific actions, such as allocating
813 <classname>String</classname> objects corresponding to string literals in
814 the code.</para>
815 <para>
816 The Gcj compiler inserts calls to <literal>JvInitClass</literal> (actually
817 <literal>_Jv_InitClass</literal>) at appropriate places to ensure that a
818 class is initialized when required. The C++ compiler does not
819 insert these calls automatically - it is the programmer's
820 responsibility to make sure classes are initialized. However,
821 this is fairly painless because of the conventions assumed by the Java
822 system.</para>
823 <para>
824 First, <literal>libgcj</literal> will make sure a class is initialized
825 before an instance of that object is created. This is one
826 of the responsibilities of the <literal>new</literal> operation. This is
827 taken care of both in Java code, and in C++ code. (When the G++
828 compiler sees a <literal>new</literal> of a Java class, it will call
829 a routine in <literal>libgcj</literal> to allocate the object, and that
830 routine will take care of initializing the class.) It follows that you can
831 access an instance field, or call an instance (non-static)
832 method and be safe in the knowledge that the class and all
833 of its base classes have been initialized.</para>
834 <para>
835 Invoking a static method is also safe. This is because the
836 Java compiler adds code to the start of a static method to make sure
837 the class is initialized. However, the C++ compiler does not
838 add this extra code. Hence, if you write a native static method
839 using CNI, you are responsible for calling <literal>JvInitClass</literal>
840 before doing anything else in the method (unless you are sure
841 it is safe to leave it out).</para>
842 <para>
843 Accessing a static field also requires the class of the
844 field to be initialized. The Java compiler will generate code
845 to call <literal>_Jv_InitClass</literal> before getting or setting the field.
846 However, the C++ compiler will not generate this extra code,
847 so it is your responsibility to make sure the class is
848 initialized before you access a static field.</para>
849 </sect1>
850 <sect1><title>Exception Handling</title>
851 <para>
852 While C++ and Java share a common exception handling framework,
853 things are not quite as integrated as we would like, yet.
854 The main issue is the incompatible exception <emphasis>values</emphasis>,
855 and that the <quote>run-time type information</quote> facilities of the
856 two languages are not integrated.</para>
857 <para>
858 Basically, this means that you cannot in C++ catch an exception
859 value (<classname>Throwable</classname>) thrown from Java code, nor
860 can you use <literal>throw</literal> on a Java exception value from C++ code,
861 and expect to be able to catch it in Java code.
862 We do intend to change this.</para>
863 <para>
864 You can throw a Java exception from C++ code by using
865 the <literal>JvThrow</literal> <acronym>CNI</acronym> function.
866 <funcsynopsis>
867 <funcdef>void <function>JvThrow</function></funcdef>
868 <paramdef>jobject <parameter>obj</parameter></paramdef>
869 </funcsynopsis>
870 Throws an exception <parameter>obj</parameter>, in a way compatible
871 with the Java exception-handling functions.
872 The class of <parameter>obj</parameter> must be a subclass of
873 <literal>Throwable</literal>.
874 </para>
875 <para>
876 Here is an example:
877 <programlisting>
878 if (i >= count)
879 JvThrow (new java::lang::IndexOutOfBoundsException());
880 </programlisting>
881 </para>
882 </sect1>
884 <sect1><title>Synchronization</title>
885 <para>
886 Each Java object has an implicit monitor.
887 The Java VM uses the instruction <literal>monitorenter</literal> to acquire
888 and lock a monitor, and <literal>monitorexit</literal> to release it.
889 The JNI has corresponding methods <literal>MonitorEnter</literal>
890 and <literal>MonitorExit</literal>. The corresponding CNI macros
891 are <literal>JvMonitorEnter</literal> and <literal>JvMonitorExit</literal>.
892 </para>
893 <para>
894 The Java source language does not provide direct access to these primitives.
895 Instead, there is a <literal>synchronized</literal> statement that does an
896 implicit <literal>monitorenter</literal> before entry to the block,
897 and does a <literal>monitorexit</literal> on exit from the block.
898 Note that the lock has to be released even the block is abnormally
899 terminated by an exception, which means there is an implicit
900 <literal>try</literal>-<literal>finally</literal>.
901 </para>
902 <para>
903 From C++, it makes sense to use a destructor to release a lock.
904 CNI defines the following utility class.
905 <programlisting>
906 class JvSynchronize() {
907 jobject obj;
908 JvSynchronize(jobject o) { obj = o; JvMonitorEnter(o); }
909 ~JvSynchronize() { JvMonitorExit(obj); }
911 </programlisting>
912 The equivalent of Java's:
913 <programlisting>
914 synchronized (OBJ) { CODE; }
915 </programlisting>
916 can be simply expressed:
917 <programlisting>
918 { JvSynchronize dummy(OBJ); CODE; }
919 </programlisting>
920 </para>
921 <para>
922 Java also has methods with the <literal>synchronized</literal> attribute.
923 This is equivalent to wrapping the entire method body in a
924 <literal>synchronized</literal> statement.
925 (Alternatively, an implementation could require the caller to do
926 the synchronization. This is not practical for a compiler, because
927 each virtual method call would have to test at run-time if
928 synchronization is needed.) Since in <literal>gcj</literal>
929 the <literal>synchronized</literal> attribute is handled by the
930 method implementation, it is up to the programmer
931 of a synchronized native method to handle the synchronization
932 (in the C++ implementation of the method).
933 In otherwords, you need to manually add <literal>JvSynchronize</literal>
934 in a <literal>native synchornized</literal> method.</para>
935 </sect1>
937 <sect1><title>Reflection</title>
938 <para>The types <literal>jfieldID</literal> and <literal>jmethodID</literal>
939 are as in JNI.</para>
940 <para>
941 The function <literal>JvFromReflectedField</literal>,
942 <literal>JvFromReflectedMethod</literal>,
943 <literal>JvToReflectedField</literal>, and
944 <literal>JvToFromReflectedMethod</literal> (as in Java 2 JNI)
945 will be added shortly, as will other functions corresponding to JNI.</para>
947 <sect1><title>Using gcjh</title>
948 <para>
949 The <command>gcjh</command> is used to generate C++ header files from
950 Java class files. By default, <command>gcjh</command> generates
951 a relatively straightforward C++ header file. However, there
952 are a few caveats to its use, and a few options which can be
953 used to change how it operates:
954 </para>
955 <variablelist>
956 <varlistentry>
957 <term><literal>--classpath</literal> <replaceable>path</replaceable></term>
958 <term><literal>--CLASSPATH</literal> <replaceable>path</replaceable></term>
959 <term><literal>-I</literal> <replaceable>dir</replaceable></term>
960 <listitem><para>
961 These options can be used to set the class path for gcjh.
962 Gcjh searches the class path the same way the compiler does;
963 these options have their familiar meanings.</para>
964 </listitem>
965 </varlistentry>
967 <varlistentry>
968 <term><literal>-d <replaceable>directory</replaceable></literal></term>
969 <listitem><para>
970 Puts the generated <literal>.h</literal> files
971 beneath <replaceable>directory</replaceable>.</para>
972 </listitem>
973 </varlistentry>
975 <varlistentry>
976 <term><literal>-o <replaceable>file</replaceable></literal></term>
977 <listitem><para>
978 Sets the name of the <literal>.h</literal> file to be generated.
979 By default the <literal>.h</literal> file is named after the class.
980 This option only really makes sense if just a single class file
981 is specified.</para>
982 </listitem>
983 </varlistentry>
985 <varlistentry>
986 <term><literal>--verbose</literal></term>
987 <listitem><para>
988 gcjh will print information to stderr as it works.</para>
989 </listitem>
990 </varlistentry>
992 <varlistentry>
993 <term><literal>-M</literal></term>
994 <term><literal>-MM</literal></term>
995 <term><literal>-MD</literal></term>
996 <term><literal>-MMD</literal></term>
997 <listitem><para>
998 These options can be used to generate dependency information
999 for the generated header file. They work the same way as the
1000 corresponding compiler options.</para>
1001 </listitem>
1002 </varlistentry>
1004 <varlistentry>
1005 <term><literal>-prepend <replaceable>text</replaceable></literal></term>
1006 <listitem><para>
1007 This causes the <replaceable>text</replaceable> to be put into the generated
1008 header just after class declarations (but before declaration
1009 of the current class). This option should be used with caution.</para>
1010 </listitem>
1011 </varlistentry>
1013 <varlistentry>
1014 <term><literal>-friend <replaceable>text</replaceable></literal></term>
1015 <listitem><para>
1016 This causes the <replaceable>text</replaceable> to be put into the class
1017 declaration after a <literal>friend</literal> keyword.
1018 This can be used to declare some
1019 other class or function to be a friend of this class.
1020 This option should be used with caution.</para>
1021 </listitem>
1022 </varlistentry>
1024 <varlistentry>
1025 <term><literal>-add <replaceable>text</replaceable></literal></term>
1026 <listitem><para>
1027 The <replaceable>text</replaceable> is inserted into the class declaration.
1028 This option should be used with caution.</para>
1029 </listitem>
1030 </varlistentry>
1032 <varlistentry>
1033 <term><literal>-append <replaceable>text</replaceable></literal></term>
1034 <listitem><para>
1035 The <replaceable>text</replaceable> is inserted into the header file
1036 after the class declaration. One use for this is to generate
1037 inline functions. This option should be used with caution.
1038 </listitem>
1039 </varlistentry>
1040 </variablelist>
1041 <para>
1042 All other options not beginning with a <literal>-</literal> are treated
1043 as the names of classes for which headers should be generated.</para>
1044 <para>
1045 gcjh will generate all the required namespace declarations and
1046 <literal>#include</literal>'s for the header file.
1047 In some situations, gcjh will generate simple inline member functions.</para>
1048 <para>
1049 There are a few cases where gcjh will fail to work properly:</para>
1050 <para>
1051 gcjh assumes that all the methods and fields of a class have ASCII
1052 names. The C++ compiler cannot correctly handle non-ASCII
1053 identifiers. gcjh does not currently diagnose this problem.</para>
1054 <para>
1055 gcjh also cannot fully handle classes where a field and a method have
1056 the same name. If the field is static, an error will result.
1057 Otherwise, the field will be renamed in the generated header; `__'
1058 will be appended to the field name.</para>
1059 <para>
1060 Eventually we hope to change the C++ compiler so that these
1061 restrictions can be lifted.</para>
1062 </sect1>
1064 </article>