www.delorie.com/gnu/docs/kawa/kawa_64.html   search  
 
Buy GNU books!


Kawa, the Java-based Scheme system

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.4 Calling Java methods from Scheme

Kawa has both a low-level and a high-level "Foreign Function Interface", which allows you to call any (virtual or static) Java method as if it were a Scheme procedure.

Function: invoke-static class name args ...
The class can be a <java.lang.Class>, a <gnu.bytecode.ClassType>, or a <symbol> or <string> that names a Java class. The name can be <symbol> or <string> that names one or more methods in the Java class. The name is "mangled" (see section 10.2 Mapping Scheme names to Java names) into a valid Java name.

Any public methods (static or instance) in the specified class (or its super-classes) that match "name" or "name$V" collectively form a generic procedure. When the procedure is applied to the argument list, the most specific applicable method is chosen depending on the argument list; that method is then called with the given arguments. Iff the method is an instance method, the first actual argument is used as the this argument. If there are no applicable methods (or no methods at all!), or there is no "best" method, WrongType is thrown.

("name$V" is used for procedures with #!rest or keyword args; the last argument must be an array type; all the "extra" arguments must be compatible with the type of the array elements.)

An example (derived from the Skij FAQ):
 
(invoke-static <java.lang.Thread> 'sleep 100)

The behavior of interpreted code and compiled code is not indentical, though you should get the same result either way unless you have designed the classes rather strangely. The details will be nailed down later, but the basic idea is that the compiler will "inline" the invoke-static call if it can pick a single "best" matching method.

Function: invoke object name args ...
The name can be <symbol> or <string> that names one or more methods in the Java class. The name is "mangled" (see section 10.2 Mapping Scheme names to Java names) into a valid Java name.

Any public methods (static or instance) in the specified class (or its super-classes) that match "name" or "name$V" collectively form a generic procedure. When the procedure is applied to the argument list, the most specific applicable method is chosen depending on the argument list; that method is then called with the given arguments. Iff the method is an instance method, the object is used as the this argument; otherwise object is prepended to the args list. If there are no applicable methods (or no methods at all!), or there is no "best" method, WrongType is thrown.

("name$V" is used for procedures with #!rest or keyword args; the last argument must be an array type; all the "extra" arguments must be compatible with the type of the array elements.)

The behavior of interpreted code and compiled code is not indentical, though you should get the same result either way unless you have designed the classes rather strangely. The details will be nailed down later, but the basic idea is that the compiler will "inline" the invoke-static call if it can pick a single "best" matching method.

If the compiler cannot determine the method to call (assuming the method name is constant), the compiler has to generate code at run-time to find the correct method. This is much slower, so the compiler will print a warning. To avoid a waning, you can use a type declaration, or insert a cast:
 
(invoke (as <java.util.Date> my-date) 'setDate cur-date)

Function: class-methods class name
Return a generic function containing those methods of class that match the name name, in the sense of invoke-static. Same as:
 
(lambda args (apply invoke-static (cons class (cons name args))))

Some examples using these functions are `vectors.scm' and `characters.scm' the directory `kawa/lib' in the Kawa sources.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003