author | cg |
Mon, 30 Mar 1998 15:26:47 +0000 | |
changeset 324 | f92b236baea5 |
parent 318 | 8720093188f7 |
child 340 | 79f09bb1ac21 |
permissions | -rw-r--r-- |
17 | 1 |
Object subclass:#Java |
2 |
instanceVariableNames:'' |
|
201 | 3 |
classVariableNames:'Classes UnresolvedClassRefs ClassPath SourceDirectories JavaHome |
224 | 4 |
Java_lang_String Java_lang_Class PrettyPrintStyle |
276 | 5 |
LastArgumentString Threads ExcludedClassPath' |
17 | 6 |
poolDictionaries:'' |
7 |
category:'Java-Support' |
|
8 |
! |
|
9 |
||
10 |
||
135 | 11 |
!Java class methodsFor:'accessing'! |
24 | 12 |
|
147 | 13 |
classForName:aString |
14 |
"return a javaClass - either existing or loaded" |
|
15 |
||
255 | 16 |
|cls loader classURL| |
147 | 17 |
|
18 |
cls := self at:aString. |
|
19 |
cls isNil ifTrue:[ |
|
261 | 20 |
('JAVA [info]: late class loading: ' , aString) infoPrintCR. |
21 |
loader := JavaClassReader classLoaderQuerySignal raise. |
|
22 |
loader isNil ifTrue:[ |
|
23 |
"/ load using default (ST/X) loader |
|
24 |
cls := JavaClassReader loadClass:aString. |
|
25 |
] ifFalse:[ |
|
26 |
"/ load using a Java class loader |
|
255 | 27 |
"/ classURL := Java as_URL:('file:' , aString , '.class'). |
28 |
"/ loader loadClass:classURL. |
|
261 | 29 |
loader perform:#'loadClass(Ljava/lang/String;)Ljava/lang/Class;' |
30 |
with:(Java as_String:aString). |
|
255 | 31 |
|
261 | 32 |
cls := self at:aString. |
33 |
] |
|
147 | 34 |
]. |
35 |
^ cls |
|
36 |
||
255 | 37 |
"Created: / 18.3.1997 / 16:45:57 / cg" |
38 |
"Modified: / 9.1.1998 / 03:43:16 / cg" |
|
147 | 39 |
! |
40 |
||
90 | 41 |
classNamed:aString |
42 |
^ self at:aString |
|
32 | 43 |
! |
44 |
||
167 | 45 |
java_lang_Class |
46 |
Java_lang_Class isNil ifTrue:[ |
|
248 | 47 |
Java_lang_Class := self at:'java.lang.Class' |
167 | 48 |
]. |
49 |
^ Java_lang_Class |
|
50 |
||
51 |
"Created: 26.3.1997 / 13:42:59 / cg" |
|
52 |
"Modified: 26.3.1997 / 13:46:24 / cg" |
|
53 |
! |
|
54 |
||
55 |
java_lang_String |
|
56 |
Java_lang_String isNil ifTrue:[ |
|
248 | 57 |
Java_lang_String := self at:'java.lang.String' |
167 | 58 |
]. |
59 |
^ Java_lang_String |
|
60 |
||
61 |
"Created: 26.3.1997 / 13:43:17 / cg" |
|
62 |
"Modified: 26.3.1997 / 13:50:21 / cg" |
|
76 | 63 |
! |
64 |
||
188 | 65 |
prettyPrintStyle |
66 |
^ PrettyPrintStyle |
|
67 |
||
68 |
"Created: 1.8.1997 / 10:37:57 / cg" |
|
248 | 69 |
! |
70 |
||
71 |
threads |
|
72 |
Threads isNil ifTrue:[ |
|
261 | 73 |
Threads := WeakIdentityDictionary new. |
248 | 74 |
]. |
75 |
^ Threads |
|
76 |
||
77 |
"Created: 26.8.1997 / 19:53:57 / cg" |
|
78 |
"Modified: 26.8.1997 / 19:55:27 / cg" |
|
192 | 79 |
! ! |
80 |
||
81 |
!Java class methodsFor:'accessing paths'! |
|
82 |
||
83 |
addToClassPath:aPath |
|
84 |
(ClassPath includes:aPath) ifFalse:[ |
|
248 | 85 |
ClassPath add:aPath |
192 | 86 |
] |
87 |
||
88 |
"Modified: 7.2.1997 / 19:23:55 / cg" |
|
89 |
"Created: 1.8.1997 / 21:10:07 / cg" |
|
90 |
! |
|
91 |
||
198 | 92 |
addToSourcePath:aPath |
93 |
(SourceDirectories includes:aPath) ifFalse:[ |
|
248 | 94 |
SourceDirectories add:aPath |
198 | 95 |
] |
96 |
||
97 |
"Modified: 7.2.1997 / 19:23:55 / cg" |
|
98 |
"Created: 2.8.1997 / 14:12:31 / cg" |
|
99 |
! |
|
100 |
||
192 | 101 |
classPath |
102 |
^ ClassPath |
|
103 |
||
104 |
"Created: 7.2.1997 / 19:23:45 / cg" |
|
105 |
"Modified: 7.2.1997 / 19:23:55 / cg" |
|
106 |
! |
|
107 |
||
108 |
classPath:aCollectionOfPaths |
|
109 |
ClassPath := aCollectionOfPaths asOrderedCollection |
|
110 |
||
111 |
"Created: 7.2.1997 / 19:23:45 / cg" |
|
112 |
! |
|
113 |
||
276 | 114 |
excludedClassPath |
115 |
^ ExcludedClassPath |
|
116 |
||
117 |
"Modified: / 7.2.1997 / 19:23:55 / cg" |
|
118 |
"Created: / 27.1.1998 / 21:57:13 / cg" |
|
119 |
! |
|
120 |
||
121 |
isExcludedFromClassPath:fileName |
|
122 |
|nm| |
|
123 |
||
124 |
nm := fileName asFilename pathName. |
|
125 |
ExcludedClassPath do:[:excludedPath | |
|
126 |
(nm startsWith:excludedPath) ifTrue:[^ true]. |
|
127 |
]. |
|
128 |
^ false |
|
129 |
||
130 |
"Created: / 27.1.1998 / 22:00:40 / cg" |
|
131 |
! |
|
132 |
||
201 | 133 |
javaHome |
134 |
^ JavaHome |
|
135 |
||
136 |
"Created: 6.8.1997 / 00:53:19 / cg" |
|
137 |
! |
|
138 |
||
139 |
javaHome:aPath |
|
140 |
JavaHome := aPath |
|
141 |
||
142 |
"Created: 6.8.1997 / 00:53:23 / cg" |
|
143 |
! |
|
144 |
||
192 | 145 |
removeFromClassPath:aPath |
146 |
(ClassPath includes:aPath) ifTrue:[ |
|
248 | 147 |
ClassPath remove:aPath |
192 | 148 |
] |
149 |
||
150 |
"Modified: 7.2.1997 / 19:23:55 / cg" |
|
151 |
"Created: 1.8.1997 / 21:10:21 / cg" |
|
188 | 152 |
! |
153 |
||
198 | 154 |
removeFromSourcePath:aPath |
155 |
(SourceDirectories includes:aPath) ifTrue:[ |
|
248 | 156 |
SourceDirectories remove:aPath |
198 | 157 |
] |
158 |
||
159 |
"Modified: 7.2.1997 / 19:23:55 / cg" |
|
160 |
"Created: 2.8.1997 / 14:13:01 / cg" |
|
161 |
! |
|
162 |
||
76 | 163 |
sourceDirectories |
164 |
^ SourceDirectories |
|
165 |
! |
|
166 |
||
167 |
sourceDirectories:aCollectionOfPaths |
|
168 |
SourceDirectories := aCollectionOfPaths asOrderedCollection |
|
169 |
||
170 |
" |
|
171 |
Java |
|
248 | 172 |
sourceDirectories:#( |
173 |
'/phys/ibm3/java/src' |
|
174 |
) |
|
76 | 175 |
" |
176 |
||
177 |
||
261 | 178 |
! |
179 |
||
180 |
sourcePath |
|
181 |
^ SourceDirectories |
|
182 |
||
183 |
"Created: / 16.1.1998 / 13:26:55 / cg" |
|
24 | 184 |
! ! |
185 |
||
135 | 186 |
!Java class methodsFor:'class initialization'! |
47 | 187 |
|
188 |
initAllClasses |
|
147 | 189 |
|system| |
190 |
||
102 | 191 |
self initAllStaticFields. |
147 | 192 |
|
198 | 193 |
system := self classForName:'java.lang.System'. |
147 | 194 |
system isInitialized ifFalse:[ |
261 | 195 |
system classInit. |
196 |
self initSystemClass. |
|
147 | 197 |
]. |
198 |
||
47 | 199 |
self allClassesDo:[:cls | |
261 | 200 |
cls isInitialized ifFalse:[ |
201 |
cls classInit |
|
202 |
] |
|
47 | 203 |
] |
204 |
||
252 | 205 |
"Modified: / 4.1.1998 / 14:41:12 / cg" |
47 | 206 |
! |
207 |
||
102 | 208 |
initAllStaticFields |
209 |
self allClassesDo:[:cls | |
|
248 | 210 |
cls initializeStaticFields |
102 | 211 |
] |
212 |
||
213 |
! |
|
214 |
||
146 | 215 |
initSystemClass |
198 | 216 |
|system| |
217 |
||
218 |
system := Java at:'java.lang.System'. |
|
255 | 219 |
system isNil ifTrue:[ |
220 |
JavaVM initializeVM. |
|
221 |
system := Java at:'java.lang.System'. |
|
261 | 222 |
system isNil ifTrue:[ |
223 |
self error:'no ''java.lang.System'' class'. |
|
224 |
^ self |
|
225 |
]. |
|
255 | 226 |
]. |
200 | 227 |
(system implements:#'initializeSystemClass()V') ifTrue:[ |
255 | 228 |
system invokeStatic:#'initializeSystemClass()V'. |
198 | 229 |
]. |
146 | 230 |
|
147 | 231 |
" |
232 |
Java initSystemClass |
|
215 | 233 |
|
234 |
(Java at:'java.lang.System') |
|
261 | 235 |
perform:#'getProperty(Ljava/lang/String;)Ljava/lang/String;' |
255 | 236 |
with:(Java as_String:'java.home') |
147 | 237 |
" |
238 |
||
261 | 239 |
"Modified: / 15.1.1998 / 00:29:37 / cg" |
146 | 240 |
! |
241 |
||
76 | 242 |
initialize |
283 | 243 |
|tryJDK1_2 jHome mozillaHome| |
244 |
||
245 |
"/ cannot use JDK1.2 yet (they made so many changes ...) |
|
246 |
tryJDK1_2 := false. |
|
116 | 247 |
|
276 | 248 |
ExcludedClassPath := OrderedCollection new. |
249 |
||
283 | 250 |
tryJDK1_2 ifTrue:[ |
251 |
jHome := '/phys/idefix/home2/java/jdk1.2beta2'. |
|
252 |
]. |
|
253 |
(jHome isNil or:[jHome asFilename exists not]) ifTrue:[ |
|
274 | 254 |
jHome := '/usr/local/java/jdk113'. |
255 |
jHome asFilename exists ifFalse:[ |
|
256 |
jHome := '/usr/lib/java'. |
|
257 |
]. |
|
239 | 258 |
]. |
188 | 259 |
|
239 | 260 |
self javaHome:jHome. |
261 |
self classPath:(Array with:jHome , '/lib/classes'). |
|
262 |
self sourceDirectories:(Array with:jHome , '/src'). |
|
188 | 263 |
|
279 | 264 |
mozillaHome := '/usr/local/java/moz3_0/lib_unix'. |
241 | 265 |
mozillaHome asFilename exists ifTrue:[ |
270 | 266 |
Java addToClassPath:(mozillaHome , '/classes'). |
276 | 267 |
"/ |
268 |
"/ care to only load mozilla classes from there ... |
|
269 |
"/ |
|
270 |
ExcludedClassPath add:(mozillaHome , '/classes/java/'). |
|
279 | 271 |
ExcludedClassPath add:(mozillaHome , '/classes/sun/'). |
241 | 272 |
]. |
188 | 273 |
self initializePrettyPrintStyle. |
274 |
||
274 | 275 |
" |
276 |
Java initialize |
|
277 |
JavaVM initializeVM |
|
278 |
" |
|
76 | 279 |
|
283 | 280 |
"Modified: / 28.1.1998 / 22:05:35 / cg" |
188 | 281 |
! |
282 |
||
283 |
initializePrettyPrintStyle |
|
284 |
PrettyPrintStyle := IdentityDictionary new. |
|
285 |
PrettyPrintStyle at:#accessAttribute put:(#color -> (Color red:0 green:0 blue:25)). |
|
286 |
PrettyPrintStyle at:#className put:(Array with:(#color -> Color black) with:#bold). |
|
287 |
PrettyPrintStyle at:#methodName put:(Array with:(#color -> Color black) with:#bold). |
|
288 |
PrettyPrintStyle at:#code put:(#color -> Color black). |
|
289 |
||
290 |
||
291 |
" |
|
292 |
Java initializePrettyPrintStyle |
|
293 |
" |
|
294 |
||
295 |
"Created: 1.8.1997 / 11:08:43 / cg" |
|
296 |
"Modified: 1.8.1997 / 11:09:58 / cg" |
|
76 | 297 |
! |
298 |
||
47 | 299 |
reinitAllClasses |
300 |
self markAllClassesUninitialized. |
|
301 |
self initAllClasses |
|
252 | 302 |
|
303 |
" |
|
304 |
Java reinitAllClasses |
|
305 |
" |
|
306 |
||
307 |
"Modified: / 4.1.1998 / 00:34:29 / cg" |
|
248 | 308 |
! |
309 |
||
310 |
reinitialize |
|
311 |
"/ all JavaThreads are lost on a restart (for now) |
|
312 |
||
313 |
Threads := nil. |
|
314 |
||
285 | 315 |
" |
316 |
Java reinitialize |
|
317 |
" |
|
248 | 318 |
|
285 | 319 |
"Created: / 26.8.1997 / 20:07:00 / cg" |
320 |
"Modified: / 28.1.1998 / 23:13:07 / cg" |
|
248 | 321 |
! |
322 |
||
323 |
startupJavaSystem |
|
301
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
324 |
|haveEventThread haveScreenUpdater| |
285 | 325 |
|
248 | 326 |
"/ |
327 |
"/ check if already running |
|
328 |
"/ |
|
301
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
329 |
|
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
330 |
haveEventThread := true. |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
331 |
(JavaEventThread isNil or:[JavaEventThread isDead]) ifTrue:[ |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
332 |
haveEventThread := false |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
333 |
]. |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
334 |
|
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
335 |
haveScreenUpdater := false. |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
336 |
haveEventThread ifTrue:[ |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
337 |
self threads do:[:aJavaThread | |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
338 |
aJavaThread name = 'JAVA-Screen Updater' ifTrue:[ |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
339 |
aJavaThread isDead ifFalse:[ |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
340 |
"/ already running |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
341 |
haveScreenUpdater := true |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
342 |
] |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
343 |
] |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
344 |
]. |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
345 |
]. |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
346 |
|
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
347 |
(haveEventThread and:[haveScreenUpdater]) ifTrue:[ |
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
348 |
^ self |
248 | 349 |
]. |
350 |
||
351 |
'JAVA [info]: (re)initializing JAVA environment completely ...' infoPrintCR. |
|
285 | 352 |
|
255 | 353 |
JavaVM initializeVM. |
285 | 354 |
|
248 | 355 |
'JAVA [info]: done JAVA initialization.' infoPrintCR. |
356 |
||
285 | 357 |
" |
358 |
Java startupJavaSystem |
|
359 |
" |
|
248 | 360 |
|
301
2945ec2eee92
better reinit (look for both event-thread AND screenUpdater thread)
cg
parents:
285
diff
changeset
|
361 |
"Modified: / 1.2.1998 / 18:02:00 / cg" |
248 | 362 |
! |
363 |
||
364 |
terminateAllThreads |
|
305 | 365 |
|myself threadsToKill| |
248 | 366 |
|
367 |
Threads isNil ifTrue:[ |
|
270 | 368 |
^ self |
248 | 369 |
]. |
370 |
||
371 |
myself := Processor activeProcess. |
|
372 |
||
305 | 373 |
threadsToKill := IdentitySet new. |
248 | 374 |
Threads do:[:aJavaThread | |
270 | 375 |
aJavaThread ~~ myself ifTrue:[ |
376 |
(aJavaThread isNil or:[aJavaThread == 0]) ifFalse:[ |
|
377 |
(aJavaThread isMemberOf:JavaProcess) ifTrue:[ |
|
378 |
aJavaThread isDead ifFalse:[ |
|
305 | 379 |
threadsToKill add:aJavaThread |
270 | 380 |
] |
381 |
] |
|
382 |
] |
|
383 |
] |
|
248 | 384 |
]. |
305 | 385 |
threadsToKill do:[:aThread | |
386 |
aThread terminate |
|
387 |
]. |
|
248 | 388 |
Threads := nil. |
389 |
||
390 |
" |
|
391 |
Java terminateAllThreads |
|
392 |
" |
|
393 |
||
270 | 394 |
"Created: / 26.8.1997 / 19:57:40 / cg" |
305 | 395 |
"Modified: / 1.2.1998 / 20:11:56 / cg" |
47 | 396 |
! ! |
397 |
||
135 | 398 |
!Java class methodsFor:'enumerating'! |
43 | 399 |
|
90 | 400 |
allClasses |
208 | 401 |
^ Classes ? #() |
402 |
||
280 | 403 |
" |
404 |
|if| |
|
405 |
||
406 |
if := Java at:'java.awt.GraphicsEnvironment'. |
|
407 |
Java allClasses select:[:aClass | |
|
408 |
aClass hasInterface:if |
|
409 |
] |
|
410 |
" |
|
411 |
||
412 |
"Modified: / 28.1.1998 / 01:42:04 / cg" |
|
90 | 413 |
! |
414 |
||
43 | 415 |
allClassesDo:aBlock |
416 |
Classes notNil ifTrue:[ |
|
248 | 417 |
Classes do:aBlock |
43 | 418 |
] |
419 |
! ! |
|
420 |
||
205 | 421 |
!Java class methodsFor:'object conversions'! |
422 |
||
423 |
as_Float:aNumber |
|
424 |
"convert an ST-float into a Java Float" |
|
425 |
||
426 |
|i| |
|
427 |
||
428 |
i := (Java at:'java.lang.Float') new. |
|
261 | 429 |
i perform:#'<init>(F)V' with:(aNumber asShortFloat). |
205 | 430 |
^ i |
431 |
||
432 |
" |
|
433 |
Java as_Float:1 |
|
434 |
Java as_Float:3.14159 |
|
435 |
" |
|
436 |
||
437 |
"Created: 7.8.1997 / 21:21:13 / cg" |
|
438 |
"Modified: 7.8.1997 / 21:22:05 / cg" |
|
439 |
! |
|
440 |
||
441 |
as_Hashtable:aDictionary |
|
261 | 442 |
"given a smalltalk dictionary, create and return |
443 |
a Java hashTable for it" |
|
444 |
||
205 | 445 |
|hashTable| |
446 |
||
447 |
hashTable := (self classForName:'java.util.Hashtable') new. |
|
448 |
aDictionary keysAndValuesDo:[:k :v | |
|
261 | 449 |
|sk sv jk jv| |
205 | 450 |
|
261 | 451 |
(sk := k) isSymbol ifTrue:[ |
452 |
sk := sk asString |
|
453 |
]. |
|
454 |
(sv := v) isSymbol ifTrue:[ |
|
455 |
sv := sv asString |
|
456 |
]. |
|
457 |
jk := self as_Object:sk. |
|
458 |
jv := self as_Object:sv. |
|
255 | 459 |
|
261 | 460 |
hashTable |
461 |
perform:#'put(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;' |
|
462 |
with:jk |
|
463 |
with:jv. |
|
205 | 464 |
]. |
465 |
^ hashTable |
|
466 |
||
467 |
" |
|
468 |
Java as_Hashtable:(Dictionary new |
|
261 | 469 |
at:'hello' put:'Hallo'; |
470 |
at:'world' put:'Welt'; |
|
471 |
yourself) |
|
205 | 472 |
" |
473 |
||
261 | 474 |
"Modified: / 14.1.1998 / 17:02:13 / cg" |
205 | 475 |
! |
476 |
||
477 |
as_Integer:anInteger |
|
478 |
"convert an ST-integer into a Java Integer" |
|
479 |
||
480 |
|i| |
|
481 |
||
482 |
i := (Java at:'java.lang.Integer') new. |
|
261 | 483 |
i perform:#'<init>(I)V' with:anInteger. |
205 | 484 |
^ i |
485 |
||
486 |
" |
|
487 |
Java as_Integer:1 |
|
488 |
" |
|
489 |
||
490 |
"Modified: 7.8.1997 / 21:19:37 / cg" |
|
491 |
"Created: 7.8.1997 / 21:21:13 / cg" |
|
492 |
! |
|
493 |
||
494 |
as_Object:anObject |
|
495 |
"convert an ST-Object into a Java Object" |
|
496 |
||
497 |
anObject isString ifTrue:[ |
|
248 | 498 |
^ self as_String:anObject |
205 | 499 |
]. |
500 |
anObject isInteger ifTrue:[ |
|
248 | 501 |
^ self as_Integer:anObject |
205 | 502 |
]. |
503 |
||
504 |
self halt. |
|
505 |
||
506 |
" |
|
507 |
Java as_Object:'hello world' |
|
508 |
" |
|
509 |
||
510 |
"Created: 7.8.1997 / 21:15:38 / cg" |
|
511 |
"Modified: 7.8.1997 / 21:20:07 / cg" |
|
512 |
! |
|
513 |
||
514 |
as_ST_String:aJavaString |
|
515 |
"hard-coding internas of java.lang.String here is bad ..." |
|
516 |
||
255 | 517 |
|str count start stop| |
518 |
||
519 |
"/ count := aJavaString instVarNamed:'count'. |
|
520 |
count := aJavaString instVarAt:(JavaSlotIndexCache string_slot_count). |
|
521 |
||
522 |
"/ str := aJavaString instVarNamed:'value' |
|
523 |
str := aJavaString instVarAt:(JavaSlotIndexCache string_slot_value). |
|
524 |
||
525 |
str size == count ifTrue:[ |
|
261 | 526 |
^ str |
255 | 527 |
]. |
205 | 528 |
|
529 |
"/ start := (aJavaString instVarNamed:'offset') + 1. |
|
530 |
start := (aJavaString instVarAt:(JavaSlotIndexCache string_slot_offset)) + 1. |
|
531 |
||
532 |
"/ stop := start + (aJavaString instVarNamed:'count') - 1. |
|
533 |
stop := start + (aJavaString instVarAt:(JavaSlotIndexCache string_slot_count)) - 1. |
|
534 |
||
535 |
"/ ^ ((aJavaString instVarNamed:'value') copyFrom:start to:stop) asString |
|
536 |
^ ((aJavaString instVarAt:(JavaSlotIndexCache string_slot_value)) |
|
261 | 537 |
copyFrom:start to:stop) asString |
205 | 538 |
|
255 | 539 |
"Created: / 8.8.1997 / 12:02:55 / cg" |
540 |
"Modified: / 9.1.1998 / 23:55:39 / cg" |
|
205 | 541 |
! |
542 |
||
543 |
as_String:aString |
|
544 |
"convert an ST-String into a Java String" |
|
545 |
||
546 |
"hard-coding internas of java.lang.String here is bad ..." |
|
547 |
||
548 |
|s| |
|
549 |
||
550 |
s := Java_lang_String basicNew. |
|
551 |
s instVarNamed:'value' put: aString. |
|
552 |
s instVarNamed:'offset' put: 0. |
|
553 |
s instVarNamed:'count' put: aString size. |
|
554 |
^ s |
|
555 |
||
556 |
" |
|
557 |
Java as_String:'hello world' |
|
558 |
" |
|
559 |
||
560 |
"Created: 7.8.1997 / 21:15:49 / cg" |
|
561 |
"Modified: 7.8.1997 / 21:19:37 / cg" |
|
562 |
! |
|
563 |
||
564 |
as_URL:aString |
|
565 |
"convert an ST-String into a Java String" |
|
566 |
||
567 |
"hard-coding internas of java.lang.String here is bad ..." |
|
568 |
||
569 |
|u| |
|
570 |
||
571 |
u := (self at:'java.net.URL') basicNew. |
|
261 | 572 |
u perform:#'<init>(Ljava/lang/String;)V' with:(self as_String:aString). |
205 | 573 |
^ u |
574 |
||
575 |
" |
|
576 |
Java as_URL:'http://www.altavista.com' |
|
577 |
Java as_URL:'file:/foo/bar' |
|
578 |
" |
|
579 |
||
580 |
"Created: 7.8.1997 / 21:15:49 / cg" |
|
581 |
"Modified: 7.8.1997 / 21:26:42 / cg" |
|
582 |
! ! |
|
583 |
||
168 | 584 |
!Java class methodsFor:'queries'! |
585 |
||
586 |
smalltalkClassFor:typeRef |
|
587 |
self halt. |
|
588 |
"/ (typeString startsWith:'[[') ifTrue:[ |
|
589 |
"/ ^ Array |
|
590 |
"/ ]. |
|
591 |
"/ (typeString startsWith:'[F') ifTrue:[ |
|
592 |
"/ ^ FloatArray |
|
593 |
"/ ]. |
|
594 |
self halt. |
|
595 |
||
596 |
"Modified: 7.4.1997 / 13:33:46 / cg" |
|
597 |
! |
|
598 |
||
599 |
smalltalkDerefType:typeString |
|
600 |
(typeString startsWith:'[') ifTrue:[ |
|
248 | 601 |
^ typeString copyFrom:2 |
168 | 602 |
]. |
603 |
self halt. |
|
604 |
||
605 |
"Modified: 7.4.1997 / 13:13:24 / cg" |
|
606 |
! ! |
|
607 |
||
135 | 608 |
!Java class methodsFor:'registering java classes'! |
17 | 609 |
|
610 |
at:aJavaName |
|
43 | 611 |
"search & return for a class by name; |
612 |
if there is no such class, return nil. |
|
613 |
The argument, aJavaName may be either an internal name |
|
614 |
(sep'd by slashes) or an external name (sep'd by periods). |
|
615 |
The 'java/lang' standard packages prefix may be ommitted." |
|
616 |
||
617 |
|sym nm cls| |
|
618 |
||
17 | 619 |
Classes isNil ifTrue:[^ nil]. |
43 | 620 |
|
621 |
sym := aJavaName asSymbolIfInterned. |
|
622 |
sym notNil ifTrue:[ |
|
248 | 623 |
cls := Classes at:sym ifAbsent:nil. |
624 |
cls notNil ifTrue:[^ cls]. |
|
43 | 625 |
]. |
17 | 626 |
|
43 | 627 |
nm := aJavaName. |
628 |
(nm includes:$.) ifTrue:[ |
|
252 | 629 |
nm := (nm asString copy replaceAll:$. with:$/). |
248 | 630 |
sym := nm asSymbolIfInterned. |
631 |
sym notNil ifTrue:[ |
|
632 |
cls := Classes at:sym ifAbsent:nil. |
|
633 |
cls notNil ifTrue:[^ cls]. |
|
634 |
]. |
|
43 | 635 |
]. |
636 |
||
637 |
(nm includes:$/) ifFalse:[ |
|
248 | 638 |
"/ |
639 |
"/ try java.lang.Foo |
|
640 |
"/ |
|
641 |
nm := 'java/lang/' , nm. |
|
642 |
sym := nm asSymbolIfInterned. |
|
643 |
sym notNil ifTrue:[ |
|
644 |
cls := Classes at:sym ifAbsent:nil. |
|
645 |
cls notNil ifTrue:[^ cls]. |
|
646 |
]. |
|
43 | 647 |
]. |
648 |
||
649 |
^ nil |
|
650 |
||
651 |
" |
|
652 |
Java at:'java/lang/String' |
|
653 |
Java at:'java.lang.String' |
|
654 |
Java at:'String' |
|
655 |
Java at:'System' |
|
656 |
Java at:'Foo' |
|
657 |
" |
|
17 | 658 |
! |
659 |
||
660 |
at:aJavaName put:aJavaClass |
|
90 | 661 |
|nameSymbol| |
662 |
||
17 | 663 |
Classes isNil ifTrue:[ |
248 | 664 |
Classes := IdentityDictionary new. |
17 | 665 |
]. |
90 | 666 |
|
667 |
nameSymbol := aJavaName asSymbol. |
|
668 |
(Classes includesKey:nameSymbol) ifTrue:[ |
|
248 | 669 |
('JAVA: class ' , aJavaName , ' is already loaded') infoPrintCR. |
670 |
self updateClassRefsFrom:(Classes at:nameSymbol) to:aJavaClass. |
|
65 | 671 |
]. |
54 | 672 |
|
90 | 673 |
Classes at:nameSymbol put:aJavaClass. |
674 |
nameSymbol == #'java/lang/String' ifTrue:[ |
|
248 | 675 |
Java_lang_String := aJavaClass |
90 | 676 |
]. |
21 | 677 |
|
43 | 678 |
"/ UnresolvedClassRefs notNil ifTrue:[ |
679 |
"/ UnresolvedClassRefs do:[:toResolve | |
|
680 |
"/ ] |
|
681 |
"/ ]. |
|
682 |
||
27 | 683 |
self changed:#classes |
17 | 684 |
|
685 |
"Created: 17.4.1996 / 23:29:31 / cg" |
|
205 | 686 |
"Modified: 7.8.1997 / 19:15:58 / cg" |
21 | 687 |
! |
688 |
||
168 | 689 |
flushAllJavaResources |
690 |
self flushClasses. |
|
691 |
||
692 |
ObjectMemory allObjectsDo:[:someObject | |
|
318 | 693 |
someObject isBehavior ifTrue:[ |
694 |
someObject isJavaClass ifTrue:[ |
|
695 |
someObject setConstantPool:nil. |
|
696 |
someObject setInterfaces:nil. |
|
697 |
someObject setMethodDictionary:(MethodDictionary new). |
|
698 |
'JAVA [info]: flushing ' print. someObject fullName printCR. |
|
699 |
] |
|
700 |
]. |
|
701 |
(someObject isMemberOf:JavaMethod) ifTrue:[ |
|
702 |
someObject setJavaClass:nil. |
|
703 |
someObject setExceptionTable:nil. |
|
704 |
]. |
|
705 |
(someObject isMemberOf:JavaUnresolvedClassConstant) ifTrue:[ |
|
706 |
someObject constantPool:nil |
|
707 |
]. |
|
168 | 708 |
]. |
709 |
self flushClasses |
|
710 |
||
711 |
" |
|
712 |
Java flushAllJavaResources |
|
713 |
" |
|
714 |
||
318 | 715 |
"Modified: / 6.2.1998 / 02:25:16 / cg" |
168 | 716 |
! |
717 |
||
21 | 718 |
flushClasses |
167 | 719 |
Classes := UnresolvedClassRefs := nil. |
168 | 720 |
Smalltalk keys copy do:[:aKey | |
248 | 721 |
(aKey startsWith:'JAVA::') ifTrue:[ Smalltalk removeKey:aKey ] |
168 | 722 |
]. |
167 | 723 |
Java_lang_String := Java_lang_Class := nil. |
261 | 724 |
JavaVM releaseAllJavaResources. |
90 | 725 |
JavaUnresolvedConstant flushPatchLists. |
21 | 726 |
|
727 |
" |
|
728 |
Java flushClasses |
|
729 |
" |
|
730 |
||
168 | 731 |
"Modified: 7.4.1997 / 15:25:46 / cg" |
21 | 732 |
! |
733 |
||
43 | 734 |
markAllClassesUninitialized |
735 |
self allClassesDo:[:aJavaClass | |
|
248 | 736 |
aJavaClass markUninitialized |
43 | 737 |
]. |
738 |
||
739 |
" |
|
740 |
Java markAllClassesUninitialized |
|
741 |
" |
|
742 |
! |
|
743 |
||
21 | 744 |
rememberUnresolved:anUnresolvedClassRef |
90 | 745 |
('JAVA: remember unresolved class: ' , anUnresolvedClassRef fullName) infoPrintCR. |
746 |
||
747 |
UnresolvedClassRefs isNil ifTrue:[ |
|
248 | 748 |
UnresolvedClassRefs := Set new. |
90 | 749 |
]. |
750 |
UnresolvedClassRefs add:anUnresolvedClassRef |
|
21 | 751 |
|
752 |
"Created: 18.4.1996 / 00:05:31 / cg" |
|
753 |
"Modified: 18.4.1996 / 00:05:41 / cg" |
|
90 | 754 |
! |
755 |
||
162 | 756 |
removeClass:aJavaClass |
757 |
"/ remove it from myself |
|
758 |
||
759 |
|javaName sym nm cls | |
|
760 |
||
761 |
javaName := aJavaClass fullName. |
|
762 |
||
763 |
sym := javaName asSymbolIfInterned. |
|
764 |
sym notNil ifTrue:[ |
|
248 | 765 |
cls := Classes at:sym ifAbsent:nil. |
162 | 766 |
]. |
767 |
cls isNil ifTrue:[ |
|
248 | 768 |
nm := javaName. |
769 |
(nm includes:$.) ifTrue:[ |
|
770 |
"/ |
|
771 |
"/ try pckg/.../name |
|
772 |
"/ |
|
252 | 773 |
nm := (nm asString copy replaceAll:$. with:$/). |
248 | 774 |
sym := nm asSymbolIfInterned. |
775 |
sym notNil ifTrue:[ |
|
776 |
cls := Classes at:sym ifAbsent:nil. |
|
777 |
]. |
|
778 |
]. |
|
162 | 779 |
]. |
212 | 780 |
(cls notNil and:[cls == aJavaClass]) ifTrue:[ |
248 | 781 |
Classes removeKey:sym. |
782 |
self updateClassRefsFrom:aJavaClass to:nil. |
|
162 | 783 |
]. |
212 | 784 |
Smalltalk removeKey:('JAVA::' , aJavaClass fullName) asSymbol. |
162 | 785 |
|
212 | 786 |
"Modified: 12.8.1997 / 02:54:34 / cg" |
162 | 787 |
! |
788 |
||
90 | 789 |
unresolvedClassRefFor:aClassName |
790 |
UnresolvedClassRefs isNil ifTrue:[^ nil]. |
|
791 |
^ UnresolvedClassRefs at:aClassName ifAbsent:nil. |
|
167 | 792 |
! |
793 |
||
794 |
updateClassRefsFrom:oldClass to:newClass |
|
795 |
"update all references to oldClass to now refer to newClass. |
|
796 |
sent, when a class is reloaded" |
|
797 |
||
214 | 798 |
newClass notNil ifTrue:[ |
248 | 799 |
"/ |
800 |
"/ kludge: the new class might have been resolved with the oldClass ... |
|
801 |
"/ |
|
802 |
newClass constantPool |
|
803 |
updateClassRefsFrom:oldClass to:newClass. |
|
214 | 804 |
]. |
205 | 805 |
|
203 | 806 |
self allClassesDo:[:aJavaClass | |
248 | 807 |
aJavaClass updateClassRefsFrom:oldClass to:newClass |
203 | 808 |
]. |
167 | 809 |
|
810 |
"Created: 26.3.1997 / 13:49:20 / cg" |
|
214 | 811 |
"Modified: 12.8.1997 / 03:04:44 / cg" |
17 | 812 |
! ! |
813 |
||
135 | 814 |
!Java class methodsFor:'source management'! |
76 | 815 |
|
816 |
classSourceOf:aClass |
|
261 | 817 |
|package dirName binary sourceFileName sourceFile dirHolder fileName path |
818 |
loader codeBaseURL protocol dir file| |
|
76 | 819 |
|
820 |
aClass isNil ifTrue:[ |
|
267 | 821 |
^ nil |
76 | 822 |
]. |
823 |
||
824 |
"/ first, look in the directory, where the binary |
|
825 |
"/ was loaded from. |
|
826 |
||
198 | 827 |
binary := aClass binaryFile. |
828 |
binary notNil ifTrue:[ |
|
267 | 829 |
binary := binary asFilename. |
324 | 830 |
aClass sourceFile notNil ifTrue:[ |
831 |
sourceFileName := binary directory constructString:(aClass sourceFile). |
|
832 |
sourceFileName asFilename exists ifFalse:[ |
|
833 |
sourceFileName := nil. |
|
834 |
] |
|
835 |
]. |
|
836 |
sourceFileName isNil ifTrue:[ |
|
837 |
sourceFileName := binary withSuffix:'java'. |
|
838 |
]. |
|
267 | 839 |
sourceFile := sourceFileName asFilename. |
261 | 840 |
] ifFalse:[ |
267 | 841 |
"/ maybe it was loaded by a java classLoader ... |
842 |
(loader := aClass classLoader) notNil ifTrue:[ |
|
843 |
(codeBaseURL := loader instVarNamed:'codeBaseURL') notNil ifTrue:[ |
|
844 |
(protocol := codeBaseURL instVarNamed:'protocol') notNil ifTrue:[ |
|
845 |
(Java as_ST_String:protocol) = 'file' ifTrue:[ |
|
846 |
dirName := Java as_ST_String:(codeBaseURL instVarNamed:'file'). |
|
847 |
dirName asFilename exists ifTrue:[ |
|
848 |
aClass sourceFile notNil ifTrue:[ |
|
849 |
sourceFileName := dirName asFilename construct:aClass sourceFile. |
|
850 |
sourceFile := sourceFileName asFilename. |
|
851 |
] |
|
852 |
] |
|
853 |
] |
|
854 |
] |
|
855 |
] |
|
856 |
] |
|
198 | 857 |
]. |
858 |
sourceFile notNil ifTrue:[ |
|
267 | 859 |
sourceFile exists ifFalse:[ |
860 |
binary notNil ifTrue:[ |
|
861 |
sourceFileName := binary withSuffix:'jav'. |
|
862 |
sourceFile := sourceFileName asFilename. |
|
863 |
sourceFile exists ifFalse:[ |
|
864 |
sourceFileName := binary withSuffix:'JAV'. |
|
865 |
sourceFile := sourceFileName asFilename. |
|
866 |
sourceFile exists ifFalse:[ |
|
867 |
sourceFileName := binary withSuffix:'JAVA'. |
|
868 |
sourceFile := sourceFileName asFilename. |
|
869 |
]. |
|
870 |
]. |
|
871 |
] |
|
872 |
]. |
|
76 | 873 |
]. |
874 |
||
176 | 875 |
"/ special case: there were multiple classes in a single |
76 | 876 |
"/ source file. |
877 |
||
198 | 878 |
binary notNil ifTrue:[ |
267 | 879 |
binary withoutSuffix baseName ~= aClass sourceFile asFilename withoutSuffix baseName ifTrue:[ |
880 |
'JAVA: trouble extracting fileName: ' print. |
|
881 |
binary withoutSuffix baseName print. ' vs. ' print. |
|
882 |
aClass sourceFile asFilename withoutSuffix baseName printCR. |
|
883 |
]. |
|
76 | 884 |
]. |
885 |
||
886 |
"/ if that fails, look in standard places |
|
887 |
||
198 | 888 |
(sourceFile isNil or:[sourceFile exists not]) ifTrue:[ |
324 | 889 |
package := aClass package. |
890 |
||
267 | 891 |
sourceFileName := aClass sourceFile. |
892 |
sourceFile := sourceFileName asFilename. |
|
893 |
sourceFile exists ifFalse:[ |
|
894 |
"/ |
|
895 |
"/ mhmh - look for its directory |
|
896 |
"/ |
|
897 |
dirName := sourceFile directoryName. |
|
898 |
fileName := sourceFile baseName. |
|
76 | 899 |
|
267 | 900 |
(dirName asFilename exists |
901 |
and:[(dirName asFilename construct:(package , '/' , fileName)) exists]) |
|
902 |
ifFalse:[ |
|
903 |
(dirName asFilename exists |
|
904 |
and:[(dirName asFilename construct:(fileName)) exists]) |
|
905 |
ifFalse:[ |
|
906 |
dirName := self findSourceDirOf:fileName inPackage:package. |
|
76 | 907 |
|
908 |
"/ [dirName isNil] whileTrue:[ |
|
909 |
"/ dirName := Dialog requestDirectoryName:'top directory for ' , package , '/' , fileName. |
|
910 |
"/ (dirName isNil or:[dirName isEmpty]) ifTrue:[^ self]. |
|
911 |
"/ ]. |
|
267 | 912 |
]. |
913 |
]. |
|
76 | 914 |
|
267 | 915 |
(dirName notNil and:[dirName asFilename exists]) ifTrue:[ |
916 |
path := (dirName asFilename construct:(package , '/' , fileName)) asFilename. |
|
917 |
path exists ifFalse:[ |
|
918 |
path := (dirName asFilename construct:(fileName)) asFilename. |
|
919 |
]. |
|
920 |
]. |
|
76 | 921 |
|
267 | 922 |
(path notNil and:[path exists]) ifFalse:[ |
923 |
^ nil |
|
924 |
]. |
|
76 | 925 |
|
267 | 926 |
sourceFile := path asFilename. |
927 |
]. |
|
76 | 928 |
]. |
929 |
^ (sourceFile contentsOfEntireFile). |
|
930 |
||
324 | 931 |
"Modified: / 29.3.1998 / 11:55:46 / cg" |
76 | 932 |
! |
933 |
||
934 |
findSourceDirOf:fileName inPackage:aPackage |
|
935 |
SourceDirectories notNil ifTrue:[ |
|
324 | 936 |
SourceDirectories do:[:aDir | |
937 |
aPackage notNil ifTrue:[ |
|
938 |
(aDir asFilename construct:('/' , aPackage , '/' , fileName)) |
|
939 |
asFilename exists ifTrue:[ |
|
940 |
^ aDir |
|
941 |
]. |
|
942 |
]. |
|
943 |
(aDir asFilename construct:('/' , fileName)) |
|
944 |
asFilename exists ifTrue:[ |
|
945 |
^ aDir |
|
946 |
]. |
|
947 |
] |
|
76 | 948 |
]. |
949 |
^ nil |
|
950 |
||
324 | 951 |
"Modified: / 29.3.1998 / 21:46:40 / cg" |
76 | 952 |
! ! |
953 |
||
224 | 954 |
!Java class methodsFor:'starting apps'! |
955 |
||
261 | 956 |
executeMainOf:aClass |
957 |
"execute main of aClass in a separate thread and wait until that thread |
|
958 |
has terminated." |
|
959 |
||
960 |
|p| |
|
961 |
||
962 |
p := self javaProcessForMainOf:aClass. |
|
963 |
p notNil ifTrue:[ |
|
964 |
p resume. |
|
965 |
Object abortSignal handle:[:ex | |
|
966 |
p terminate. |
|
967 |
ex reject. |
|
968 |
] do:[ |
|
969 |
p waitUntilTerminated |
|
970 |
]. |
|
971 |
] |
|
972 |
||
973 |
"Modified: / 15.1.1998 / 02:15:13 / cg" |
|
974 |
"Created: / 15.1.1998 / 17:14:55 / cg" |
|
975 |
! |
|
976 |
||
313 | 977 |
executeMainOf:aClass withArgumentString:commandLineString |
978 |
"execute main of aClass in a separate thread and wait until that thread |
|
979 |
has terminated." |
|
980 |
||
981 |
|p| |
|
982 |
||
983 |
p := self javaProcessForMainOf:aClass argumentString:commandLineString. |
|
984 |
p notNil ifTrue:[ |
|
985 |
p resume. |
|
986 |
Object abortSignal handle:[:ex | |
|
987 |
p terminate. |
|
988 |
ex reject. |
|
989 |
] do:[ |
|
990 |
p waitUntilTerminated |
|
991 |
]. |
|
992 |
] |
|
993 |
||
994 |
"Modified: / 5.2.1998 / 00:38:22 / cg" |
|
995 |
"Created: / 5.2.1998 / 00:40:38 / cg" |
|
996 |
! |
|
997 |
||
224 | 998 |
javaProcessForMainOf:aJavaClass |
999 |
"ask for a commandLine, create a java process to invoke |
|
1000 |
its main and return it. The process is not scheduled for |
|
1001 |
execution." |
|
1002 |
||
1003 |
|args| |
|
1004 |
||
1005 |
args := Dialog |
|
261 | 1006 |
request:'argument string:' |
1007 |
initialAnswer:LastArgumentString ? '' |
|
1008 |
onCancel:nil. |
|
228 | 1009 |
args isNil ifTrue:[^ nil]. |
224 | 1010 |
|
1011 |
LastArgumentString := args. |
|
1012 |
||
1013 |
^ self javaProcessForMainOf:aJavaClass argumentString:args. |
|
1014 |
||
255 | 1015 |
"Created: / 15.8.1997 / 04:51:46 / cg" |
1016 |
"Modified: / 9.1.1998 / 18:01:47 / cg" |
|
224 | 1017 |
! |
1018 |
||
1019 |
javaProcessForMainOf:aJavaClass argumentString:argString |
|
1020 |
"create a java process, ready to invoke the classes main |
|
1021 |
method. Returns the process - ready to run but not yet resumed" |
|
1022 |
||
1023 |
|p argStringArray t| |
|
1024 |
||
261 | 1025 |
JavaVM initializeVMIfNoEventThreadRunning. |
1026 |
(Java at:'java.lang.System') instVarNamed:'security' put:nil. |
|
1027 |
||
224 | 1028 |
argString isEmpty ifTrue:[ |
252 | 1029 |
argStringArray := #() |
224 | 1030 |
] ifFalse:[ |
252 | 1031 |
argStringArray := argString asCollectionOfWords asArray |
1032 |
collect:[:s | Java as_String:s]. |
|
224 | 1033 |
]. |
1034 |
||
1035 |
p := JavaProcess |
|
252 | 1036 |
for:[ |
1037 |
aJavaClass |
|
1038 |
invoke:#'main' |
|
1039 |
signature:#'([Ljava/lang/String;)V' |
|
1040 |
with:argStringArray |
|
1041 |
] |
|
1042 |
priority:(Processor activePriority - 1). |
|
224 | 1043 |
|
1044 |
p name:(aJavaClass fullName , '::main()'). |
|
1045 |
^ p |
|
1046 |
||
252 | 1047 |
"Created: / 15.8.1997 / 04:41:20 / cg" |
261 | 1048 |
"Modified: / 15.1.1998 / 17:33:18 / cg" |
224 | 1049 |
! ! |
1050 |
||
135 | 1051 |
!Java class methodsFor:'documentation'! |
17 | 1052 |
|
1053 |
version |
|
324 | 1054 |
^ '$Header: /home/jv/Projects/SmalltalkX/repositories/cvs/stx/libjava/Java.st,v 1.62 1998/03/30 15:26:47 cg Exp $' |
17 | 1055 |
! ! |
76 | 1056 |
Java initialize! |