source: trunk/source/examples/jfli/examples/session.lisp @ 11435

Last change on this file since 11435 was 11435, checked in by gb, 12 years ago

Rich Hickey's 'jfli' (Lisp<->Java) bridge, as of jfli091104.zip.
The file 'jni.lisp' is LispWorks?-specific FFI code; there'll need
to be something CCL-specific that exports the same interface.

Text files in the .zip were CRLF-terminated; hopefully, svn will
fix that ...

File size: 3.2 KB
Line 
1;this presumes you have already compiled jni.lisp and jfli.lisp into fasl files
2
3CL-USER 4 > (load "/lisp/jni")
4; Loading fasl file C:\lisp\jni.fsl
5#P"C:/lisp/jni.fsl"
6
7CL-USER 5 > (load "/lisp/jfli")
8; Loading fasl file C:\lisp\jfli.fsl
9#P"C:/lisp/jfli.fsl"
10
11;The user API is entirely in the jfli package
12CL-USER 6 > (use-package :jfli)
13T
14
15;tell the library where Java is located
16CL-USER 7 > (setf *jni-lib-path* "/j2sdk1.4.2_01/jre/bin/client/jvm.dll")
17"/j2sdk1.4.2_01/jre/bin/client/jvm.dll"
18
19;this starts the VM - note how you can set the classpath
20CL-USER 8 > (create-jvm "-Djava.class.path=/lisp/jfli.jar")
210
22#<Pointer: JNI:PVM = #x081022A0>
23#<Pointer: JNI:PENV = #x0086A858>
24
25;define wrappers for the members of Object
26CL-USER 9 > (def-java-class "java.lang.Object")
27NIL
28
29;and of Properties, a Hashtable-like class
30CL-USER 10 > (def-java-class "java.util.Properties")
31#<STANDARD-CLASS |java.util|:PROPERTIES. 2066B964>
32
33;the above will create these packages if they do not already exist
34;use the packages for easy name access
35
36CL-USER 11 > (use-package "java.lang")
37T
38
39CL-USER 12 > (use-package "java.util")
40T
41
42;create a Properties instance, note keyword-style member inits, string conversion etc
43;also note typed return value
44CL-USER 13 > (setf p (new properties. :getproperty "fred" "ethel"))
45#<PROPERTIES. 20664A94>
46
47;virtual functions work as normal
48CL-USER 14 > (object.tostring p)
49"{fred=ethel}"
50
51;setter was generated for member function because it follows the JavaBeans property protocol
52CL-USER 15 > (setf (properties.getproperty p "ricky") "lucy")
53"lucy"
54
55CL-USER 16 > (object.tostring p)
56"{ricky=lucy, fred=ethel}"
57
58CL-USER 17 > (properties.size p)
592
60
61;totally dynamic access, create wrappers as you need
62CL-USER 18 > (def-java-class "java.lang.Class")
63#<STANDARD-CLASS CLASS. 20680EC4>
64
65CL-USER 19 > (class.getname (object.getclass p))
66"java.util.Properties"
67
68CL-USER 20 > (def-java-class "java.util.Enumeration")
69#<STANDARD-CLASS ENUMERATION. 20669274>
70
71;no need to wait for the vendor to enhance the language - you use Lisp!
72CL-USER 21 > (defmacro doenum ((e enum) &body body)
73  (let ((genum (gensym)))
74    `(let ((,genum ,enum))
75       (do ()
76           ((not (enumeration.hasmoreelements ,genum)))
77         (let ((,e (enumeration.nextelement ,genum)))
78           ,@body)))))(defmacro doenum ((e enum) &body body)
79               (let ((genum (gensym)))
80                 `(let ((,genum ,enum))
81                    (do ()
82                        ((not (enumeration.hasmoreelements ,genum)))
83                      (let ((,e (enumeration.nextelement ,genum)))
84                        ,@body)))))
85DOENUM
86
87;can't do this in Java yet can in Lisp
88CL-USER 22 > (doenum (prop (properties.elements p)) (print (object.tostring prop)))
89
90"lucy" 
91"ethel" 
92NIL
93
94;doc strings are created giving original Java signatures and indicating overloads
95CL-USER 23 > (documentation 'properties.getproperty 'function)
96"java.lang.String getProperty(java.lang.String,java.lang.String)
97java.lang.String getProperty(java.lang.String)
98"
99
100CL-USER 24 > (documentation 'properties.new 'function)
101"java.util.Properties()
102java.util.Properties(java.util.Properties)
103"
104
Note: See TracBrowser for help on using the repository browser.