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 |
|
41
|
14 |
!JavaObject class methodsFor:'queries'!
|
|
15 |
|
|
16 |
browserClass
|
|
17 |
"return the browser to use for this class -
|
|
18 |
this can be redefined in special classes, to get different browsers"
|
|
19 |
|
|
20 |
self == JavaObject ifTrue:[
|
|
21 |
^ super browserClass
|
|
22 |
].
|
|
23 |
^ JavaBrowser
|
|
24 |
|
|
25 |
|
|
26 |
! !
|
|
27 |
|
29
|
28 |
!JavaObject methodsFor:'initialization'!
|
|
29 |
|
|
30 |
initializeToZero
|
|
31 |
1 to:self class instSize do:[:i |
|
|
32 |
self instVarAt:i put:0
|
|
33 |
]
|
|
34 |
! !
|
|
35 |
|
26
|
36 |
!JavaObject methodsFor:'message sending'!
|
|
37 |
|
29
|
38 |
invokeJava:selector
|
|
39 |
"send javaSelector (name+sig) message, without arguments"
|
|
40 |
|
41
|
41 |
|method cls sel|
|
26
|
42 |
|
41
|
43 |
sel := selector asSymbolIfInterned.
|
|
44 |
sel notNil ifTrue:[
|
|
45 |
cls := self class.
|
|
46 |
[cls notNil] whileTrue:[
|
|
47 |
method := cls compiledMethodAt:sel.
|
|
48 |
method notNil ifTrue:[
|
|
49 |
^ self invokeJavaMethod:method
|
|
50 |
].
|
|
51 |
cls := cls superclass.
|
29
|
52 |
].
|
|
53 |
].
|
|
54 |
|
|
55 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
56 |
|
|
57 |
!
|
|
58 |
|
|
59 |
invokeJava:selector with:arg
|
|
60 |
"send javaSelector (name+sig) message, with 1 argument"
|
|
61 |
|
41
|
62 |
|method cls sel|
|
26
|
63 |
|
41
|
64 |
sel := selector asSymbolIfInterned.
|
|
65 |
sel notNil ifTrue:[
|
|
66 |
cls := self class.
|
|
67 |
[cls notNil] whileTrue:[
|
|
68 |
method := cls compiledMethodAt:sel.
|
|
69 |
method notNil ifTrue:[
|
|
70 |
^ self invokeJavaMethod:method with:arg
|
|
71 |
].
|
|
72 |
cls := cls superclass.
|
29
|
73 |
].
|
|
74 |
].
|
|
75 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
76 |
!
|
|
77 |
|
|
78 |
invokeJavaMethod:aJavaMethod
|
|
79 |
"invoke java method, without arguments"
|
|
80 |
|
|
81 |
|i val|
|
|
82 |
|
|
83 |
aJavaMethod numArgs ~~ 0 ifTrue:[
|
|
84 |
self halt:'need arguments'
|
26
|
85 |
].
|
29
|
86 |
|
|
87 |
i := JavaInterpreter new.
|
|
88 |
i push:self.
|
|
89 |
|
|
90 |
val := i interpret:aJavaMethod sender:thisContext sender.
|
|
91 |
|
|
92 |
^ self convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
93 |
!
|
|
94 |
|
|
95 |
invokeJavaMethod:aJavaMethod with:arg
|
|
96 |
"invoke java method, with one argument"
|
|
97 |
|
|
98 |
|i val|
|
|
99 |
|
|
100 |
aJavaMethod numArgs ~~ 1 ifTrue:[
|
|
101 |
self halt:'argument count'
|
|
102 |
].
|
|
103 |
|
|
104 |
|
26
|
105 |
i := JavaInterpreter new.
|
|
106 |
i push:self.
|
29
|
107 |
i push:arg.
|
|
108 |
|
|
109 |
val := i interpret:aJavaMethod sender:thisContext sender.
|
|
110 |
|
|
111 |
^ self convertJavaObject:val signature:(aJavaMethod retValSignature)
|
|
112 |
|
|
113 |
! !
|
|
114 |
|
|
115 |
!JavaObject methodsFor:'printing & storing'!
|
|
116 |
|
|
117 |
displayString
|
34
|
118 |
^ super displayString. "/ ^ 'a JavaObject(' , self class name , ')'
|
29
|
119 |
|
|
120 |
! !
|
|
121 |
|
|
122 |
!JavaObject methodsFor:'smalltalk interface'!
|
|
123 |
|
|
124 |
convertJavaObject:val signature:retValSignature
|
|
125 |
retValSignature = 'void' ifTrue:[
|
|
126 |
^ #void
|
|
127 |
].
|
|
128 |
retValSignature = 'boolean' ifTrue:[
|
|
129 |
val == 0 ifTrue:[^ false].
|
|
130 |
^ true
|
|
131 |
].
|
|
132 |
retValSignature = 'int' ifTrue:[
|
|
133 |
val isInteger ifFalse:[
|
|
134 |
self halt
|
|
135 |
].
|
|
136 |
^ val
|
|
137 |
].
|
34
|
138 |
retValSignature = 'char[]' ifTrue:[
|
|
139 |
"/ these are ST-strings
|
|
140 |
^ val
|
|
141 |
].
|
|
142 |
|
|
143 |
retValSignature = 'char' ifTrue:[
|
|
144 |
"/ these are ST-characters
|
40
|
145 |
val isInteger ifTrue:[
|
|
146 |
^ Character value:val
|
|
147 |
].
|
|
148 |
self halt.
|
34
|
149 |
^ val
|
|
150 |
].
|
|
151 |
|
29
|
152 |
retValSignature = 'Object' ifTrue:[
|
|
153 |
^ val
|
|
154 |
].
|
|
155 |
|
|
156 |
'no conversion for: ' print. val class name print. ' to: ' print. retValSignature printNL.
|
|
157 |
^ val.
|
|
158 |
!
|
|
159 |
|
|
160 |
invoke:selector
|
|
161 |
"send a message, without args"
|
|
162 |
|
41
|
163 |
|method cls sel|
|
29
|
164 |
|
41
|
165 |
sel := selector asSymbolIfInterned.
|
|
166 |
sel notNil ifTrue:[
|
|
167 |
cls := self class.
|
|
168 |
[cls notNil and:[cls ~~ JavaObject]] whileTrue:[
|
|
169 |
cls methodArray do:[:aMethod |
|
|
170 |
"/ aMethod name printNL.
|
|
171 |
aMethod name == selector ifTrue:[
|
|
172 |
aMethod numArgs == 0 ifTrue:[
|
|
173 |
^ self invokeJavaMethod:aMethod
|
|
174 |
]
|
29
|
175 |
]
|
41
|
176 |
].
|
|
177 |
cls := cls superclass.
|
29
|
178 |
].
|
|
179 |
].
|
|
180 |
|
|
181 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
182 |
|
|
183 |
"
|
|
184 |
|stack|
|
|
185 |
|
|
186 |
stack := (Java at:'java/util/Stack') basicNew.
|
|
187 |
stack invoke:#'<init>'.
|
|
188 |
"
|
|
189 |
"
|
|
190 |
|stack|
|
|
191 |
|
|
192 |
stack := (Java at:'java/util/Stack') basicNew.
|
|
193 |
stack invoke:#isEmpty.
|
|
194 |
"
|
|
195 |
"
|
|
196 |
|stack|
|
|
197 |
|
|
198 |
stack := (Java at:'java/util/Stack') basicNew.
|
|
199 |
stack invoke:#size.
|
|
200 |
"
|
|
201 |
!
|
|
202 |
|
|
203 |
invoke:selector with:argument
|
33
|
204 |
"send a message, with 1 argument. This method needs more work."
|
|
205 |
|
41
|
206 |
|sel method cls argClass jSel|
|
29
|
207 |
|
41
|
208 |
sel := selector asSymbolIfInterned.
|
|
209 |
sel notNil ifTrue:[
|
33
|
210 |
"/
|
41
|
211 |
"/ hard to do - must find a matching method probably
|
33
|
212 |
"/
|
41
|
213 |
(argument isKindOf:JavaObject) ifTrue:[
|
|
214 |
argClass := argument class.
|
|
215 |
] ifFalse:[
|
|
216 |
"/
|
|
217 |
"/ map to Java:
|
|
218 |
"/ String -> [c
|
|
219 |
"/
|
|
220 |
(argument isMemberOf:String) ifTrue:[
|
|
221 |
jSel := (selector , '([C)V') asSymbolIfInterned.
|
|
222 |
jSel notNil ifTrue:[
|
|
223 |
^ self invokeJava:jSel with:argument
|
29
|
224 |
]
|
|
225 |
]
|
|
226 |
].
|
41
|
227 |
|
|
228 |
cls := self class.
|
|
229 |
[cls notNil and:[cls ~~ JavaObject]] whileTrue:[
|
|
230 |
cls methodArray do:[:aMethod |
|
|
231 |
aMethod name == sel ifTrue:[
|
|
232 |
aMethod numArgs == 1 ifTrue:[
|
|
233 |
"/
|
|
234 |
"/ this is not completely correct:
|
|
235 |
"/ must look for the best type-match,
|
|
236 |
"/ (especially: have to look for best match
|
|
237 |
"/ over whole superclass chain ...)
|
|
238 |
"/ for now take the first with matching number of args
|
|
239 |
"/
|
|
240 |
^ self invokeJavaMethod:aMethod with:argument
|
|
241 |
]
|
|
242 |
]
|
|
243 |
].
|
|
244 |
cls := cls superclass.
|
|
245 |
].
|
29
|
246 |
].
|
|
247 |
|
|
248 |
^ self doesNotUnderstand:(Message selector:selector)
|
|
249 |
|
|
250 |
"
|
|
251 |
|stack|
|
|
252 |
|
33
|
253 |
stack := (Java at:'java/util/Stack') new.
|
29
|
254 |
stack invoke:#push with:1.
|
|
255 |
"
|
26
|
256 |
! !
|
|
257 |
|
|
258 |
!JavaObject class methodsFor:'documentation'!
|
|
259 |
|
|
260 |
version
|
41
|
261 |
^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/JavaObject.st,v 1.6 1996/05/03 11:29:12 cg Exp $'
|
26
|
262 |
! !
|