Contents | Prev | Next | Index
With Canterbury Oberon-2 for Java it is possible to directly import any existing foreign Java classes. A direct Java class import enables the programmer to gain access to a foreign Java class which was not originally created by Canterbury Oberon-2. With conventional programming language tools it used to be necessary to write some kind of interface modules describing the foreign APIs. Not so with Canterbury Oberon-2 for Java. This compiler directly generates an internal module. That one contains the foreign Java class reference type, its record type, and any additional types as needed for its class members.
Here is how the direct import finds a foreign Java class:
If the import identifier does not refer to an existing Oberon-2 module name, the compiler attempts to find a foreign Java class. It first replaces the "_" characters in the import identifier with "." characters and then calls java.lang.Class.forName with the new identifier for finding and loading the foreign class. The missing Oberon-2 module is then internally generated and contains the foreign Java class declaration in a suitable Oberon-2 notation.
Example of a module-qualified access to a foreign Java class:
To import the Java class java.io.RandomAccessFile:
IMPORT java_io_RandomAccessFile;
The class reference type can then be accessed as:
java_io_RandomAccessFile.POINTER_TO_RandomAccessFile
And the corresponding class record is accessed as:
java_io_RandomAccessFile.RandomAccessFile
Supporting types, as needed for the members of the foreign Java class, are accessable with the same module qualifier, too. For example, the Java type byte[] is needed by java.io.RandomAccessFile for some of its members and can be accessed as:
java_io_RandomAccessFile.POINTER_TO_ARRAY_OF_SHORTINT
Additional types for direct Java class imports:
Methods are regarded as type-bound procedures. E.g. method RandomAccessFile.read becomes a procedure in module java_io_RandomAccessFile as follows:
PROCEDURE( Self : POINTER_TO_RandomAccessFile ) read*
( ) : LONGINT;
A constructor is always regarded as a type-bound procedure of name _init_. E.g. the default constructor for java.io.RandomAccessFile looks like this in module java_IO_RandomAccessFile:
(*$JAVA CONSTRUCTOR *)
PROCEDURE( Self: POINTER_TO_RandomAccessFile ) _init_* ();
A Java array is seen by Oberon-2 as a pointer to an open array. E.g., method java.io.RandomAccessFile.readFully contains a formal parameter of Java type byte[], as follows, in module java_io_RandomAccessFile:
TYPE POINTER_TO_ARRAY_OF_SHORTINT* =
POINTER TO ARRAY OF SHORTINT;PROCEDURE( Self: POINTER_TO_RandomAccessFile) readFully*
( b : POINTER_TO_ARRAY_OF_SHORTINT );
A Java array of class elements is seen as a pointer to open array of class references. E.g. method java.util.Vector.copyInto has a formal parameter which is of Java type java.lang.Object[], and looks like this in module java_util_Vector:
TYPE POINTER_TO_ARRAY_OF_POINTER_TO_Object* =
POINTER TO ARRAY OF java_lang_Object.POINTER_TO_Object;PROCEDURE( Self: POINTER_TO_Vector ) copyInto*
( anArray : POINTER_TO_ARRAY_OF_POINTER_TO_Object );
Basic Java types from foreign imports are always mapped to the corresponding Oberon-2 counterparts:
Java type Oberon-2 type char CHAR boolean BOOLEAN byte SHORTINT short INTEGER int LONGINT long SYSTEM.HUGEINT float REAL double LONGREAL
Contents | Prev | Next | Index
Canterbury Oberon-2 for Java (Last documentation update
Feb 8, 2000)
Copyright © 1998 Mill Hill &
Canterbury Corporation, Ltd. All rights reserved
Please send any comments or corrections to
mhc@webcom.com