A class for jobject.
The class standard-java-object
is a class for jobject.
Instances of standard-java-object
can be passed to the Java interface functions and callers you define, and returned from Java calls whenever a jobject is needed. Each instance is normally associated with a jobject, which is used by the Java interface.
Apart from accessing the jobject in the instance, the Java interface does not do anything with the class, and makes no assumptions about it. There is no need for the class hierarchy in Lisp to reflect the class hierarchy in Java.
You can define your own classes that inherit from standard-java-object
as well as other classes using standard defclass. Alternatively, you can tell the importing interface to define classes. There is no obvious advantage for using the latter.
The jobject slot defaults to nil
, which the Java interface interprets as an invalid value. Your code needs to do something to set it. One option is to set it explicitly using the accessor java-instance-jobject
. When you do that, the object must be a jobject, but the interface does not put any other restrictions. As long as it fits with the logic of your program, an instance of any Lisp class can hold a jobject of any Java class.
The other way to set the jobject slot is to use one of the interface functions that does it implicitly. This include the functions make-java-instance, create-instance-jobject and create-instance-jobject-list, and the :construct
keyword argument to make-instance.
The :construct
keyword is processed by an :after
method of cl:initialize-instance
on standard-java-object
. When :construct
is supplied, it needs to be either a list (possibly nil
) of arguments for the constructor, or t
, which means use default-constructor-arguments to get the argument list. The method then calls create-instance-jobject-list with the instance and the arguments to set create and set the jobject.
Additionally, the instance that is returned by create-instance-from-jobject also has the jobject.
When you pass :construct t
, the call to default-constructor-arguments happens inside cl:initialize-instance
, before all the cl:initialize-instance
methods were called (the actual order of calls is the standard order). That means that if default-constructor-arguments depends on some values in the instance that may be set by an cl:initialize-instance
method of another class, it may not work properly. You can avoid this problem by not passing the keyword :construct
and instead using create-instance-jobject-list on the result of make-instance.
The interface for setting the jobject implicitly requires an association from the CLOS class name to the constructor, by using define-java-constructor or setup-java-constructor with the class-symbol argument.
create-instance-from-jobject requires an association from the Java class name to the CLOS class name, which is created by record-java-class-lisp-symbol.
LispWorks User Guide and Reference Manual - 13 Feb 2015