26
|
1 |
Object subclass:#JavaObject
|
|
2 |
instanceVariableNames:''
|
|
3 |
classVariableNames:''
|
|
4 |
poolDictionaries:''
|
|
5 |
category:'Java-Classes'
|
|
6 |
!
|
|
7 |
|
|
8 |
|
69
|
9 |
!JavaObject class methodsFor:'misc'!
|
29
|
10 |
|
|
11 |
resolveClassRefs
|
54
|
12 |
!
|
|
13 |
|
|
14 |
resolveClassRefsIgnoring:setOfClasses
|
29
|
15 |
! !
|
|
16 |
|
69
|
17 |
!JavaObject class methodsFor:'smalltalk interface'!
|
29
|
18 |
|
|
19 |
convertJavaObject:val signature:retValSignature
|
91
|
20 |
^ val.
|
29
|
21 |
retValSignature = 'void' ifTrue:[
|
|
22 |
^ #void
|
|
23 |
].
|
|
24 |
retValSignature = 'boolean' ifTrue:[
|
|
25 |
val == 0 ifTrue:[^ false].
|
|
26 |
^ true
|
|
27 |
].
|
|
28 |
retValSignature = 'int' ifTrue:[
|
|
29 |
val isInteger ifFalse:[
|
|
30 |
self halt
|
|
31 |
].
|
|
32 |
^ val
|
|
33 |
].
|
34
|
34 |
retValSignature = 'char[]' ifTrue:[
|
|
35 |
"/ these are ST-strings
|
|
36 |
^ val
|
|
37 |
].
|
|
38 |
|
|
39 |
retValSignature = 'char' ifTrue:[
|
|
40 |
"/ these are ST-characters
|
40
|
41 |
val isInteger ifTrue:[
|
|
42 |
^ Character value:val
|
|
43 |
].
|
|
44 |
self halt.
|
34
|
45 |
^ val
|
|
46 |
].
|
|
47 |
|
29
|
48 |
retValSignature = 'Object' ifTrue:[
|
|
49 |
^ val
|
|
50 |
].
|
|
51 |
|
44
|
52 |
retValSignature = 'String' ifTrue:[
|
|
53 |
^ self stringFromJavaString:val
|
|
54 |
].
|
|
55 |
|
29
|
56 |
'no conversion for: ' print. val class name print. ' to: ' print. retValSignature printNL.
|
|
57 |
^ val.
|
43
|
58 |
|
|
59 |
|
29
|
60 |
!
|
|
61 |
|
43
|
62 |
javaStringFrom:aString
|
|
63 |
"hard-coding internas of java.lang.String here is bad ..."
|
|
64 |
|
|
65 |
|s|
|
|
66 |
|
91
|
67 |
s := Java javaString basicNew.
|
43
|
68 |
s instVarNamed:'value' put: aString.
|
|
69 |
s instVarNamed:'offset' put: 0.
|
|
70 |
s instVarNamed:'count' put: aString size.
|
|
71 |
^ s
|
44
|
72 |
!
|
|
73 |
|
|
74 |
stringFromJavaString:aJavaString
|
|
75 |
"hard-coding internas of java.lang.String here is bad ..."
|
|
76 |
|
|
77 |
(aJavaString instVarNamed:'offset') ~~ 0 ifTrue:[self halt].
|
|
78 |
|
|
79 |
^ ((aJavaString instVarNamed:'value')
|
|
80 |
copyTo:(aJavaString instVarNamed:'count')) asString
|
43
|
81 |
! !
|
|
82 |
|
|
83 |
!JavaObject methodsFor:'initialization'!
|
|
84 |
|
52
|
85 |
initializeFields:initialValues
|
|
86 |
|sz|
|
|
87 |
|
|
88 |
sz := self class instSize.
|
|
89 |
1 to:sz do:[:i |
|
|
90 |
self instVarAt:i put:(initialValues at:i)
|
|
91 |
]
|
|
92 |
!
|
|
93 |
|
43
|
94 |
initializeToZero
|
52
|
95 |
|sz|
|
|
96 |
|
|
97 |
sz := self class instSize.
|
|
98 |
1 to:sz do:[:i |
|
43
|
99 |
self instVarAt:i put:0
|
|
100 |
]
|
|
101 |
! !
|
|
102 |
|
|
103 |
!JavaObject methodsFor:'message sending'!
|
|
104 |
|
|
105 |
invokeJava:selector
|
|
106 |
"send javaSelector (name+sig) message, without arguments"
|
|
107 |
|
|
108 |
|method|
|
|
109 |
|
|
110 |
method := self class lookupMethodFor:selector.
|
|
111 |
method notNil ifTrue:[
|
78
|
112 |
^ self invokeJavaMethod:method sender:thisContext
|
43
|
113 |
].
|
|
114 |
|
|
115 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
116 |
|
|
117 |
!
|
|
118 |
|
101
|
119 |
invokeJava:selector interpreter:i sender:s with:arg1 with:arg2
|
|
120 |
"send javaSelector (name+sig) message, with 2 arguments"
|
|
121 |
|
|
122 |
|method|
|
|
123 |
|
|
124 |
method := self class lookupMethodFor:selector.
|
|
125 |
method notNil ifTrue:[
|
|
126 |
^ self invokeJavaMethod:method interpreter:i sender:s with:arg1 with:arg2
|
|
127 |
].
|
|
128 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
129 |
!
|
|
130 |
|
43
|
131 |
invokeJava:selector with:arg
|
|
132 |
"send javaSelector (name+sig) message, with 1 argument"
|
|
133 |
|
|
134 |
|method|
|
|
135 |
|
|
136 |
method := self class lookupMethodFor:selector.
|
|
137 |
method notNil ifTrue:[
|
|
138 |
^ self invokeJavaMethod:method with:arg
|
|
139 |
].
|
|
140 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
141 |
!
|
|
142 |
|
|
143 |
invokeJavaMethod:aJavaMethod
|
|
144 |
"invoke java method, without arguments"
|
|
145 |
|
101
|
146 |
^ self invokeJavaMethod:aJavaMethod interpreter:JavaInterpreter new sender:thisContext sender.
|
43
|
147 |
!
|
|
148 |
|
101
|
149 |
invokeJavaMethod:aJavaMethod interpreter:i sender:aContext
|
78
|
150 |
"invoke a java method, without arguments"
|
54
|
151 |
|
101
|
152 |
|val|
|
54
|
153 |
|
|
154 |
aJavaMethod numArgs ~~ 0 ifTrue:[
|
|
155 |
self halt:'need arguments'
|
|
156 |
].
|
|
157 |
aJavaMethod isStatic ifTrue:[
|
|
158 |
self halt:'static function'
|
|
159 |
].
|
|
160 |
|
101
|
161 |
"/ i := JavaInterpreter new.
|
54
|
162 |
i push:self.
|
78
|
163 |
|
54
|
164 |
val := i interpret:aJavaMethod sender:aContext.
|
|
165 |
|
|
166 |
^ JavaObject convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
167 |
!
|
|
168 |
|
101
|
169 |
invokeJavaMethod:aJavaMethod interpreter:i sender:s with:arg1
|
|
170 |
"invoke a java method, with two arguments.
|
|
171 |
CAVEAT: these cannot be long or doubles currently."
|
43
|
172 |
|
101
|
173 |
|val|
|
43
|
174 |
|
|
175 |
aJavaMethod numArgs ~~ 1 ifTrue:[
|
|
176 |
self halt:'argument count'
|
|
177 |
].
|
|
178 |
aJavaMethod isStatic ifTrue:[
|
|
179 |
self halt:'static function'
|
|
180 |
].
|
|
181 |
|
|
182 |
i push:self.
|
101
|
183 |
i push:arg1.
|
43
|
184 |
|
101
|
185 |
val := i interpret:aJavaMethod sender:s.
|
43
|
186 |
|
|
187 |
^ JavaObject convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
188 |
|
44
|
189 |
!
|
|
190 |
|
101
|
191 |
invokeJavaMethod:aJavaMethod interpreter:i sender:s with:arg1 with:arg2
|
78
|
192 |
"invoke a java method, with two arguments.
|
|
193 |
CAVEAT: these cannot be long or doubles currently."
|
44
|
194 |
|
101
|
195 |
|val|
|
44
|
196 |
|
|
197 |
aJavaMethod numArgs ~~ 2 ifTrue:[
|
|
198 |
self halt:'argument count'
|
|
199 |
].
|
|
200 |
aJavaMethod isStatic ifTrue:[
|
|
201 |
self halt:'static function'
|
|
202 |
].
|
|
203 |
|
|
204 |
i push:self.
|
|
205 |
i push:arg1.
|
|
206 |
i push:arg2.
|
|
207 |
|
101
|
208 |
val := i interpret:aJavaMethod sender:s.
|
44
|
209 |
|
|
210 |
^ JavaObject convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
211 |
|
83
|
212 |
!
|
|
213 |
|
112
|
214 |
invokeJavaMethod:aJavaMethod interpreter:i sender:s with:arg1 with:arg2 with:arg3 with:arg4
|
|
215 |
"invoke a java method, with 4 arguments.
|
|
216 |
CAVEAT: these cannot be long or doubles currently."
|
|
217 |
|
|
218 |
|val|
|
|
219 |
|
|
220 |
aJavaMethod numArgs ~~ 4 ifTrue:[
|
|
221 |
self halt:'argument count'
|
|
222 |
].
|
|
223 |
aJavaMethod isStatic ifTrue:[
|
|
224 |
self halt:'static function'
|
|
225 |
].
|
|
226 |
|
|
227 |
i push:self.
|
|
228 |
i push:arg1.
|
|
229 |
i push:arg2.
|
|
230 |
i push:arg3.
|
|
231 |
i push:arg4.
|
|
232 |
|
|
233 |
val := i interpret:aJavaMethod sender:s.
|
|
234 |
|
|
235 |
^ JavaObject convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
236 |
|
|
237 |
!
|
|
238 |
|
101
|
239 |
invokeJavaMethod:aJavaMethod interpreter:i sender:s withAll:args
|
|
240 |
"invoke a java method, with two arguments.
|
83
|
241 |
CAVEAT: these cannot be long or doubles currently."
|
|
242 |
|
101
|
243 |
|val|
|
83
|
244 |
|
101
|
245 |
aJavaMethod numArgs ~~ args size ifTrue:[
|
83
|
246 |
self halt:'argument count'
|
|
247 |
].
|
|
248 |
aJavaMethod isStatic ifTrue:[
|
|
249 |
self halt:'static function'
|
|
250 |
].
|
|
251 |
|
|
252 |
i push:self.
|
101
|
253 |
args do:[:arg |
|
|
254 |
i push:arg.
|
|
255 |
].
|
83
|
256 |
|
101
|
257 |
val := i interpret:aJavaMethod sender:s.
|
83
|
258 |
|
|
259 |
^ JavaObject convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
260 |
|
101
|
261 |
!
|
|
262 |
|
|
263 |
invokeJavaMethod:aJavaMethod sender:aContext
|
|
264 |
"invoke a java method, without arguments"
|
|
265 |
|
|
266 |
^ self invokeJavaMethod:aJavaMethod interpreter:JavaInterpreter new sender:aContext
|
|
267 |
!
|
|
268 |
|
|
269 |
invokeJavaMethod:aJavaMethod with:arg
|
|
270 |
"invoke a java method, with one argument.
|
|
271 |
CAVEAT: this cannot be a long or doubles currently."
|
|
272 |
|
|
273 |
^ self invokeJavaMethod:aJavaMethod interpreter:JavaInterpreter new sender:thisContext sender with:arg
|
|
274 |
|
|
275 |
!
|
|
276 |
|
|
277 |
invokeJavaMethod:aJavaMethod with:arg1 with:arg2
|
|
278 |
"invoke a java method, with two arguments.
|
|
279 |
CAVEAT: these cannot be long or doubles currently."
|
|
280 |
|
|
281 |
^ self invokeJavaMethod:aJavaMethod interpreter:JavaInterpreter new sender:thisContext sender with:arg1 with:arg2
|
|
282 |
|
43
|
283 |
! !
|
|
284 |
|
|
285 |
!JavaObject methodsFor:'printing & storing'!
|
|
286 |
|
|
287 |
displayString
|
96
|
288 |
self class == Java javaString ifTrue:[
|
48
|
289 |
^ '''' , (JavaObject stringFromJavaString:self) , ''''
|
|
290 |
].
|
|
291 |
|
101
|
292 |
^ super displayString.
|
|
293 |
|
96
|
294 |
Object errorSignal handle:[:ex |
|
|
295 |
^ super displayString.
|
|
296 |
] do:[
|
|
297 |
^ JavaObject stringFromJavaString:(self invoke:#toString).
|
|
298 |
].
|
|
299 |
|
43
|
300 |
^ super displayString. "/ ^ 'a JavaObject(' , self class name , ')'
|
|
301 |
! !
|
|
302 |
|
|
303 |
!JavaObject methodsFor:'smalltalk interface'!
|
|
304 |
|
29
|
305 |
invoke:selector
|
|
306 |
"send a message, without args"
|
|
307 |
|
101
|
308 |
^ self invoke:selector interpreter:(JavaInterpreter new) sender:thisContext sender
|
|
309 |
|
|
310 |
"
|
|
311 |
|stack|
|
|
312 |
|
|
313 |
stack := (Java at:'java/util/Stack') basicNew.
|
|
314 |
stack invoke:#'<init>'.
|
|
315 |
"
|
|
316 |
"
|
|
317 |
|stack|
|
|
318 |
|
|
319 |
stack := (Java at:'java/util/Stack') new.
|
|
320 |
stack invoke:#isEmpty.
|
|
321 |
"
|
|
322 |
"
|
|
323 |
|stack|
|
|
324 |
|
|
325 |
stack := (Java at:'java/util/Stack') new.
|
|
326 |
stack invoke:#size.
|
|
327 |
"
|
|
328 |
!
|
|
329 |
|
|
330 |
invoke:selector interpreter:i sender:aContext
|
|
331 |
"send a message, without args"
|
|
332 |
|
41
|
333 |
|method cls sel|
|
29
|
334 |
|
78
|
335 |
method := self lookupMethod:selector numArgs:0.
|
|
336 |
method notNil ifTrue:[
|
101
|
337 |
^ self invokeJavaMethod:method interpreter:i sender:aContext
|
29
|
338 |
].
|
|
339 |
|
|
340 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
341 |
|
|
342 |
"
|
|
343 |
|stack|
|
|
344 |
|
|
345 |
stack := (Java at:'java/util/Stack') basicNew.
|
|
346 |
stack invoke:#'<init>'.
|
|
347 |
"
|
|
348 |
"
|
|
349 |
|stack|
|
|
350 |
|
43
|
351 |
stack := (Java at:'java/util/Stack') new.
|
29
|
352 |
stack invoke:#isEmpty.
|
|
353 |
"
|
|
354 |
"
|
|
355 |
|stack|
|
|
356 |
|
43
|
357 |
stack := (Java at:'java/util/Stack') new.
|
29
|
358 |
stack invoke:#size.
|
|
359 |
"
|
|
360 |
!
|
|
361 |
|
101
|
362 |
invoke:selector interpreter:i sender:aContext with:argument
|
43
|
363 |
"send a message, with 1 argument.
|
|
364 |
TEMPORARY: This method needs more work."
|
33
|
365 |
|
41
|
366 |
|sel method cls argClass jSel|
|
29
|
367 |
|
41
|
368 |
sel := selector asSymbolIfInterned.
|
|
369 |
sel notNil ifTrue:[
|
33
|
370 |
"/
|
41
|
371 |
"/ hard to do - must find a matching method probably
|
33
|
372 |
"/
|
41
|
373 |
(argument isKindOf:JavaObject) ifTrue:[
|
|
374 |
argClass := argument class.
|
|
375 |
] ifFalse:[
|
|
376 |
"/
|
|
377 |
"/ map to Java:
|
|
378 |
"/ String -> [c
|
|
379 |
"/
|
|
380 |
(argument isMemberOf:String) ifTrue:[
|
|
381 |
jSel := (selector , '([C)V') asSymbolIfInterned.
|
|
382 |
jSel notNil ifTrue:[
|
101
|
383 |
^ self invokeJava:jSel interpreter:i sender:aContext with:argument
|
29
|
384 |
]
|
|
385 |
]
|
|
386 |
].
|
41
|
387 |
|
78
|
388 |
method := self lookupMethod:sel numArgs:1.
|
|
389 |
method notNil ifTrue:[
|
101
|
390 |
^ self invokeJavaMethod:method interpreter:i sender:aContext with:argument
|
41
|
391 |
].
|
78
|
392 |
|
|
393 |
"/ cls := self class.
|
|
394 |
"/ [cls notNil and:[cls ~~ JavaObject]] whileTrue:[
|
|
395 |
"/ cls methodDictionary keysAndValuesDo:[:sel :aMethod |
|
|
396 |
"/ aMethod name == sel ifTrue:[
|
|
397 |
"/ aMethod numArgs == 1 ifTrue:[
|
|
398 |
"/ "/
|
|
399 |
"/ "/ this is not completely correct:
|
|
400 |
"/ "/ must look for the best type-match,
|
|
401 |
"/ "/ (especially: have to look for best match
|
|
402 |
"/ "/ over whole superclass chain ...)
|
|
403 |
"/ "/ for now take the first with matching number of args
|
|
404 |
"/ "/
|
|
405 |
"/ ^ self invokeJavaMethod:aMethod with:argument
|
|
406 |
"/ ]
|
|
407 |
"/ ]
|
|
408 |
"/ ].
|
|
409 |
"/ cls := cls superclass.
|
|
410 |
"/ ].
|
29
|
411 |
].
|
|
412 |
|
|
413 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
414 |
|
|
415 |
"
|
|
416 |
|stack|
|
|
417 |
|
33
|
418 |
stack := (Java at:'java/util/Stack') new.
|
29
|
419 |
stack invoke:#push with:1.
|
|
420 |
"
|
78
|
421 |
"
|
|
422 |
|stack|
|
|
423 |
|
|
424 |
stack := (Java at:'java/util/Stack') new.
|
|
425 |
stack invoke:#push with:1.
|
|
426 |
stack invoke:#push with:2.
|
|
427 |
stack invoke:#pop.
|
|
428 |
stack invoke:#pop.
|
|
429 |
stack invoke:#pop.
|
|
430 |
"
|
|
431 |
!
|
|
432 |
|
112
|
433 |
invoke:selector interpreter:i sender:aContext with:arg1 with:arg2 with:arg3 with:arg4
|
|
434 |
"send a message, with 4 arguments.
|
|
435 |
TEMPORARY: This method needs more work."
|
|
436 |
|
|
437 |
|sel method cls argClass jSel|
|
|
438 |
|
|
439 |
sel := selector asSymbolIfInterned.
|
|
440 |
sel notNil ifTrue:[
|
|
441 |
method := self lookupMethod:sel numArgs:4.
|
|
442 |
method notNil ifTrue:[
|
|
443 |
^ self invokeJavaMethod:method interpreter:i sender:aContext
|
|
444 |
with:arg1 with:arg2 with:arg3 with:arg4
|
|
445 |
].
|
|
446 |
].
|
|
447 |
|
|
448 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
449 |
|
|
450 |
!
|
|
451 |
|
|
452 |
invoke:selector interpreter:i sender:aContext withAll:arguments
|
|
453 |
"send a message, with arguments.
|
|
454 |
TEMPORARY: This method needs more work."
|
|
455 |
|
|
456 |
|sel method cls argClass jSel|
|
|
457 |
|
|
458 |
sel := selector asSymbolIfInterned.
|
|
459 |
sel notNil ifTrue:[
|
|
460 |
method := self lookupMethod:sel numArgs:arguments size.
|
|
461 |
method notNil ifTrue:[
|
|
462 |
^ self invokeJavaMethod:method interpreter:i sender:aContext withAll:arguments
|
|
463 |
].
|
|
464 |
].
|
|
465 |
|
|
466 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
467 |
|
|
468 |
!
|
|
469 |
|
101
|
470 |
invoke:selector sender:aContext
|
|
471 |
"send a message, without args"
|
83
|
472 |
|
101
|
473 |
|method cls sel|
|
83
|
474 |
|
101
|
475 |
method := self lookupMethod:selector numArgs:0.
|
|
476 |
method notNil ifTrue:[
|
|
477 |
^ self invokeJavaMethod:method interpreter:(JavaInterpreter new) sender:aContext
|
83
|
478 |
].
|
|
479 |
|
|
480 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
481 |
|
|
482 |
"
|
|
483 |
|stack|
|
|
484 |
|
101
|
485 |
stack := (Java at:'java/util/Stack') basicNew.
|
|
486 |
stack invoke:#'<init>'.
|
|
487 |
"
|
|
488 |
"
|
|
489 |
|stack|
|
|
490 |
|
|
491 |
stack := (Java at:'java/util/Stack') new.
|
|
492 |
stack invoke:#isEmpty.
|
|
493 |
"
|
|
494 |
"
|
|
495 |
|stack|
|
|
496 |
|
|
497 |
stack := (Java at:'java/util/Stack') new.
|
|
498 |
stack invoke:#size.
|
|
499 |
"
|
|
500 |
!
|
|
501 |
|
|
502 |
invoke:selector with:arg
|
|
503 |
"send a message, with 1 argument.
|
|
504 |
TEMPORARY: This method needs more work."
|
|
505 |
|
|
506 |
^ self invoke:selector interpreter:JavaInterpreter new sender:thisContext sender with:arg
|
|
507 |
|
|
508 |
"
|
|
509 |
|stack|
|
|
510 |
|
83
|
511 |
stack := (Java at:'java/util/Stack') new.
|
|
512 |
stack invoke:#push with:1.
|
|
513 |
"
|
|
514 |
"
|
|
515 |
|stack|
|
|
516 |
|
|
517 |
stack := (Java at:'java/util/Stack') new.
|
|
518 |
stack invoke:#push with:1.
|
|
519 |
stack invoke:#push with:2.
|
|
520 |
stack invoke:#pop.
|
|
521 |
stack invoke:#pop.
|
|
522 |
stack invoke:#pop.
|
|
523 |
"
|
|
524 |
!
|
|
525 |
|
78
|
526 |
lookupMethod:selector numArgs:nargs
|
|
527 |
"lookup a method"
|
|
528 |
|
|
529 |
|method cls sel|
|
|
530 |
|
|
531 |
sel := selector asSymbolIfInterned.
|
|
532 |
sel notNil ifTrue:[
|
|
533 |
cls := self class.
|
|
534 |
[cls notNil and:[cls ~~ JavaObject]] whileTrue:[
|
|
535 |
cls methodDictionary keysAndValuesDo:[:sel :aMethod |
|
|
536 |
aMethod name == selector ifTrue:[
|
|
537 |
aMethod numArgs == nargs ifTrue:[
|
|
538 |
^ aMethod
|
|
539 |
]
|
|
540 |
]
|
|
541 |
].
|
|
542 |
cls := cls superclass.
|
|
543 |
].
|
|
544 |
].
|
|
545 |
|
|
546 |
^ nil
|
|
547 |
|
|
548 |
"
|
|
549 |
|stack|
|
|
550 |
|
|
551 |
stack := (Java at:'java/util/Stack') basicNew.
|
|
552 |
stack lookupMethod:#'<init>' numArgs:0.
|
|
553 |
"
|
|
554 |
"
|
|
555 |
|stack|
|
|
556 |
|
|
557 |
stack := (Java at:'java/util/Stack') new.
|
|
558 |
stack lookupMethod:#isEmpty numArgs:0.
|
|
559 |
"
|
26
|
560 |
! !
|
|
561 |
|
69
|
562 |
!JavaObject class methodsFor:'documentation'!
|
26
|
563 |
|
|
564 |
version
|
112
|
565 |
^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaObject.st,v 1.19 1996/07/02 22:16:06 cg Exp $'
|
26
|
566 |
! !
|