Interfacing Scheme and Java


The scheme package provides a set of functions and objects to allows the user to access Java objects. For this purpose a new Scheme type has been added: the java-objects. These features are only available if the current kernel is an instance of scheme.extensions.ScmExtendedKernel.

1 - Predefined Java-objects.

There are two predefined java-objects:
 
*java-null* contains the null value
*java-kernel* contains the kernel (an instance of ScmKernel) itself

2 - Predefined functions.

Here is the list of all the predefined functions which deal with java-object:
 
(boolean->java-boolean b) Creates a new java-object which contains a java.lang.Boolean object initialized with the value of b 
Returns the created object
(char->java-char c) Creates a new java-object which contains a java.lang.Character object initialized with the value of c 
Returns the created object
(integer->java-byte i) Creates a new java-object which contains a java.lang.Byte object initialized with the value of i 
Returns the created object
(integer->java-int i) Creates a new java-object which contains a java.lang.Integer object initialized with the value of i 
Returns the created object
(integer->java-long i) Creates a new java-object which contains a java.lang.Long object initialized with the value of i 
Returns the created object
(integer->java-short i) Creates a new java-object which contains a java.lang.Short object initialized with the value of i 
Returns the created object
(java-boolean->boolean j) Returns #t or #f depending on the value of j 
(java-byte->integer j) Returns an integer initialized with the value of j
(java-char->char j) Returns a character initialized with the value of j
(java-class string) Loads a java class from its full name (ex: (java-class "java.lang.Integer")
returns a java-object which represents the loaded class
(java-constructor j . L) Loads a constructor of the given class 'j' and the classes of its parameters. All parameters of this function must be java-object containing a Java class. Theses objects can be obtained by a call to java-class 
Returns the constructor encapsulated in a java-object.
(java-double->real j) Returns a real initialized with the value of j
(java-field j string) Loads a field (java.lang.reflect.Field) of the given name (string) and from the class 'j'. (ex: (java-field (java-class "java.lang.Integer") "TYPE")
Returns the field encapsulated in a java-object.
(java-field-get jf jo) The arguments must be java-objects. The first must contains a java.lang.reflect.Field and the second must contains a valid object for this field or be *java-null* if the field is static. 
Returns the value of the field 'jf'
(java-field-set jf jo j) Sets the value value of the field 'jf'. The arguments must be java-objects. The first must contains a java.lang.reflect.Field, the second must contains a valid object for this field or be *java-null* and the third must contains a valid value to set to the field. 
Returns an #[undefined] value
(java-float->real j) Returns a real initialized with the value of j
(java-int->integer j) Returns an integer initialized with the value of j
(java-long->integer j) Returns an integer initialized with the value of j
(java-method j string . L) Loads a method (java.lang.reflect.Method) of the given name (string), from the class 'j' and with parameters of the classes L. 
Returns the method encapsulated in a java-object.
(java-method-invoke jm jo . L) Invokes the method 'jm' with this set to 'jo' and with the list of parameters 'L'
Returns the result in a java-object
(java-new-instance jc . L) Create a new instance of the class which owns 'jc'. 'L' is the list of parameters passed to the constructor. 
Returns the object in a java-object
(java-object? obj) Returns #t if the given object is a java-object, #f otherwise
(java-object obj) Returns the given object encapulated in a java-object
(java-short->integer j) Returns an integer initialized with the value of j
(java-string->string j) Returns a string initialized with the value of j
(real->java-double r) Creates a new java-object which contains a java.lang.Double object initialized with the value of r 
Returns the created object
(real->java-float r) Creates a new java-object which contains a java.lang.Float object initialized with the value of r 
returns the created object
(string->java-string str) Creates a new java-object which contains a java.lang.String object initialized with the value of str 
Returns the created object

3 - Example.

This example is a demonstration of how to use of the previously described functions. It shows how to create and display a Frame that contains a Button. When we click on this button it calls a given procedure.

;; First we load the needed classes

(define jframe  (java-class "java.awt.Frame"))
(define jstring (java-class "java.lang.String"))

;; Now we load the primitive type 'int'

(define jinteger  (java-class "java.lang.Integer"))
(define int.type  (java-field jinteger "TYPE"))
(define jint      (java-field-get int.type *java-null*))

;; We load Frame's constructor and methods

(define frame-cons (java-constructor jframe jstring))
(define frame-show (java-method jframe "show"))
(define frame-set-size (java-method jframe "setSize" jint jint))

;; We can now create an instance of Frame

(define the-frame
  (java-new-instance
    frame-cons
    (string->java-string "A Frame created by Scheme")))

;; It is time to resize and show the frame

(java-method-invoke
  frame-set-size the-frame
                 (integer->java-int 300)
                 (integer->java-int 400))

(java-method-invoke frame-show the-frame)
 

4 - The same example in an object oriented way.

As you can see the functions described above are not very easy to use... But if you use the object oriented library given with the scheme package the program becomes:

(require 'jframe)

(define the-frame
  (<jframe> create (<jstring> create "A Frame created by Scheme")))

(the-frame set-size! (<jint> create 300)
                     (<jint> create 400))

(the-frame show)

NOTES:
- The 'create' method is a library defined method of the class <class>. It creates an object by invoking 'make' and calls the 'init' method of this object (all library classes must redefine 'init') with the given parameters.
- If you copy this example in a file named 'example.scm' you can run it by typing the command:

'java ScmLoader library/library.scm example.scm'


 Stéphane Hillion - 1998