26
|
1 |
Object subclass:#JavaObject
|
|
2 |
instanceVariableNames:''
|
|
3 |
classVariableNames:''
|
|
4 |
poolDictionaries:''
|
|
5 |
category:'Java-Classes'
|
|
6 |
!
|
|
7 |
|
|
8 |
|
29
|
9 |
!JavaObject class methodsFor:'misc'!
|
|
10 |
|
|
11 |
resolveClassRefs
|
|
12 |
! !
|
|
13 |
|
43
|
14 |
!JavaObject class methodsFor:'smalltalk interface'!
|
29
|
15 |
|
|
16 |
convertJavaObject:val signature:retValSignature
|
|
17 |
retValSignature = 'void' ifTrue:[
|
|
18 |
^ #void
|
|
19 |
].
|
|
20 |
retValSignature = 'boolean' ifTrue:[
|
|
21 |
val == 0 ifTrue:[^ false].
|
|
22 |
^ true
|
|
23 |
].
|
|
24 |
retValSignature = 'int' ifTrue:[
|
|
25 |
val isInteger ifFalse:[
|
|
26 |
self halt
|
|
27 |
].
|
|
28 |
^ val
|
|
29 |
].
|
34
|
30 |
retValSignature = 'char[]' ifTrue:[
|
|
31 |
"/ these are ST-strings
|
|
32 |
^ val
|
|
33 |
].
|
|
34 |
|
|
35 |
retValSignature = 'char' ifTrue:[
|
|
36 |
"/ these are ST-characters
|
40
|
37 |
val isInteger ifTrue:[
|
|
38 |
^ Character value:val
|
|
39 |
].
|
|
40 |
self halt.
|
34
|
41 |
^ val
|
|
42 |
].
|
|
43 |
|
29
|
44 |
retValSignature = 'Object' ifTrue:[
|
|
45 |
^ val
|
|
46 |
].
|
|
47 |
|
44
|
48 |
retValSignature = 'String' ifTrue:[
|
|
49 |
^ self stringFromJavaString:val
|
|
50 |
].
|
|
51 |
|
29
|
52 |
'no conversion for: ' print. val class name print. ' to: ' print. retValSignature printNL.
|
|
53 |
^ val.
|
43
|
54 |
|
|
55 |
|
29
|
56 |
!
|
|
57 |
|
43
|
58 |
javaStringFrom:aString
|
|
59 |
"hard-coding internas of java.lang.String here is bad ..."
|
|
60 |
|
|
61 |
|s|
|
|
62 |
|
|
63 |
s := (Java at:'java/lang/String') basicNew.
|
|
64 |
s instVarNamed:'value' put: aString.
|
|
65 |
s instVarNamed:'offset' put: 0.
|
|
66 |
s instVarNamed:'count' put: aString size.
|
|
67 |
^ s
|
44
|
68 |
!
|
|
69 |
|
|
70 |
stringFromJavaString:aJavaString
|
|
71 |
"hard-coding internas of java.lang.String here is bad ..."
|
|
72 |
|
|
73 |
(aJavaString instVarNamed:'offset') ~~ 0 ifTrue:[self halt].
|
|
74 |
|
|
75 |
^ ((aJavaString instVarNamed:'value')
|
|
76 |
copyTo:(aJavaString instVarNamed:'count')) asString
|
43
|
77 |
! !
|
|
78 |
|
|
79 |
!JavaObject methodsFor:'initialization'!
|
|
80 |
|
52
|
81 |
initializeFields:initialValues
|
|
82 |
|sz|
|
|
83 |
|
|
84 |
sz := self class instSize.
|
|
85 |
1 to:sz do:[:i |
|
|
86 |
self instVarAt:i put:(initialValues at:i)
|
|
87 |
]
|
|
88 |
!
|
|
89 |
|
43
|
90 |
initializeToZero
|
52
|
91 |
|sz|
|
|
92 |
|
|
93 |
sz := self class instSize.
|
|
94 |
1 to:sz do:[:i |
|
43
|
95 |
self instVarAt:i put:0
|
|
96 |
]
|
|
97 |
! !
|
|
98 |
|
|
99 |
!JavaObject methodsFor:'message sending'!
|
|
100 |
|
|
101 |
invokeJava:selector
|
|
102 |
"send javaSelector (name+sig) message, without arguments"
|
|
103 |
|
|
104 |
|method|
|
|
105 |
|
|
106 |
method := self class lookupMethodFor:selector.
|
|
107 |
method notNil ifTrue:[
|
|
108 |
^ self invokeJavaMethod:method
|
|
109 |
].
|
|
110 |
|
|
111 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
112 |
|
|
113 |
!
|
|
114 |
|
|
115 |
invokeJava:selector with:arg
|
|
116 |
"send javaSelector (name+sig) message, with 1 argument"
|
|
117 |
|
|
118 |
|method|
|
|
119 |
|
|
120 |
method := self class lookupMethodFor:selector.
|
|
121 |
method notNil ifTrue:[
|
|
122 |
^ self invokeJavaMethod:method with:arg
|
|
123 |
].
|
|
124 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
125 |
!
|
|
126 |
|
44
|
127 |
invokeJava:selector with:arg1 with:arg2
|
|
128 |
"send javaSelector (name+sig) message, with 2 arguments"
|
|
129 |
|
|
130 |
|method|
|
|
131 |
|
|
132 |
method := self class lookupMethodFor:selector.
|
|
133 |
method notNil ifTrue:[
|
|
134 |
^ self invokeJavaMethod:method with:arg1 with:arg2
|
|
135 |
].
|
|
136 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
137 |
!
|
|
138 |
|
43
|
139 |
invokeJavaMethod:aJavaMethod
|
|
140 |
"invoke java method, without arguments"
|
|
141 |
|
|
142 |
|i val|
|
|
143 |
|
|
144 |
aJavaMethod numArgs ~~ 0 ifTrue:[
|
|
145 |
self halt:'need arguments'
|
|
146 |
].
|
|
147 |
aJavaMethod isStatic ifTrue:[
|
|
148 |
self halt:'static function'
|
|
149 |
].
|
|
150 |
|
|
151 |
i := JavaInterpreter new.
|
|
152 |
i push:self.
|
|
153 |
val := i interpret:aJavaMethod sender:thisContext sender.
|
|
154 |
|
|
155 |
^ JavaObject convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
156 |
!
|
|
157 |
|
|
158 |
invokeJavaMethod:aJavaMethod with:arg
|
|
159 |
"invoke java method, with one argument"
|
|
160 |
|
|
161 |
|i val|
|
|
162 |
|
|
163 |
aJavaMethod numArgs ~~ 1 ifTrue:[
|
|
164 |
self halt:'argument count'
|
|
165 |
].
|
|
166 |
aJavaMethod isStatic ifTrue:[
|
|
167 |
self halt:'static function'
|
|
168 |
].
|
|
169 |
|
|
170 |
i := JavaInterpreter new.
|
|
171 |
i push:self.
|
|
172 |
i push:arg.
|
|
173 |
|
|
174 |
val := i interpret:aJavaMethod sender:thisContext sender.
|
|
175 |
|
|
176 |
^ JavaObject convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
177 |
|
44
|
178 |
!
|
|
179 |
|
|
180 |
invokeJavaMethod:aJavaMethod with:arg1 with:arg2
|
|
181 |
"invoke java method, with two arguments"
|
|
182 |
|
|
183 |
|i val|
|
|
184 |
|
|
185 |
aJavaMethod numArgs ~~ 2 ifTrue:[
|
|
186 |
self halt:'argument count'
|
|
187 |
].
|
|
188 |
aJavaMethod isStatic ifTrue:[
|
|
189 |
self halt:'static function'
|
|
190 |
].
|
|
191 |
|
|
192 |
i := JavaInterpreter new.
|
|
193 |
i push:self.
|
|
194 |
i push:arg1.
|
|
195 |
i push:arg2.
|
|
196 |
|
|
197 |
val := i interpret:aJavaMethod sender:thisContext sender.
|
|
198 |
|
|
199 |
^ JavaObject convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
200 |
|
43
|
201 |
! !
|
|
202 |
|
|
203 |
!JavaObject methodsFor:'printing & storing'!
|
|
204 |
|
|
205 |
displayString
|
48
|
206 |
self class fullName = 'java/lang/String' ifTrue:[
|
|
207 |
^ '''' , (JavaObject stringFromJavaString:self) , ''''
|
|
208 |
].
|
|
209 |
|
43
|
210 |
^ super displayString. "/ ^ 'a JavaObject(' , self class name , ')'
|
|
211 |
! !
|
|
212 |
|
|
213 |
!JavaObject methodsFor:'smalltalk interface'!
|
|
214 |
|
29
|
215 |
invoke:selector
|
|
216 |
"send a message, without args"
|
|
217 |
|
41
|
218 |
|method cls sel|
|
29
|
219 |
|
41
|
220 |
sel := selector asSymbolIfInterned.
|
|
221 |
sel notNil ifTrue:[
|
|
222 |
cls := self class.
|
|
223 |
[cls notNil and:[cls ~~ JavaObject]] whileTrue:[
|
|
224 |
cls methodArray do:[:aMethod |
|
|
225 |
"/ aMethod name printNL.
|
|
226 |
aMethod name == selector ifTrue:[
|
|
227 |
aMethod numArgs == 0 ifTrue:[
|
|
228 |
^ self invokeJavaMethod:aMethod
|
|
229 |
]
|
29
|
230 |
]
|
41
|
231 |
].
|
|
232 |
cls := cls superclass.
|
29
|
233 |
].
|
|
234 |
].
|
|
235 |
|
|
236 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
237 |
|
|
238 |
"
|
|
239 |
|stack|
|
|
240 |
|
|
241 |
stack := (Java at:'java/util/Stack') basicNew.
|
|
242 |
stack invoke:#'<init>'.
|
|
243 |
"
|
|
244 |
"
|
|
245 |
|stack|
|
|
246 |
|
43
|
247 |
stack := (Java at:'java/util/Stack') new.
|
29
|
248 |
stack invoke:#isEmpty.
|
|
249 |
"
|
|
250 |
"
|
|
251 |
|stack|
|
|
252 |
|
43
|
253 |
stack := (Java at:'java/util/Stack') new.
|
29
|
254 |
stack invoke:#size.
|
|
255 |
"
|
|
256 |
!
|
|
257 |
|
|
258 |
invoke:selector with:argument
|
43
|
259 |
"send a message, with 1 argument.
|
|
260 |
TEMPORARY: This method needs more work."
|
33
|
261 |
|
41
|
262 |
|sel method cls argClass jSel|
|
29
|
263 |
|
41
|
264 |
sel := selector asSymbolIfInterned.
|
|
265 |
sel notNil ifTrue:[
|
33
|
266 |
"/
|
41
|
267 |
"/ hard to do - must find a matching method probably
|
33
|
268 |
"/
|
41
|
269 |
(argument isKindOf:JavaObject) ifTrue:[
|
|
270 |
argClass := argument class.
|
|
271 |
] ifFalse:[
|
|
272 |
"/
|
|
273 |
"/ map to Java:
|
|
274 |
"/ String -> [c
|
|
275 |
"/
|
|
276 |
(argument isMemberOf:String) ifTrue:[
|
|
277 |
jSel := (selector , '([C)V') asSymbolIfInterned.
|
|
278 |
jSel notNil ifTrue:[
|
|
279 |
^ self invokeJava:jSel with:argument
|
29
|
280 |
]
|
|
281 |
]
|
|
282 |
].
|
41
|
283 |
|
|
284 |
cls := self class.
|
|
285 |
[cls notNil and:[cls ~~ JavaObject]] whileTrue:[
|
|
286 |
cls methodArray do:[:aMethod |
|
|
287 |
aMethod name == sel ifTrue:[
|
|
288 |
aMethod numArgs == 1 ifTrue:[
|
|
289 |
"/
|
|
290 |
"/ this is not completely correct:
|
|
291 |
"/ must look for the best type-match,
|
|
292 |
"/ (especially: have to look for best match
|
|
293 |
"/ over whole superclass chain ...)
|
|
294 |
"/ for now take the first with matching number of args
|
|
295 |
"/
|
|
296 |
^ self invokeJavaMethod:aMethod with:argument
|
|
297 |
]
|
|
298 |
]
|
|
299 |
].
|
|
300 |
cls := cls superclass.
|
|
301 |
].
|
29
|
302 |
].
|
|
303 |
|
|
304 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
305 |
|
|
306 |
"
|
|
307 |
|stack|
|
|
308 |
|
33
|
309 |
stack := (Java at:'java/util/Stack') new.
|
29
|
310 |
stack invoke:#push with:1.
|
|
311 |
"
|
26
|
312 |
! !
|
|
313 |
|
|
314 |
!JavaObject class methodsFor:'documentation'!
|
|
315 |
|
|
316 |
version
|
52
|
317 |
^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaObject.st,v 1.11 1996/05/06 14:20:35 cg Exp $'
|
26
|
318 |
! !
|