All Manuals > LispWorks User Guide and Reference Manual > 42 Java classes and methods



Java class

public class com.lispworks.LispCalls implements InvocationHandler

The Java class com.lispworks.LispCalls defines methods for calling from Java to Lisp.

com.lispworks.LispCalls is part of the LispWorks distribution. For Android it is part of the 7-0-0-0/etc/lispworks.jar file. See the Android interface for details. On other platforms it is defined in the JAR file lispcalls.jar which is part of the LispWorks distribution in the etc directory, that is (lispworks-file "etc/lispcalls.jar"). This JAR file needs to be on the classpath (for example by the keyword argument :java-class-path to init-java-interface).










public static int callIntV (String name, Object... args)

public static int callIntA (String name, Object[] args)

public static double callDoubleV (String name, Object... args)

public static double callDoubleA (String name, Object[] args)

public static Object callObjectV (String name, Object... args)

public static Object callObjectA (String name, Object[] args)

public static void callVoidV (String name, Object... args)

public static void callVoidA (String name, Object[] args)


In the method name, the type specifies the return type, and V or A specifies whether the arguments are supplied as Variable arguments or Array. Otherwise the pairs of V and A methods behave the same.

The name argument is a string specifying a Lisp symbol. The name is parsed by a simple parser as described for com.lispworks.LispCalls.checkLispSymbol (with fboundp = true).

If the symbol is not found or is not fbound, these methods throw a RuntimeException with a string giving the reason for failure.

If the symbol is found, it is applied to the arguments args. For each argument, if it is a primitive type or of a class corresponding to a primitive type or a string, it is converted to the corresponding Lisp value. Otherwise it is passed as a jobject. See Types and conversion between Lisp and Java. The result of the call is converted to the return type of the method and returned from the method. The conversion of the result type allows any float to be returned as a double, but does not coerce between integers and floats. For the Object return value, the result must be either a Java object (jobject or an instance of standard-java-object), or a Lisp object that can be converted to a Java object. See Types and conversion between Lisp and Java.

The Lisp function is an ordinary Lisp function, but it needs to return the right value. Unless the call is using the Void callers (com.lispworks.LispCalls.callVoidA or com.lispworks.LispCalls.callVoidV), returning the wrong value will call the java-to-lisp-debugger-hook (see init-java-interface) with an appropriate condition, and then return zero of the correct type (that is 0, 0d0 or Java null) from the call.

The call to the Lisp function is wrapped such that trying to throw out of it does not actually finish the throw, and instead returns zero of the correct type from the call. It is also wrapped by a debugger hook, which is invoked if the code tries to enter the debugger (normally as a result of an unhandled error, but could be any call to cl:invoke-debugger). The hook calls the java-to-lisp-debugger-hook (see init-java-interface) with the condition, and then calls cl:abort. If there is no cl:abort restart inside the Lisp function that catches this abort, this causes returning a zero of the correct type.

An important issue to remember is that when delivering with shaking, LispWorks eliminates symbols for which there is no reference. If the only call to a Lisp symbol foo is from Java, LispWorks will not see the reference and it will eliminate foo. To guard against this, you can either pass foo in a list to the deliver keyword :keep-symbols, or more conveniently, use the function hcl:deliver-keep-symbols (see the LispWorks Delivery User Guide ), for example:

(defun function-called-from-java (arg1 arg2)
(deliver-keep-symbols 'function-called-from-java)

int sum = com.lispworks.LispCalls.callIntV ("+", 2, 3, 10);
=> sum = 15

int position = com.lispworks.LispCalls.callIntV ("search", "r", "international");
=> position = 4

double logThree = com.lispworks.LispCalls.callDoubleV("log", 3);
=> logThree = 1.0986123



public static native Object createLispProxy(String name)


Creates a Lisp proxy, which is a Java proxy which calls Lisp functions.

name specifies a symbol which is the name of a proxy definition, defined in Lisp by either define-lisp-proxy or setup-lisp-proxy. The name is parsed by a simple parser as described for com.lispworks.LispCalls.checkLispSymbol (with fboundp = false).

Once it found the symbol, it makes a proxy the same way that calling make-lisp-proxy with name would, and returns it. The result is an Object that implements all the interfaces that are defined in the proxy definition, and when the methods of these interfaces are called on the object it calls into Lisp. See define-lisp-proxy for details.

If createLispProxy is successful it returns the proxy object. If there is any problem, this will cause a call to cl:error. If the cl:error call is not handled, the java-to-lisp-debugger-hook (see init-java-interface) is called with the condition, and then null is returned from createLispProxy. If the error is handled and tries to throw out of the context of the Lisp side of createLispProxy, the throw is blocked and createLispProxy returns null.



public static boolean checkLispSymbol(String name, boolean fboundp)


Checks whether a Lisp symbol exists, and optionally whether it is fbound.

name specifies the name of the Lisp symbol. The string name is parsed in a simple way, rather than using the Lisp reader. The parsing involves:

  1. Upcase the string.
  2. If there is a colon, take the part before it as a package name. Otherwise use "COMMON-LISP-USER" as the package name.
  3. If the colon is followed by another colon, skip it and set a flag allowing internals. Otherwise, set a flag allowing only externals.
  4. Take the rest of the string as the symbol name.
  5. Find the package from the package name.
  6. Find the symbol using the package and the symbol name. If it is internal, use it only if the flag allowing internal was set.
  7. If fboundp is true, check whether the symbol is fbound.

If all these steps succeed, checkLispSymbol returns true. Otherwise it returns false.

For symbols with names that do not need escaping, the result is the same normal processing by the Lisp reader without interning when there is no symbol.

checkLispSymbol caches the results in the Java side, which means that if the symbol appears or gets defined after the first call to checkLispSymbol it may return the wrong result.

See also

Calling from Java to Lisp


LispWorks User Guide and Reference Manual - 13 Feb 2015