1 | JSS |
---|
2 | === |
---|
3 | |
---|
4 | Created by Alan Ruttenberg |
---|
5 | |
---|
6 | |
---|
7 | JSS stands for either "Java Simple Syntax" or "Java Syntax Sucks", |
---|
8 | depending on your mood. |
---|
9 | |
---|
10 | The dynamic dispatch of the java.lang.reflect package is used to make |
---|
11 | it real easy, if perhaps less efficient, to write Java code since you |
---|
12 | don't need to be bothered with imports, or with figuring out which |
---|
13 | method to call. The only time that you need to know a class name is |
---|
14 | when you want to call a static method, or a constructor, and in those |
---|
15 | cases, you only need to know enough of the class name that is unique |
---|
16 | wrt to the classes on your classpath. |
---|
17 | |
---|
18 | Java methods look like this: #"toString". Java classes are represented |
---|
19 | as symbols, which are resolved to the appropriate java class |
---|
20 | name. When ambiguous, you need to be more specific. A simple example: |
---|
21 | |
---|
22 | (let ((sw (new 'StringWriter))) |
---|
23 | (#"write" sw "Hello ") |
---|
24 | (#"write" sw "World") |
---|
25 | (print (#"toString" sw))) |
---|
26 | |
---|
27 | What's happened here? First, all the classes in all the jars in the |
---|
28 | classpath have been collected. For each class a.b.C.d, we have |
---|
29 | recorded that b.c.d, b.C.d, C.d, c.d, and d potentially refer to this |
---|
30 | class. In your call to new, as long as the symbol can refer to only |
---|
31 | one class, we use that class. In this case, it is |
---|
32 | java.io.StringWriter. You could also have written |
---|
33 | |
---|
34 | (new 'io.stringwriter) |
---|
35 | |
---|
36 | or |
---|
37 | (new '|io.StringWriter|) |
---|
38 | |
---|
39 | or |
---|
40 | (new 'java.io.StringWriter) |
---|
41 | |
---|
42 | The call |
---|
43 | |
---|
44 | (#"write" sw "Hello ") |
---|
45 | |
---|
46 | uses the code in invoke.java to call the method named "write" with |
---|
47 | the arguments sw and "Hello ". JSS figures out the right java method |
---|
48 | to call, and calls it. |
---|
49 | |
---|
50 | Static calls are possible as well with the #" macro, but the |
---|
51 | first argument MUST BE A SYMBOL to distinguish |
---|
52 | |
---|
53 | (#"getProperties" "java.lang.System") |
---|
54 | |
---|
55 | from |
---|
56 | |
---|
57 | (#"getProperties" 'java.lang.System) |
---|
58 | |
---|
59 | The first attempts to call a method on the java.lang.String object |
---|
60 | with the contents "java.lang.System", which results in an error, while |
---|
61 | the second invokes the static java.lang.System.getProperties() method. |
---|
62 | |
---|
63 | If you want to do a raw java call, use #0"toString". Raw calls |
---|
64 | return their results as Java objects, avoiding doing the usual Java |
---|
65 | object to Lisp object conversions that ABCL does. |
---|
66 | |
---|
67 | |
---|
68 | (with-constant-signature ((name jname raw?)*) &body body) |
---|
69 | |
---|
70 | binds a macro which expands to a jcall, promising that the same method |
---|
71 | will be called every time. Use this if you are making a lot of calls and |
---|
72 | want to avoid the overhead of a the dynamic dispatch. |
---|
73 | e.g. |
---|
74 | |
---|
75 | (with-constant-signature ((tostring "toString")) |
---|
76 | (time (dotimes (i 10000) (tostring "foo")))) |
---|
77 | |
---|
78 | runs about three times faster than |
---|
79 | |
---|
80 | (time (dotimes (i 10000) (#"toString" "foo"))) |
---|
81 | |
---|
82 | |
---|
83 | (with-constant-signature ((tostring "toString" t)) ...) |
---|
84 | |
---|
85 | will cause the toString to be a raw java call. See |
---|
86 | JSS::GET-ALL-JAR-CLASSNAMES for an example. |
---|
87 | |
---|
88 | Implementation is that the first time the function is called, the |
---|
89 | method is looked up based on the arguments passed, and thereafter |
---|
90 | that method is called directly. Doesn't work for static methods at |
---|
91 | the moment (lazy) |
---|
92 | |
---|
93 | (japropos string) finds all class names matching string |
---|
94 | |
---|
95 | (jcmn class-name) lists the names of all methods for the class |
---|
96 | |
---|
97 | |
---|
98 | Compatibility |
---|
99 | ------------- |
---|
100 | |
---|
101 | The function ENSURE-COMPATIBILITY attempts to provide a compatibility |
---|
102 | mode to existing users of JSS by importing the necessary symbols into |
---|
103 | CL-USER. |
---|
104 | |
---|
105 | Some notes on other compatibilty issues: |
---|
106 | |
---|
107 | *classpath-manager* |
---|
108 | |
---|
109 | Since we are no longer using Beanshell, this is no longer present. |
---|
110 | For obtaining the current classloader use JAVA:*CLASSLOADER*. |
---|
111 | |
---|