Contents | Prev | Next | Index


Direct Java Class Imports

With Canterbury Pascal for JVM 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 Pascal. With conventional programming language tools it used to be necessary to write some kind of interface units describing the foreign APIs. Not so with Canterbury Pascal for JVM. This compiler directly generates an internal interface unit. That one then contains the foreign Java class type and any additional types as needed for its class members.

Here is how the direct import finds a foreign Java class:

If the identifier in a USES-clause does not refer to an existing Pascal unit 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 Pascal interface unit is then internally generated and contains the foreign Java class declaration in a suitable Pascal notation.

Example of a unit-qualified access to a foreign Java class:

To import the Java class java.io.RandomAccessFile:

USES java_io_RandomAccessFile;

The class type can then be accessed as:

java_io_RandomAccessFile.RandomAccessFile

It can also be referenced using an unqualified identifier:

RandomAccessFile

Supporting types, as needed for the members of the foreign Java class, are accessable from the same imported unit, 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 Pascal-style methods. E.g. method java.io.RandomAccessFile.read becomes a method in unit java_io_RandomAccessFile as follows:

PROCEDURE RandomAccessFile.read
( ) : LONGINT;

A constructor is always regarded as a Pascal constructor method of name _init_. E.g. the default constructor for java.io.RandomAccessFile looks like this in unit java_io_RandomAccessFile:

CONSTRUCTOR RandomAccessFile._init_ ;

A Java array is seen by Pascal 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 unit java_io_RandomAccessFile:

TYPE
  POINTER_TO_ARRAY_OF_SHORTINT
=
  ^ARRAY OF SHORTINT;

PROCEDURE 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 unit java_util_Vector:

TYPE
  POINTER_TO_ARRAY_OF_Object =

  ^ARRAY OF java_lang_Object.Object;

PROCEDURE Vector.copyInto
( anArray : POINTER_TO_ARRAY_Object );

Basic Java types from foreign imports are always mapped to the corresponding Pascal counterparts:

Java type Pascal type
char CHAR
boolean BOOLEAN
byte SHORTINT
short INTEGER
int LONGINT
long SYSTEM.HUGEINT
float SINGLE
double DOUBLE


Contents | Prev | Next | Index

Canterbury Pascal for JVM  (Last documentation update Sep 02, 2004)
Copyright © 1999-2004 J.Neuhoff - mhccorp.com  . All rights reserved.
Please send any comments or corrections to neuhoff@mhccorp.com